[Qt-interest] why parent widget in constructor?

Andreas Pakulat apaku at gmx.de
Wed Apr 29 09:53:19 CEST 2009


On 29.04.09 08:55:21, Luca Ferrari wrote:
> 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?

Yes.

> In this case, isn't it dangerous if I keep a pointer to B for use in another 
> widget?

Possibly, yes. Thats why people use QPointer or other smart-pointer
implementations. 

> Is there any real case this could happen or it is a bad programming 
> style?

Its not uncommon for a widet to expose its childs to the outside. So
whoever wants to use the widgets childs has to be sure that its parent
still exists.

Andreas

-- 
Tomorrow will be cancelled due to lack of interest.



More information about the Qt-interest-old mailing list