[PyKDE] Issue with QDialog and lifetime

James Emerton ephelon at gmail.com
Thu Jun 23 19:45:57 BST 2005


On 6/23/05, Giovanni Bajo <rasky at develer.com> wrote:

> > Does anyone see a potential problem with exec_loop() transferring
> > ownership of the dialog instance back to Python?
> 
> Well, which C++ API would you intend to call to achieve this? We still need the
> QDialog to keep its parent widget because it is the widget on which the dialog
> is modal.

Well, the parent-child relationship still exists.  You would end up
with a bad reference if the modal dialog's parent were destroyed.  I
can't think of any correct code that would do this.  (And doing this
in C++ would cause a different set of nasty errors.)

The strategy is to tie the lifetime of the modal dialog to the Python
reference.  The ~QObject() will still clean up the parent's child
object list.  This should make the behavior equivalent to the
idiomatic C++ code.

You should be able to test this with sip.transfer( dlg, 0 ) just
before dlg.exec_loop().  Ultimately, a sipTransfer() should be added
to the %MethodCode for exec_loop().


Of course, none of this addresses the potential exeception from
MyDialog.__init__()  To do that, you would need to handle that case
explicitly.  (This is not unprecidented in C++.  If a ctor throws the
dtor is not called and the object must be manually cleaned up.)

I suppose the other option is to contaminate the public API of
QDialog.  There IS already an obsolete bool parameter, 'modal' to the
QDialog ctor that could be co-opted to mean 'don't transfer
ownership'.

James




More information about the PyQt mailing list