I would recommend not putting the delete logic into the close and hide
events of your dialog. This makes your dialog less reusable, as now it
always wants to deleted when hidden and might be surprising to someone
using it.

The way you have shown your example. you are calling the blocking exec_()
which creates a modal window. You can simply call deleteLater() on your
dialog after exec_() returns.

Justin

On Fri, Aug 19, 2016 at 10:56 AM johan Borgström <[email protected]>
wrote:

> Hi,
>
> I have a question regarding the best way to present a custom dialog and
> how to make sure it is properly deleted when it is closed. I use the
> following code to present a custom dialog. This is a simplified dialog.
>
> I am calling deleteLater in the close and hide event to make sure the
> widget is destroyed when it is closed. I tried to use WA_DeleteOnClose but
> had no luck with that. Is there a "better" way to do this? Thankful for
> comments and ideas.
>
> from PySide2 import QtCore, QtGui, QtWidgets
> from shiboken2 import wrapInstance
> from maya import OpenMayaUI as omui
>
> class CustomDialogExample(QtWidgets.QDialog):
>
>     def __init__(self, parent=None):
>         super(CustomDialogExample, self).__init__(parent)
>
>         self.setWindowFlags(QtCore.Qt.FramelessWindowHint |
> QtCore.Qt.Dialog)
>
>         vbox = QtWidgets.QVBoxLayout(self)
>
>         self.comboBox = QtWidgets.QComboBox()
>         self.comboBox.addItems(['AL', 'B', 'C'])
>         vbox.addWidget(self.comboBox)
>
>         vbox.addStretch()
>
>         buttonBox =
> QtWidgets.QDialogButtonBox(QtWidgets.QDialogButtonBox.Ok |
> QtWidgets.QDialogButtonBox.Cancel, QtCore.Qt.Horizontal, self)
>         buttonBox.accepted.connect(self.accept)
>         buttonBox.rejected.connect(self.reject)
>         vbox.addWidget(buttonBox)
>
>     @staticmethod
>     def getSelection(parent=None):
>
>         dialog = CustomDialogExample(parent)
>         #dialog.setAttribute(QtCore.Qt.WA_DeleteOnClose, True)
>         result = dialog.exec_()
>         return (result==QtWidgets.QDialog.Accepted,
> dialog.comboBox.currentText())
>
>     def closeEvent(self, event):
>         print 'closeEvent'
>         self.deleteLater()
>
>     def hideEvent(self, event):
>         print 'hideEvent'
>         self.deleteLater()
>
>
> def showDialog():
>     mayaMainWindow = wrapInstance(long(omui.MQtUtil.mainWindow()),
> QtWidgets.QWidget)
>     accept, data = CustomDialogExample.getSelection(mayaMainWindow)
>     print(accept, data)
>
>
> BR
> Johan
>
>
>
>
> --
> You received this message because you are subscribed to the Google Groups
> "Python Programming for Autodesk Maya" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to [email protected].
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/python_inside_maya/04031d72-8bde-4fe3-813d-55b1ed072d7c%40googlegroups.com
> <https://groups.google.com/d/msgid/python_inside_maya/04031d72-8bde-4fe3-813d-55b1ed072d7c%40googlegroups.com?utm_medium=email&utm_source=footer>
> .
> For more options, visit https://groups.google.com/d/optout.
>

-- 
You received this message because you are subscribed to the Google Groups 
"Python Programming for Autodesk Maya" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To view this discussion on the web visit 
https://groups.google.com/d/msgid/python_inside_maya/CAPGFgA0EmLeQB0%2BVzK1%2B%3Dx7af9nkeLZAzNj7M%2B_%2BcdS60_4u_A%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to