QApplication instance already exists

I am making some simple PySide at 3Dsmax 2015.

That's my fault:

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.

This is my code:

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

When I use this code in Picharm, you have no errors. It only appears when I use the 3Dsmax 2015 Listener app

+4
source share
3 answers

Direct quote from the help file ( Using PySide ):

Usually one creates a PySide application object in a script using QtGui.QApplication (). However, in 3ds Max there is already a PySide application running, so you get a handle to this object as follows:

QtGui.QApplication.instance()
+9
source

You create a QApplication instance in the line:

app = QApplication(sys.argv)

, QApplication, - (, - "3Dsmax 2015 Listener" ), .

:

QT QApplication

+1

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

, 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):
        self.testBtn = QtWidgets.QPushButton("Test")
        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()
0
source

All Articles