[prev in list] [next in list] [prev in thread] [next in thread] 

List:       pykde
Subject:    Re: [PyKDE] Issue with QDialog and lifetime
From:       Phil Thompson <phil () riverbankcomputing ! co ! uk>
Date:       2005-07-25 18:26:01
Message-ID: 200507251926.01976.phil () riverbankcomputing ! co ! uk
[Download RAW message or body]

On Thursday 07 July 2005 12:27 pm, Giovanni Bajo wrote:
> Phil Thompson <phil@riverbankcomputing.co.uk> wrote:
> >> The problem is that the idiomatic way of using modal dialogs in C++ is
> >> to create local variables, which gets destroyed when their scope
> >> finishes (when
> >> the function exits). Moreover, the dialogs have to be children of
>
> whatever
>
> >> widgets they are modal over (e.g. the main window).
> >>
> >> This has no direct translations in PyQt. If you do:
> >>
> >> def slotWhatever(self):
> >>    dlg = MyModalDialog(self)
> >>    if dlg.exec_loop() == QDialog.Accepted:
> >>        return dlg.data()
> >>    return None
> >>
> >> you are leaking "dlg" because its lifetime is bound to its parent. The
> >> current common workaround is something like:
> >>
> >> def slotWhatever(self):
> >>    dlg = MyModalDialog(self)
> >>    try:
> >>        if dlg.exec_loop() == QDialog.Accepted:
> >>            return dlg.data()
> >>        return None
> >>    finally:
> >>        dlg.deleteLater()
> >>
> >> which is ugly and error-prone (you have to remember to do it).
> >>
> >> James proposed this solution:
> >>
> >> def slotWhatever(self):
> >>    dlg = MyModalDialog(self)
> >>    sip.transfer(dlg, 0)
> >>    if dlg.exec_loop() == QDialog.Accepted:
> >>        return dlg.data()
> >>    return None
> >>
> >> and then proposed to hardcode the transfer() call within the %MethodCode
> >> for
> >> QDialog.exec_loop. I'll remember you that exec_loop is called only for
> >> modal
> >> dialogs, while for modeless dialogs show() is called.

Ok, this is in tonight's PyQt snapshot.

Phil

_______________________________________________
PyKDE mailing list    PyKDE@mats.imk.fraunhofer.de
http://mats.imk.fraunhofer.de/mailman/listinfo/pykde
[prev in list] [next in list] [prev in thread] [next in thread] 

Configure | About | News | Add a list | Sponsored by KoreLogic