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.
For more options, visit https://groups.google.com/d/optout.

Reply via email to