[PyKDE] Top-level windows and ownership

Andreas Gerstlauer gerstl at ics.uci.edu
Sun Nov 4 20:53:13 GMT 2001


Hi!

I want to create an application that has multiple independent
top-level windows. I have a problem with ownership (reference
counting) in PyQt in that case.

In C++ I can do something like this (right out of Qt docu):

void createNewTopLevel(void)
{
   // Create a window which looks after its own existence.
   QMainWindow *w = new QMainWindow(0 /* no parent!! */, "test",
                                                Qt::WDestructiveClose);
   w->show();
}

and then forget about it and all the pointers I have to it. Qt will take
care of deleting the window when it is closed.

In PyQt I don't know how to do this. If I pass a parent (usually the main
widget) into the constructor PyQt will transfer owenership of the window 
to Qt (i.e. increment the ref count) and everything is fine. 

However, if I don't want that (windows should be independent, e.g. not 
minimized when the main widget is minimized) I have to keep a reference
to all top-level windows around in Python (and then I will have to manage
those references, i.e. track when windows are closed to delete the
corresponding reference etc. - not nice). Otherwise, as soon as the
reference is gone and the ref count drops to zero, PyQt will call the C++
destructor since Python and not C++ has ownership of the window. Boom,
window gone.

Any ideas anybody? Is there any way I can explicitly transfer ownership
to the C++ side?

Andreas




More information about the PyQt mailing list