[Qt-interest] why parent widget in constructor?

Scott Aron Bloom Scott.Bloom at sabgroup.com
Wed Apr 29 09:54:15 CEST 2009



> -----Original Message-----
> From: qt-interest-bounces at trolltech.com [mailto:qt-interest-
> bounces at trolltech.com] On Behalf Of Luca Ferrari
> Sent: Tuesday, April 28, 2009 11:55 PM
> To: qt-interest at trolltech.com
> Subject: Re: [Qt-interest] why parent widget in constructor?
> 
> On Monday 27 April 2009 18:01:29 Andreas Pakulat's cat walking on the
keyboard
> wrote:
> > On 27.04.09 15:06:39, Luca Ferrari wrote:
> > > > a) a object hierarchy, that manages itself. If I delete an
object in
> > > > the hierarchy the whole subtree is deleted, without me needing
to do
> > > > anything. This is very nice for temporary windows/dialogs, I can
create
> > > > the window on the stack and even if all its children are created
on the
> > > > heap, they'll be deleted for me once the window goes out of
scope.
> > > >
> 
> A doubt: so the object tree means that having a widget A that is
parent of B
> and C, when A is deleted also B and C are deleted?
> In this case, isn't it dangerous if I keep a pointer to B for use in
another
> widget? Is there any real case this could happen or it is a bad
programming
> style?
> 
> Luca
> 
> 

Yes b & c would be delted if their parent pointer was deleted.

This is issues is no different when you own or keep a pointer that you
don't control the lifespan.


That said, QT makes like a bit easier because they have the class
QPointer (it's a template class) that is connected to the destroyed()
signal, and sets the returned pointer to null .. 

So if you have that situation, you check for NULL and youre ok.

Scott




More information about the Qt-interest-old mailing list