How to explicitly delete a QDialog when finished with it?
[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()??
Re: How to explicitly delete a QDialog when finished with it?
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??
I just use obj.deleteLater(). There's also sip.delete(obj) if you really
need something gone immediately, but I almost never use that.
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??