A QApplication instance already exists

Minh Tuan Nguyen picture Minh Tuan Nguyen · Nov 26, 2014 · Viewed 11.4k times · Source

I'm doing some simple PySide on 3Dsmax 2015.

This is my error:

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 on Picharm dont have any errors. It only appear when I use on 3Dsmax 2015 Listener

Answer

Swordslayer picture Swordslayer · Nov 26, 2014

Direct citation from the helpfile (Using PySide):

Normally 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 for that object like this:

QtGui.QApplication.instance()