[PyQt] Getting rid of an object

Phil Thompson phil at riverbankcomputing.com
Sat Jun 7 19:32:40 BST 2008


On Saturday 07 June 2008 6:01:01 pm David Klasinc wrote:
> Greetings,
>
> I am facing a rather annoying problem since I have no idea how to get
> rid of a dialog that I created.
>
> For example, in an instance of QMainWindow I opened another dialog with
>
>   self.dialogNew = newDialog (self)
>   self.dialogNew.show ()
>
> newDialog is a subclassed QDialog with UI created in Qt Designer.
>
>
> Now, how do I get rid of that dialog and when will its destructor get
> called?
>
> I implemented __del__ method just too see when it gets called, but it
> turns out that I can execute
>
> del self.dialogNew
>
> and still nothing happens.
>
> My probles goes deeper than this, since I have one QObject class that I
> used to implement timer. Now, I would want to delete certain instance of
> this class because I don't need it. And since del doesn't do anything,
> my timer never stops.
>
> If anyone can shed some light on this, I'd be very happy.

When you give a QObject derived object a parent then you lose ownership of it, 
ie. the C++ dtor isn't called when the corresponding Python object is garbage 
collected. You either need to set the parent to None before removing the last 
reference to the Python object, or don't give it a parent in the first place.

Also, QDialog has special handling for the common use case where you really 
want to specify a parent - but it only works when you use exec_() rather than 
show().

Phil


More information about the PyQt mailing list