Skip to content Skip to sidebar Skip to footer

Pyside How To See Qml Errors In Python Console?

I have the following code: if __name__ == '__main__': os.environ['QT_QUICK_CONTROLS_STYLE'] = 'Material' app = QGuiApplication(sys.argv) engine = QQmlApplicationEngine(

Solution 1:

If you want to know the error message when using QQmlApplicationEngine you should use the warnings signal but it does not seem to work, so a workaround is to use qInstallMessageHandler to get the messages that Qt gives.

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_())

Post a Comment for "Pyside How To See Qml Errors In Python Console?"