PyQt QML error console missing

2019-07-07 07:18发布

The title says pretty much everything.

Lets say I have this simple application:

main.py >>>

import sys
from PyQt5.QtCore import QUrl
from PyQt5.QtWidgets import QApplication
from PyQt5.QtQuick import QQuickView

# Main Function
if __name__ == '__main__':
    # Create main app
    myApp = QApplication(sys.argv)
    # Create a label and set its properties
    appLabel = QQuickView()
    appLabel.setSource(QUrl('main.qml'))

    # Show the Label
    appLabel.show()

    # Execute the Application and Exit
    myApp.exec_()
    sys.exit()

main.qml >>>

import QtQuick 2.0

Rectangle {
    width: 250; height: 175

    Text {
        id: helloText
        anchors.verticalCenter: parent.verticalCenter
        anchors.horizontalCenter: parent.horizontalCenter
        text: "Hello World!!!\n Traditional first app using PyQt5"
        horizontalAlignment: Text.AlignHCenter
    }
}

Now this example is working fine. But lets say I make a typo in main.qml and I write heigth instead of height. Then the python code will work just fine but it will launch an empty window without any error message.

What shall I do to see errors from .qml file in my python console? Finding typo in 6000 lines of code is extremely painful.

I am using PyQt 5.5.1, Anaconda 2.4.1 (Python 3.5.1), Windows 8.1

1条回答
叼着烟拽天下
2楼-- · 2019-07-07 07:56

If all you want is to see error output on the console, you don't need to do anything, because Qt automatically does that anyway. For example, if I change height to heigth in your example, the following message is printed on stderr:

file:///home/foo/test/main.qml:4:17: Cannot assign to non-existent property "heigth" width: 250; heigth: 175

If you want to raise an exception within your application, you can connect to the statusChanged signal and get the details from the errors method:

    def handleStatusChange(status):
        if status == QQuickView.Error:
            errors = appLabel.errors()
            if errors:
                raise Exception(errors[0].description())

    appLabel = QQuickView()
    appLabel.statusChanged.connect(handleStatusChange)
查看更多
登录 后发表回答