PyQt - 加载多个UI文件

时间:2012-09-04 23:47:38

标签: python qt4 pyqt maya

我有多个ui文件,每个都在Qt Designer中创建。我有一个调用第一个UI的函数(main)。第一个UI上有一个按钮,用于调用第二个UI并关闭第一个UI。

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()

问题是当我运行main()时没有任何反应。另请注意,我正在为Maya创建此脚本并使用PyQt4。

2 个答案:

答案 0 :(得分:1)

我找到了答案,结果我需要为我的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()

答案 1 :(得分:0)

根据您的Maya版本,Qt可能已经或可能尚未作为应用程序运行。

几年前,Autodesk开始将Qt引入他们的软件中,而最新的Maya几乎完全由Qt驱动。

从您可以在没有崩溃的情况下运行代码这一事实判断,我假设QApplication存在于某个已经陷入困境的地方。重要的是要注意,你不能在没有首先创建QApplication的情况下创建任何QtGui组件(好吧,QPaintDevice的 - 所以QWidget,QDialog,QMainWindow等),否则你会崩溃。

您没有看到任何内容这一事实意味着您使用的是使用Qt的Maya版本(并初始化了QApplication)但未运行其事件循环 - 或者存在父母问题。

我首先将你的主要功能切换成这样:

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())

如果这不起作用,那么您需要在maya的事件循环中更新Qt事件循环。我不记得那个语法...但最终你需要使用maya的回调系统来更新Qt事件:

    QtGui.QApplication.instance().processEvents()
相关问题