QApplication.instance() returns QCoreApplication instance

QApplication.instance() returns QCoreApplication instance

Anonymous
Not applicable
5,358 Views
7 Replies
Message 1 of 8

QApplication.instance() returns QCoreApplication instance

Anonymous
Not applicable

Hello!

In python, I am trying to get the active QApplication object for Max in order to access top-level information such as the active QScreen, screen geometry, etc.

When I run the tollowing code, I get a QCoreApplication instead of the expected QGuiApplication or QApplication:

QtGui.QGuiApplication.instance()

Is there another method for getting the QApplication?

0 Likes
5,359 Views
7 Replies
Replies (7)
Message 2 of 8

drew_avis
Autodesk
Autodesk

Hi, this is a known limitation, and we will document how to work around it.  One solution is to use shiboken2 (included with PySide2) to cast the QCoreApplication to a QApplication, like this:

>>> from PySide2 import QtWidgets
>>> import shiboken2
>>> app2 = shiboken2.wrapInstance(shiboken2.getCppPointer(QtWidgets.QApplication.instance())[0], QtWidgets.QApplication)
>>> app2
<PySide2.QtWidgets.QApplication object at 0x0000020A13D67F08>

Hope that helps,

Drew



Drew Avis
Content Experience Designer
0 Likes
Message 3 of 8

Anonymous
Not applicable

I will try this out ASAP, I spent a long time searching for exactly this workaround.  Thanks!!

0 Likes
Message 4 of 8

Anonymous
Not applicable

Thank you for this, it's very helpful.

 

I'm curious though. Why does this problem not happen with Autodesk Maya 2018 ? Why does the instance() method return the correct PySide2 type in that application ?

0 Likes
Message 5 of 8

colbey82
Contributor
Contributor

I'm running Max 2020, but I can't import shiboken2, if I run your code I get the following error:

 

Traceback (most recent call last):
ImportError: No module named shiboken2

 

Any thoughts on what's causing this?

0 Likes
Message 6 of 8

drew_avis
Autodesk
Autodesk

Shiboken2 is part of the PySide2 module in 3ds Max 2020.  Can you try this?

from PySide2 import shiboken2

 

Hope that helps,

Drew



Drew Avis
Content Experience Designer
Message 7 of 8

bradley_henke
Enthusiast
Enthusiast

I'm running into this problem on Maya 2020 as well. I've tried using your work around and it seems to work the first time the function is called; however, it seems to stomp on the qApp variable so that any subsequent calls to 

PySide2.QtWidgets.QApplication.instance()

return None.

 

Do you have any other recommendations? Any chance this can patched on Autodesk's end in the same manner as SideFx? (https://www.sidefx.com/changelog/?journal=&categories=&body=&version=18.0&build_0=366&build_1=366&sh...)

Message 8 of 8

jlmunozYTUDK
Community Visitor
Community Visitor

Thanks.

0 Likes