问题描述
标题几乎说明了一切.
让我说我有这个简单的应用程序:
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:
如果您想在应用程序中引发异常,则可以连接到 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错误控制台丢失的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!