PyQt QML错误控制台丢失 [英] PyQt QML error console missing

查看:169
本文介绍了PyQt QML错误控制台丢失的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

标题几乎说明了一切.

让我说我有这个简单的应用程序:

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
    }
}

现在,此示例运行正常.但是可以说我在main.qml中打错了字,我写的是heigth而不是height.然后,python代码将正常工作,但它将启动一个空窗口,而没有任何错误消息.

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.

在python控制台中如何查看.qml文件中的错误?在6000行代码中查找拼写错误是非常痛苦的.

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

我正在使用PyQt 5.5.1,Anaconda 2.4.1(Python 3.5.1),Windows 8.1

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

推荐答案

如果只想在控制台上看到错误输出,则无需执行任何操作,因为Qt会自动执行此操作.例如,如果在您的示例中将height更改为heigth,则会在stderr上打印以下消息:

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:无法分配给 不存在的属性高度"宽度:250;高度:175

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

如果您想在应用程序中引发异常,则可以连接到 statusChanged 信号,并从错误方法获取详细信息:

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)

这篇关于PyQt QML错误控制台丢失的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆