缺少 PyQt QML 错误控制台
PyQt QML error console missing
标题几乎说明了一切。
假设我有这个简单的应用程序:
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 >>>
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 代码将正常工作,但它会启动一个空的 window 而没有任何错误消息。
如何在我的 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 signal and get the details from the errors 方法:
def handleStatusChange(status):
if status == QQuickView.Error:
errors = appLabel.errors()
if errors:
raise Exception(errors[0].description())
appLabel = QQuickView()
appLabel.statusChanged.connect(handleStatusChange)
标题几乎说明了一切。
假设我有这个简单的应用程序:
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 >>>
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 代码将正常工作,但它会启动一个空的 window 而没有任何错误消息。
如何在我的 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 signal and get the details from the errors 方法:
def handleStatusChange(status):
if status == QQuickView.Error:
errors = appLabel.errors()
if errors:
raise Exception(errors[0].description())
appLabel = QQuickView()
appLabel.statusChanged.connect(handleStatusChange)