Custom dialog

77 views
Skip to first unread message

johan Borgström

unread,
Aug 18, 2016, 6:56:47 PM8/18/16
to Python Programming for Autodesk Maya
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




Justin Israel

unread,
Aug 18, 2016, 10:46:50 PM8/18/16
to python_in...@googlegroups.com
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

--
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 python_inside_m...@googlegroups.com.
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.

johan Borgström

unread,
Aug 19, 2016, 3:29:26 PM8/19/16
to Python Programming for Autodesk Maya
Hi Justin,

Thanks for your input. I agree that would make it less reusable, deleteLater seems to do the trick.

cheers, Johan
Justin

To unsubscribe from this group and stop receiving emails from it, send an email to python_inside_maya+unsub...@googlegroups.com.
Reply all
Reply to author
Forward
0 new messages