[PySide] QObject.destroyed() is not emitted

Stephan Deibel sdeibel at wingware.com
Wed Nov 28 15:33:23 CET 2012

Alexey Vihorev wrote:
> Got problems with the following code:
> from PySide import QtCore
> def onDestroy(*args):
> print('destroyed')
> obj = QtCore.QObject()
> obj.destroyed.connect(onDestroy)
> The onDestroy() method is not called, unless del(obj) is called 
> explicitly. In PyQt4 it is called without del(obj). Is that a bug or 
> intended behavior? I’m using Qt 4.8.2, PySide 1.1.2 32bit, Windows 7-64

Since no one responded I'll take a stab at this:

I suspect the life cycle of the QObject under PySide is a little 
different and the instance will be deleted later during garbage 
collection and not immediately when it goes out of scope. I'm actually 
not sure why that is happening in PyQt (seems slightly surprising).

In general if you want to make sure an instance is deleted at a 
particular moment in time, you need to delete it explicitly. By that, I 
mean calling some sort of delete method (in PySide I think it's 
shiboken.delete(obj) and not just "del obj". The latter just deletes 
your reference to the instance but doesn't destroy the instance until 
all other references are gone and it is garbage collected. Of course 
calling shiboken.delete(obj) will be a problem if you still have other 
references and try to use them!

There is also obj.deleteLater() which marks an object for deletion but 
it's not deleted until the event loop is reached again. Depending on 
what you're doing this may be a safer way to delete it.

At first I thought this might be a refcount bug in PySide. However, I'm 
thinking not since "del obj" just deletes your reference to it and if 
there were extra references hanging around (either on purpose or as a 
result of a refcount bug) then it would not be deleting the instance at 
all even after "del obj".

I don't understand PySide internals that well so it's possible I'm 
missing some subtlety here. I'm going more on my knowledge of Python in 

- Stephan

More information about the PySide mailing list