1

众所周知,QtGui是用于GUI程序创建界面的,而QtCore是用于非GUI程序的,实际上是在界面下工作的。但是要获取正在运行的应用程序的实例,我发现我们可以使用 QtCore 和 QtGui 来返回当前正在运行的实例,使用QtCore.QCoreApplicationand QtGui.QApplication

那么使用它们返回的实例有什么区别呢?他们指的是同一件事吗?

4

2 回答 2

2

您可以使用该shiboken模块来检查实际发生的情况:

>>> import shiboken
>>> from PySide import QtCore, QtGui
>>> app = QtGui.QApplication([])
>>> app
<PySide.QtGui.QApplication object at 0x7fc6031c98c8>
>>> print(shiboken.dump(app))
C++ address....... PySide.QtGui.QApplication/0x11446c0
hasOwnership...... 0
containsCppWrapper 1
validCppObject.... 1
wasCreatedByPython 1

>>> print(shiboken.dump(QtGui.QApplication.instance()))
C++ address....... PySide.QtGui.QApplication/0x11446c0
hasOwnership...... 0
containsCppWrapper 1
validCppObject.... 1
wasCreatedByPython 1

>>> print(shiboken.dump(QtCore.QCoreApplication.instance()))
C++ address....... PySide.QtGui.QApplication/0x11446c0
hasOwnership...... 0
containsCppWrapper 1
validCppObject.... 1
wasCreatedByPython 1

>>> print(shiboken.dump(QtGui.qApp))
C++ address....... PySide.QtGui.QApplication/0x11446c0
hasOwnership...... 0
containsCppWrapper 1
validCppObject.... 1
wasCreatedByPython 1

如您所见,每次都引用相同的底层 C++ 对象。如果您创建了一个QCoreApplication,那么QCoreApplication每次也会引用相同的内容。

这里的意图是应该只有一个应用程序对象。但是 Qt 类并不是真正的单例。因此,如果您尝试创建另一个实例,PySide 会选择引发错误:

>>> app2 = QtGui.QApplication([])
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
RuntimeError: A QApplication instance already exists.

但是,在 PyQt 中,没有什么可以阻止您创建多个实例,在 C++ 中也必须如此。据推测,这通常会导致未定义的行为。

sip.dump与 PyQt 一起使用显示了其他一些重要的区别:

# NB: abbreviated output
>>> import sip
>>> from PyQt4 import QtCore, QtGui
>>> app = QtGui.QApplication([])
>>> sip.dump(app)
<PyQt4.QtGui.QApplication object at 0x7fc801a91678>
    Reference count: 3
    Address of wrapped object: 0x25190e0
    Created by: Python
    To be destroyed by: Python
>>> sip.dump(QtCore.QCoreApplication.instance())
<PyQt4.QtGui.QApplication object at 0x7fc801a91678>
    Reference count: 3
    Address of wrapped object: 0x25190e0
    Created by: Python
    To be destroyed by: Python
>>> sip.dump(QtGui.QApplication.instance())
<PyQt4.QtGui.QApplication object at 0x7fc801a91678>
    Reference count: 3
    Address of wrapped object: 0x25190e0
    Created by: Python
    To be destroyed by: Python
>>> sip.dump(QtGui.qApp)
<PyQt4.QtGui.QApplication object at 0x7fc801a91558>
    Reference count: 3
    Address of wrapped object: 0x25190e0
    Created by: C/C++
    To be destroyed by: C/C++

请注意,与 PySide 不同,qApp实例是由 C++ 而不是 Python 创建的,并且 PyQt 包装器也不相同。如果您决定创建自己的 的子类,请记住这一点QApplication,因为qApp会忽略它!如果要qApp引用自己的子类,则需要显式设置它,如下所示:

myapp = MyCustomApplication(sys.argv)
QtGui.qApp = myapp

在 PySide 中,不需要这个小技巧。

于 2016-04-12T18:14:49.997 回答
1

没有区别,因为instance()方法 inQApplication继承自QCoreApplication. 您还可以按如下方式进行演示:

>>> 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>

请注意,无论您使用哪个类访问实例,PyQt 都会正确地对对象进行类型转换。在 C++ 中,您需要自己进行这种类型转换。

于 2016-04-11T23:33:57.597 回答