python - 缺少 PyQt QML 错误控制台

标签 python qt qml anaconda pyqt5

标题几乎说明了一切。

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

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

现在这个例子运行良好。但是假设我在 main.qml 中打错了字,我写的是高度而不是高度。然后 python 代码将正常工作,但它会启动一个空窗口,没有任何错误消息。

我应该怎么做才能在我的 python 控制台中看到来自 .qml 文件的错误?在 6000 行代码中查找拼写错误非常痛苦。

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

最佳答案

如果您只想在控制台上看到错误输出,则无需执行任何操作,因为 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)

关于python - 缺少 PyQt QML 错误控制台,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35384677/

相关文章:

Eclipse PyDev 的 Python 反编译器?

c++ - qml无窗应用中如何设置拖拽区域

qt - 如何在 QML 中使用 Font Awesome

python - Django 1.8 无法加载 css 模板

python - 使用python用分隔符分割字符串,同时忽略引号内的分隔符和转义引号

python - 我想在给定的虚拟函数中更新我的函数 interpolate()

c++ - Mac OS 上的 Qt - 检测停靠菜单上的点击

qt - OpenGL 渲染到 QML 项目

Qt 4.5 - 如何使用模板类型执行排队连接?

c++ - QtQuick 使用 QQuickTextNode