[PyKDE] Properly deleting objects from a QWidgetStack

Phil Thompson phil at riverbankcomputing.co.uk
Sun Jul 27 11:05:01 BST 2003


On Saturday 26 July 2003 9:25 pm, Jacob M. Burbach wrote:
> On Saturday 26 July 2003 03:22 pm, Jacob M. Burbach wrote:
> > What is the proper way to remove and destroy an object from a
> > QWidgetStack?
> >
> > I tried:
> > 	object = widgetStack.widget( objectId )
> > 	widgetStack.removeWidget( object )
> > 	del object
> >
> > However it doesn't seem to call the destructor of that object, causing a
> > memory leak. What is the proper way to do this to make sure the object
> > really gets destroyed?
> >
> >
> > _______________________________________________
> > PyKDE mailing list    PyKDE at mats.imk.fraunhofer.de
> > http://mats.imk.fraunhofer.de/mailman/listinfo/pykde
>
> Update, after doing a quick check, I see I have to call
> QWidgetStack.removeChild to actually decrease the reference count
>
> Example:
> 	object = widgetStack.widget( objectId )
>   	widgetStack.removeWidget( object )
> 	widgerStack.removeChild(object)
> 	print sys.getrefcount(object)
>  	del object
>
> Which says it has 2 references, one would be the object variable, so
> apparently I have an extra reference laying around somewhere...

When you add a widget to a QWidgetStack ownership is transfered to C++ because 
the widget is re-parented and so it's C++ dtor will be called as a 
consequence of the QWidgetStack's dtor being called. When ownership is 
transfered, the reference count is incremented - so that, if ownership is 
transfered back, the original Python object (including it's specific type) is 
returned.

You might expect ownership of the widget to be transfered back to Python when 
you call removeWidget(). It isn't because the widget isn't removed from the 
QWidgetStack's children. Another option, other that removeChild(), would be 
to reparent() the child and give it a new parent of None.

Phil




More information about the PyQt mailing list