如何在 PySide2 中检查 gui 与命令行模式?

How to check for gui vs command line mode in PySide2?

在我的 PySide2 代码中,我需要检查程序是否在 gui 模式或命令行中 运行。在 PySide 中,我曾经这样做过:

if QApplication.type() == QApplication.Type.GuiClient:
    # do stuff

但是,根据 http://doc.qt.io/qt-5/sourcebreaks.html

QCoreApplication::Type and QApplication::type() are removed. These Qt 3 legacy application types did not match the application types available in Qt 5. Use qobject_cast instead to dynamically find out the exact application type.

我的 PySide 程序 运行 在主机应用程序中运行,所以我没有自己创建 QCoreApplication 实例,我只需要检查主机应用程序是否处于批处理模式(非 GUI)避免一些问题。

我现在的问题是如何使用 qobject_cast 在 PySide2 中实现相同的目的? 文档中的 C++ examples 并没有告诉我如何使用 Python 来解决这个问题。

有人可以提供示例或建议其他方法来实现同样的目标吗?

最后我需要做的是:

isinstance(QtWidgets.QApplication.instance(), QtWidgets.QApplication)

which returns 在 GUI 模式下为真,在命令 line/batch 模式下为假。 感谢 ekhumoro 为我指明了正确的方向!