pythonqtpysideqapplicationqcoreapplication

Difference between QtGui.QApplication and QtCore.QCoreApplication


What is known about QtGui is that it is used for GUI programs to create interfaces, and QtCore is for non-GUI programs and actually works under the interface. But to get the instance of the running application, I found that we can use either QtCore and QtGui to return the current running instance using QtCore.QCoreApplication and QtGui.QApplication.

So what is the difference between the instance returned using them? Are they referring to the same thing?


Solution

  • There is no difference because the instance() method in QApplication is inherited from QCoreApplication. You can also demonstrate this as follows:

    >>> from PyQt4.QtCore import QCoreApplication
    >>> from PyQt4.QtGui import QApplication
    >>> a = QApplication([])
    >>> a
    <PyQt4.QtGui.QApplication object at 0x02A75620>
    >>> QApplication.instance()
    <PyQt4.QtGui.QApplication object at 0x02A75620>
    >>> QCoreApplication.instance()
    <PyQt4.QtGui.QApplication object at 0x02A75620>
    
    >>> b = QCoreApplication([])
    >>> b
    <PyQt4.QtCore.QCoreApplication object at 0x02A75670>
    >>> QCoreApplication.instance()
    <PyQt4.QtCore.QCoreApplication object at 0x02A75670>
    >>> QApplication.instance()
    <PyQt4.QtCore.QCoreApplication object at 0x02A75670>
    

    Note that PyQt is correctly typecasting the object regardless of which class you use to access the instance. In C++, you would need to do this typecasting yourself.