How to explicitly delete a QDialog when finished with it?

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
3 messages Options
Reply | Threaded
Open this post in threaded view
|

How to explicitly delete a QDialog when finished with it?

J Barchan
[I'm sorry if this has been asked before, but I have Googled and can't find it.]

In PyQt 5, how are you supposed to actually delete a (dynamically-created) QDialog once you are done with it, if you cannot create it with Qt.WA_DeleteOnClose?

Existing code creates many (modal) dialogs repeatedly, usually calling exec().  Some of the calls deliberately do not pass WA_DeleteOnClose (e.g. I think some places want to access stuff in the dialog after the user has exited it).

I get the same dialogs repeatedly created.  Investigating via QApplication.allWidgets(), I see:
  • If a parent was specified, it does get deleted, but not till the parent gets deleted, which is too late (space-wise).
  • If parent was None, I'm not even sure when it does get deleted.

Either way, if I do not specify WA_DeleteOnClose I want to delete it explicitly when I'm done reading its values or whatever.  In C++ I'd call delete explicitly.  What should I do from PyQt/Python?  del??  .deleteLater()??


--
Kindest,
Jonathan

_______________________________________________
PyQt mailing list    [hidden email]
https://www.riverbankcomputing.com/mailman/listinfo/pyqt
Reply | Threaded
Open this post in threaded view
|

Re: How to explicitly delete a QDialog when finished with it?

Florian Bruhin
Hey,

On Thu, Jul 12, 2018 at 11:07:41AM +0100, J Barchan wrote:
> Either way, if I do *not* specify WA_DeleteOnClose I want to delete it
> explicitly when I'm done reading its values or whatever.  In C++ I'd call
> delete explicitly.  What should I do from PyQt/Python?  del??
> .deleteLater()??

I just use obj.deleteLater(). There's also sip.delete(obj) if you really
need something gone immediately, but I almost never use that.

Florian

--
https://www.qutebrowser.org | [hidden email] (Mail/XMPP)
   GPG: 916E B0C8 FD55 A072 | https://the-compiler.org/pubkey.asc
         I love long mails! | https://email.is-not-s.ms/

_______________________________________________
PyQt mailing list    [hidden email]
https://www.riverbankcomputing.com/mailman/listinfo/pyqt

signature.asc (849 bytes) Download Attachment
Reply | Threaded
Open this post in threaded view
|

Re: How to explicitly delete a QDialog when finished with it?

J Barchan


On 12 July 2018 at 11:47, Florian Bruhin <[hidden email]> wrote:
Hey,

On Thu, Jul 12, 2018 at 11:07:41AM +0100, J Barchan wrote:
> Either way, if I do *not* specify
​​
WA_DeleteOnClose I want to delete it
> explicitly when I'm done reading its values or whatever.  In C++ I'd call
> delete explicitly.  What should I do from PyQt/Python?  del??
> .deleteLater()??

I just use obj.deleteLater(). There's also sip.delete(obj) if you really
need something gone immediately, but I almost never use that.

Florian

--
https://www.qutebrowser.org | [hidden email] (Mail/XMPP)
   GPG: 916E B0C8 FD55 A072 | https://the-compiler.org/pubkey.asc
         I love long mails! | https://email.is-not-s.ms/

​Thanks, Florian, I'll give deleteLater() a go and test.

​Let me ask you one other question.  I have a lot of these to deal with.  I suspect that some of those which do not presently pass
WA_DeleteOnClose to the constructor do not actually access the dialog after exec()All my dialogs are derived from my own base class.  For simplicity/clarity, I'd like to make my base class unconditionally call super().__init__(parent, Qt.WA_DeleteOnClose).  Then those callers which do not want auto-delete can explicitly go self.setWindowFlags(self.windowFlags() & ~Qt.WA_DeleteOnClose) immediately after construction.  I think this will give easiest default behaviour for my code.  The question is: is it OK to switch off WA_DeleteOnClose after construction, or do you think there is something which needs to know about this flag at construction-time only, and afterward is too late to switch off??



--
Kindest,
Jonathan

_______________________________________________
PyQt mailing list    [hidden email]
https://www.riverbankcomputing.com/mailman/listinfo/pyqt