A QApplication instance already exists

前端 未结 3 1933
死守一世寂寞
死守一世寂寞 2021-01-03 00:33

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

This is my error:

python.ExecuteFile \"C:\\Program Files\\Autodesk\\3ds Max 2015\\scripts\\Python\\dem         


        
相关标签:
3条回答
  • 2021-01-03 00:54

    You're creating an instance of QApplication in the line:

    app = QApplication(sys.argv)
    

    And getting that error because there's another instance of QApplication created somewhere before that (presumably somewhere in "3Dsmax 2015 Listener") and you're only allowed one.

    See:

    QT documentation on QApplication

    0 讨论(0)
  • 2021-01-03 00:59

    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()
    
    0 讨论(0)
  • 2021-01-03 01:10

    As a note this has changed somewhat in 3DS Max 2018 and PySide2. I'm just playing around with it myself right now and I was able to get it working after a bit of tinkering. Here's a link to the documentation, though be warned that there is a small typo in the code (at least at the time of writing): 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

    As mentioned in other answers you need to make your UI a child of the main 3DS Max application. The good news is that they have simplified this a bit for you with the function GetQMaxMainWindow(). Use it like this:

    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()
    
    0 讨论(0)
提交回复
热议问题