The difference between QtWidgets.QApplication.instance() and QtWidgets.qApp is that the latter is a static module variable that must be created when the module is first imported. This leads to the following initially obscure behavior:
>>> from PyQt5 import QtWidgets >>> inst = QtWidgets.QApplication.instance() >>> qapp = QtWidgets.qApp >>> (inst, qapp) (None, <PyQt5.QtWidgets.QApplication object at 0x7ff3c8bd3948>)
So, although the QApplication object has not yet been created, the qApp variable still points to the QApplication instance. If the modules were more like classes, so that they could have dynamic properties, it would be possible for qApp work just like QApplication.instance() , and initially return None . But since it is static, it should always return an object of the correct type so that it can later refer to the same main C ++ object as QApplication.instance() .
However, it is important to note that qApp initially just an empty shell:
>>> qapp.objectName() Traceback (most recent call last): File "<stdin>", line 1, in <module> RuntimeError: wrapped C/C++ object of type QApplication has been deleted
After creating QApplication they will point to the same thing:
>>> app = QtWidgets.QApplication([]) >>> app.setObjectName('foo') >>> qapp.objectName() 'foo'
So the reason that (QtWidgets.QApplication.instance() is QtWidgets.qApp) returns False is because the two objects are different python wrappers around the same underlying C ++ object.
It's important to know this point if you ever need to create your own subclass of QApplication , but still want to use qApp :
>>> from PyQt5 import QtWidgets >>> class MyApp(QtWidgets.QApplication): ... def hello(self): print('Hello World') ... >>> myapp = MyApp([]) >>> myapp.hello() Hello World >>> >>> QtWidgets.qApp <PyQt5.QtWidgets.QApplication object at 0x7f5e42f40948> >>> QtWidgets.qApp.hello() Traceback (most recent call last): File "<stdin>", line 1, in <module> AttributeError: 'QApplication' object has no attribute 'hello' >>> >>> inst = QtWidgets.QApplication.instance() >>> inst <__main__.MyApp object at 0x7f5e42f409d8> >>> inst.hello() Hello World
The only way to avoid overwriting the qApp module variable qApp (and, obviously, be sure to do this before it is imported by other modules):
>>> QtWidgets.qApp = myapp >>> QtWidgets.qApp.hello() Hello World