PyQt QML错误控制台丢失

2024-09-29 23:25:56 发布

您现在位置:Python中文网/ 问答频道 /正文

书名说明了一切。在

假设我有一个简单的应用程序:

在主.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()

在主.qml&>>>

^{pr2}$

现在这个例子运行得很好。但假设我错了主.qml我写身高而不是身高。然后python代码就可以正常工作了,但是它将启动一个空窗口,没有任何错误消息。在

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

我使用的是PyQt 5.5.1、Anaconda 2.4.1(python3.5.1)、windows8.1


Tags: andthe代码fromimportmaincreatesys
1条回答
网友
1楼 · 发布于 2024-09-29 23:25:56

如果您只想在控制台上看到错误输出,那么您不需要做任何事情,因为Qt无论如何都会自动执行这些操作。例如,如果我在您的示例中将height更改为heigth,那么以下消息将打印在stderr上:

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

如果要在应用程序中引发异常,可以连接到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)

相关问题 更多 >

    热门问题