标题几乎说明了一切。
假设我有这个简单的应用程序:
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()
主.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 中犯了一个拼写错误,我写的是 height 而不是 height。然后 python 代码将正常工作,但它将启动一个空窗口,没有任何错误消息。
我该怎么做才能在 python 控制台中看到 .qml 文件中的错误?在 6000 行代码中查找拼写错误是极其痛苦的。
我正在使用 PyQt 5.5.1、Anaconda 2.4.1 (Python 3.5.1)、Windows 8.1
如果您只想在控制台上看到错误输出,则无需执行任何操作,因为 Qt 会自动执行此操作。例如,如果我改变height
to heigth
在您的示例中,以下消息打印在 stderr 上:
file:///home/foo/test/main.qml:4:17:无法分配给不存在的属性“height”宽度:250;身高:175
如果您想在应用程序中引发异常,您可以连接到状态已更改 http://doc.qt.io/qt-5/qquickview.html#statusChanged发出信号并从中获取详细信息errors http://doc.qt.io/qt-5/qquickview.html#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)
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)