2016-02-13 69 views
3

標題說幾乎所有的東西。PyQt QML錯誤控制檯丟失

可以說我有這個簡單的應用:

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中犯了一個錯字,並且我寫了heigth而不是height。然後python代碼將工作得很好,但它會啓動一個沒有任何錯誤信息的空窗口。

我該怎麼做才能在我的python控制檯中查看.qml文件中的錯誤?在6000行代碼中查找錯字是非常痛苦的。

我使用的PyQt 5.5.1,2.4.1蟒蛇(Python的3.5.1版本),Windows 8.1

+0

[QQuickView.errors(HTTP: //doc.qt.io/qt-5/qquickview.html#errors)。 – ekhumoro

+0

多數民衆贊成在很好,但如果我想打印像零分的東西,恰好發生時呢? –

回答

0

如果你想要的是看到在控制檯上的錯誤輸出,你不需要做任何事,因爲無論如何,Qt都會自動執行此操作。例如,如果我在你的改變。例如heightheigth,下面的消息被打印在標準錯誤:

文件:///home/foo/test/main.qml:4:17:不能分配給 不存在的財產「heigth」寬度: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) 
+0

「如果你只是想在控制檯上看到錯誤輸出,你不需要做任何事情,因爲Qt自動地這樣做。」它不適合我,我實際上不得不手動打印錯誤。 – Gallaecio

+0

@Gallaecio。對於使用Qt-5.7.0和PyQt-5.7的例子,這個例子仍然是一樣的。 – ekhumoro

+0

我不是說這個例子不起作用,我說如果你只想輸出錯誤而不是引發異常,那麼你不能像引用的文本所暗示的那樣不做任何事情。您必須使用類似於您示例中的代碼並自行打印錯誤消息。至少這是我必須做的,而且我使用的是相同版本的Qt(和Python 3,不知道這是否有所不同)。 – Gallaecio