funxer
funxer

Reputation: 43

PyQt - Loading multiple UI files

I have multiple ui files, each created in Qt Designer. I have a function (main) that calls the first UI. There is a button on this first UI that calls the second UI and closes the first one.

from PyQt4 import QtGui,QtCore, uic

uifile_1 = '/Users/Shared/Autodesk/maya/scripts/python/Intro_UI.ui'
form_1, base_1 = uic.loadUiType(uifile_1)

uifile_2 = '/Users/Shared/Autodesk/maya/scripts/python/objtemplate_tuner.ui'
form_2, base_2 = uic.loadUiType(uifile_2)

class CreateUI_2(base_2, form_2):
    def __init__(self):
        super(base_2,self).__init__()
        self.setupUi(self)

class CreateUI_1(base_1, form_1):
    def __init__(self):
        super(base_1,self).__init__()
        self.setupUi(self)
        self.Establish_Connections()

    def Do_ButtonPress(self):        
        UI_2=CreateUI_2()
        UI_2.show()
        self.close()
    def Establish_Connections(self):
        QtCore.QObject.connect(self.noncharactermeshes_Button, QtCore.SIGNAL("clicked()"),self.Do_ButtonPress)      

def main():       
    UI_1 = CreateUI_1()
    UI_1.show()

main()

The problem is that when I run main() nothing happens. Also note I'm creating this script for Maya and using PyQt4.

Upvotes: 1

Views: 3314

Answers (2)

funxer
funxer

Reputation: 43

I found an answer, turns out I needed to use a global variable for my ui.

    def Do_ButtonPress(self):
        global UI_2
        UI_2=CreateUI_2()
        UI_2.show()

...

def main():
    global UI_1
    UI_1 = CreateUI_1()
    UI_1.show()

Upvotes: 1

Eric Hulser
Eric Hulser

Reputation: 4022

Depending on your version of Maya, Qt may or may not already be running as an application.

Autodesk started introducing Qt into their software a few years ago, and the latest Maya is almost entirely Qt driven.

Judging from the fact that you can run your code without a crash, I'm assuming that a QApplication exists somewhere already under the hood. It is important to note that you cannot create any QtGui components (well, QPaintDevice's - so QWidget, QDialog, QMainWindow, etc.) without first creating a QApplication, or you'll get a crash.

The fact that you aren't seeing anything means you're either in a Maya version that uses Qt (and initialized a QApplication) but does not run its event loop - or there is a parenting issue.

I'd switch your main function to look like this first:

def main():
    app = None
    # there needs to be 1 and only 1 application instance
    if ( not QtGui.QApplication.instance() ):
        app = QtGui.QApplication([])

    UI_1 = CreateUI_1()
    UI_1.show()

    # see if you can parent to a window (maya)
    UI_1.setParent(QtGui.QApplication.activeWindow())

If that does not work, then you will need to update the Qt event loop within maya's event loop. I don't remember the syntax for that...but ultimately you will need to use maya's callback system to update the Qt events:

    QtGui.QApplication.instance().processEvents()

Upvotes: 0

Related Questions