[Qt-interest] Is child object guaranteed to be delete when its parent destroy?

Andreas Pakulat apaku at gmx.de
Sat Mar 28 09:13:11 CET 2009


On 28.03.09 11:34:47, Kermit Mei wrote:
> Hello community!
> 
> I always consider that if a parent object calls deleteLater(), then all
> of its children will delete automatically. But I can't understand what I
> encouterred today. To represent it simply, for example:
> 
> A,B,C are all QWidget's sub-classes, and they are different. Now, in
> the defination of A's method, I create a B's object:
> 
> A::fooA() {
>   pB = new B(this);
> }
> 
> And later, I pass pB to a QStackedWidget's Object, and add it to the
> stacked-object.

With that you're giving away pB to the stackwidget. The stackwidget
takes ownership of the widgets in its stack, so the parent of pB is
changed to be the stackwidget.

> I know I can delete it in ~A(), but I wonder to know why it can't delete
> it self when A was deleted? It is a child of, isn't it? 

Because A is not the parent anymore.

Andreas

-- 
Do nothing unless you must, and when you must act -- hesitate.



More information about the Qt-interest-old mailing list