Search code examples
python-2.7pyqtpyqt4maya

Maya + PyQt dialog. How to run a single copy of Qt window?


use this simple code for run a window based on qdialog:

import maya.OpenMayaUI as mui
import sip
from PyQt4 import QtGui, QtCore, uic

#----------------------------------------------------------------------
def getMayaWindow():
    ptr = mui.MQtUtil.mainWindow()
    return sip.wrapinstance(long(ptr), QtCore.QObject)

#----------------------------------------------------------------------
form_class, base_class = uic.loadUiType('perforceBrowserWnd.ui')

#----------------------------------------------------------------------
class PerforceWindow(base_class, form_class):
    def __init__(self, parent=getMayaWindow()):
        super(base_class, self).__init__(parent)
        self.setupUi(self)

#----------------------------------------------------------------------
def perforceBrowser2():
    perforceBrowserWnd = PerforceWindow()
    perforceBrowserWnd.show()

perforceBrowser2()

every time you run the function perforceBrowser2() there is a new copy of windows.

how to find whether a window is already running and not to open a new copy of it, and go to the opened window? or just do not give a script to run a second copy of window?

ps. maya2014 + pyqt4 + python2.7


Solution

  • Keep a global reference to the window:

    perforceBrowserWnd = None
    
    def perforceBrowser2():
        global perforceBrowserWnd
        if perforceBrowserWnd is None:
            perforceBrowserWnd = PerforceWindow()
        perforceBrowserWnd.show()