【问题标题】:A QApplication instance already exists一个 QApplication 实例已经存在
【发布时间】:2015-01-24 21:09:13
【问题描述】:

我正在 3Dsmax 2015 上做一些简单的 PySide。

这是我的错误:

python.ExecuteFile "C:\Program Files\Autodesk\3ds Max 2015\scripts\Python\demoUniTest.py"
-- Runtime error:  Line 32  <module>()
  <type 'exceptions.RuntimeError'> A QApplication instance already exists.

这是我的代码:

import sys
from PySide.QtCore import *
from PySide.QtGui import *
from math import *

class Form(QDialog):
def __init__(self,parent=None):
    super(Form,self).__init__(parent)

    self.browser = QTextBrowser()
    self.lineedit = QLineEdit("Type an expression and press Enter")
    self.lineedit.selectAll()

    layout = QVBoxLayout()
    layout.addWidget(self.browser)
    layout.addWidget(self.lineedit)
    self.setLayout(layout)

    self.lineedit.setFocus()

    self.connect(self.lineedit, SIGNAL("returnPressed()"),self.updateUi)
    self.setWindowTitle("Calculate")

def updateUi(self):
    try:
        text = self.lineedit.text()
        self.browser.append("%s = <b>%s</b>" % (text,eval(text)))
    except:
        self.browser.append("<font color=red>%s is invalid</font>" %text)

app = QApplication(sys.argv)

form = Form()

form.show()

app.exec_()

当我在 Pycharm 上使用此代码时,我没有收到任何错误。只有当我在 3Dsmax 2015 Listener 上使用它时才会出现

【问题讨论】:

    标签: python pyside maxscript


    【解决方案1】:

    帮助文件中的直接引用 (Using PySide):

    通常在脚本中使用创建 PySide 应用程序对象 QtGui.QApplication()。但是,在 3ds Max 中,已经有一个 PySide 应用程序正在运行,因此您可以像这样获得该对象的句柄:

    QtGui.QApplication.instance()
    

    【讨论】:

    • 这在 Max 2018 后不再有效,请参阅我的区域帖子 here
    【解决方案2】:

    请注意,这在 3DS Max 2018 和 PySide2 中有所改变。我现在只是自己玩弄它,经过一些修补后我能够让它工作。这是文档的链接,但请注意代码中有一个小错字(至少在撰写本文时):http://help.autodesk.com/view/3DSMAX/2018/ENU/?guid=__developer_what_s_new_in_3ds_max_python_api_what_s_new_in_the_3ds_max_2018_p_html

    如其他答案中所述,您需要使您的 UI 成为 3DS Max 主应用程序的子项。好消息是他们使用GetQMaxMainWindow() 功能为您简化了这一点。像这样使用它:

    from PySide2 import QtWidgets, QtCore, QtGui
    import MaxPlus
    import os
    
    class SampleUI(QtWidgets.QDialog):
        def __init__(self, parent=MaxPlus.GetQMaxMainWindow()):
            super(SampleUI, self).__init__(parent)
            self.initUI()
    
        def initUI(self):
            mainLayout = QtWidgets.QHBoxLayout()
            testBtn = QtWidgets.QPushButton("Test!")
            mainLayout.addWidget(testBtn)
            self.setLayout(mainLayout)
    
    if __name__ == "__main__":
        try:
            ui.close()
        except:
            pass
    
        ui = SampleUI()
        ui.show()
    

    【讨论】:

      【解决方案3】:

      您正在以下行中创建 QApplication 的实例:

      app = QApplication(sys.argv)
      

      之所以会出现该错误,是因为在此之前某处创建了另一个 QApplication 实例(大概在“3Dsmax 2015 侦听器”中的某处),并且您只被允许使用一个。

      见:

      QT documentation on QApplication

      【讨论】:

        猜你喜欢
        • 2010-12-20
        • 1970-01-01
        • 1970-01-01
        • 2023-03-08
        • 2017-07-29
        • 1970-01-01
        • 1970-01-01
        • 2014-03-30
        • 2015-08-09
        相关资源
        最近更新 更多