Cur*_*984 5 python qml pyside2
我有以下代码:
if __name__ == '__main__':
os.environ["QT_QUICK_CONTROLS_STYLE"] = "Material"
app = QGuiApplication(sys.argv)
engine = QQmlApplicationEngine()
engine.load('./QML/main.qml')
if not engine.rootObjects():
sys.exit(-1)
sys.exit(app.exec_())
Run Code Online (Sandbox Code Playgroud)
正如您所看到的,如果“engine.load”失败,我只会看到“-1”退出代码,而不会详细说明失败的原因以及发生的错误。如何在 python 控制台中打印 QML 错误?
QQuickView
当使用而不是 QQmlApplicationEngine时,有一个解决方法,并在这篇文章中进行了描述,但是,我想知道 QQmlApplicationEngine 是否可以实现类似的功能?
如果您想知道使用 QQmlApplicationEngine 时的错误消息,您应该使用信号warnings
,但它似乎不起作用,因此解决方法是使用qInstallMessageHandler
获取 Qt 给出的消息。
import os
import sys
from PySide2 import QtCore, QtGui, QtQml
def qt_message_handler(mode, context, message):
if mode == QtCore.QtInfoMsg:
mode = 'Info'
elif mode == QtCore.QtWarningMsg:
mode = 'Warning'
elif mode == QtCore.QtCriticalMsg:
mode = 'critical'
elif mode == QtCore.QtFatalMsg:
mode = 'fatal'
else:
mode = 'Debug'
print("%s: %s (%s:%d, %s)" % (mode, message, context.file, context.line, context.file))
if __name__ == '__main__':
os.environ["QT_QUICK_CONTROLS_STYLE"] = "Material"
QtCore.qInstallMessageHandler(qt_message_handler)
app = QtGui.QGuiApplication(sys.argv)
engine = QtQml.QQmlApplicationEngine()
qml_filename = os.path.join(os.path.dirname(__file__), 'QML/main.qml')
engine.load(QtCore.QUrl.fromLocalFile(qml_filename))
if not engine.rootObjects():
sys.exit(-1)
sys.exit(app.exec_())
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
2036 次 |
最近记录: |