Closing qDialog (if exists) in pySide

Hi,

I created a UI in designer, converted it to python using pyside-uic and then following a tutorial I did this:

from PySide import QtCore, QtGui
import mirroratorUI as customUI
#import mirroratorCore as mirroratorCore
from shiboken import wrapInstance
import maya.OpenMayaUI as omui
reload(customUI)

def maya_main_window():
    main_window_ptr = omui.MQtUtil.mainWindow()
    return wrapInstance(long(main_window_ptr), QtGui.QWidget)

class ControlMainWindow(QtGui.QDialog):

    def __init__(self, parent=None):

        super(ControlMainWindow, self).__init__(parent)
        self.setWindowFlags(QtCore.Qt.Tool)
        self.ui =  customUI.Ui_MainWindow()
        self.ui.setupUi(self)

myWin = ControlMainWindow(parent=maya_main_window())
myWin.show()

The UI will be used into Maya, and it appears, but if I open it, for example, 3 times, I will have 3 dialgos. I remember there is a way to check if the dialog already exists, and if it does, deleting it. I found some info on google but I either didn’t understand how to use those info or they were not fit to my case…

Many thanks for your help,
Daniele

So your best case it to keep a reference to the dialog somewhere, and check to see if it already exists.

So this should work in the script editor, and at the module level in an imported file. This won’t work if you wrap it in a function though, as the function namespace isn’t retained between executions.
And is fairly similar to the usual, if window exists, delete window idiom.


from PySide import QtCore, QtGui
from shiboken import wrapInstance
import maya.OpenMayaUI as omui


def maya_main_window():
    main_window_ptr = omui.MQtUtil.mainWindow()
    return wrapInstance(long(main_window_ptr), QtGui.QWidget)


class ControlMainWindow(QtGui.QDialog):

    def __init__(self, parent=None):
        super(ControlMainWindow, self).__init__(parent)
        self.setWindowFlags(QtCore.Qt.Tool)

try:
    myWin.deleteLater()
except NameError as e:
    pass

myWin = ControlMainWindow(parent=maya_main_window())
myWin.show()



Usually the better option is to keep a reference to your dialog somewhere, say an instance variable on a tool, and just check that to see if the dialog is already initialized and visible. Though that can get tricky if you’re reloading the tool each time you launch it.

Hi,

Thanks for your quick reply! I am a bit confused, in the code you wrote it is not importing the ui, is it?

Thanks,
Daniele

No, I couldn’t test it with your ui, you’d just add those lines back in.
I went with the most minimal example is all.

[QUOTE=R.White;31171]No, I couldn’t test it with your ui, you’d just add those lines back in.
I went with the most minimal example is all.[/QUOTE]

Everything works now! thank you!