PDA

View Full Version : QApplication Instance Error



Nfrancisj
28th April 2016, 19:32
Im having issues implementing my little window into a full Qt Application. Let me explain...
im using NUKE (by the foundry) that is built using PyQt/PySide. Im builing just a quick dialog that pops up and lets you choose stuff.
I've built other dialogs and they work fine.

this current project tell me : "A QApplication instance already exisits"

this is where the problem is, i think:


def ElementBrowserShow():
app = QApplication(sys.argv)
window = MainDialog()
window.show()
sys.exit(app.exec_())

When I omit the 2nd line, i get an error on the 5th line where "app" is not defined. if i also omit the 5th line, the dialog flashes on and then quits.
i read that i can implement app = QtGui.QApplication.instance(), but it doesnt work either. Not sure if im using it correctly.



def ElementBrowserShow():
app = QtGui.QApplication.instance()
window = MainDialog()
window.show()
sys.exit(app.exec_())

d_stranz
28th April 2016, 20:45
"A QApplication instance already exists"

Doesn't that tell you exactly what the problem is? Somewhere in your code (or the foundry, whatever that is), a QApplication instance has already been created. You've posted only a tiny fragment of code which looks like it might be a slot of some sort, and if it is, then it won't be executed unless it has been connected to a signal and that signal has been fired via an event loop running in a QApplication (usually) instance. That fact that it is being called says that is exactly the case.

Nfrancisj
29th April 2016, 06:26
I figured out the issue, and solution. Nuke doesn't allow multiple instances.
I had to omit line 1, 2, and 5.

Not sure exactly why this works...but Nuke had issues calling those lines from inside a function. So instead of calling the function, I just call window.show()

d_stranz
29th April 2016, 22:42
Nuke doesn't allow multiple instances.

No, it is the Qt runtime that doesn't allow multiple QApplication instances. Nuke uses Qt and created the QApplication instance when it started up. If you are adding PyQt code to be used within Nuke, then you cannot create another QApplication instance, but must use the one already created.

Nfrancisj
30th April 2016, 05:36
Yes, thank you! That's what I meant to say, but couldn't find the right words. :D