简体   繁体   English

缺少PyQt QML错误控制台

[英]PyQt QML error console missing

The title says pretty much everything. 标题几乎说明了一切。

Lets say I have this simple application: 可以说我有这个简单的应用程序:

main.py >>> 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 >>> 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. 但是让我说我​​在main.qml中输入一个拼写错误而我写的是heigth而不是height。 Then the python code will work just fine but it will launch an empty window without any error message. 然后python代码将正常工作,但它将启动一个没有任何错误消息的空窗口。

What shall I do to see errors from .qml file in my python console? 如何在我的python控制台中查看.qml文件中的错误? Finding typo in 6000 lines of code is extremely painful. 在6000行代码中查找拼写错误非常痛苦。

I am using PyQt 5.5.1, Anaconda 2.4.1 (Python 3.5.1), Windows 8.1 我使用的是PyQt 5.5.1,Anaconda 2.4.1(Python 3.5.1),Windows 8.1

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. 如果您只想在控制台上看到错误输出,则无需执行任何操作,因为Qt无论如何都会自动执行此操作。 For example, if I change height to heigth in your example, the following message is printed on stderr: 例如,如果我改变heightheigth在你的例子,下面的消息被打印在stderr:

file:///home/foo/test/main.qml:4:17: Cannot assign to non-existent property "heigth" width: 250; file:///home/foo/test/main.qml:4:17:无法分配给不存在的属性“heigth”width:250; heigth: 175 高: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: 如果要在应用程序中引发异常,可以连接到statusChanged信号并从errors方法获取详细信息:

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

    appLabel = QQuickView()
    appLabel.statusChanged.connect(handleStatusChange)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM