3

The title says pretty much everything.

Lets say I have this simple application:

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

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.

What shall I do to see errors from .qml file in my python console? Finding typo in 6000 lines of code is extremely painful.

I am using PyQt 5.5.1, Anaconda 2.4.1 (Python 3.5.1), Windows 8.1

Ford O.
  • 1,394
  • 8
  • 25
  • [QQuickView.errors](http://doc.qt.io/qt-5/qquickview.html#errors). – ekhumoro Feb 13 '16 at 21:26
  • Thats nice, but what if I want to print something like division by zero exactly when it happens? – Ford O. Feb 14 '16 at 08:08
  • This question was well answered by @eyllanesc here : [PySide How to see QML errors in python console?](https://stackoverflow.com/a/53992028/12587140) – Mohammed Dec 23 '19 at 21:06
  • This question was well answered by @eyllanesc here : [PySide How to see QML errors in python console?](https://stackoverflow.com/a/53992028/12587140) – Mohammed Dec 23 '19 at 21:09

1 Answers1

1

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:

file:///home/foo/test/main.qml:4:17: Cannot assign to non-existent property "heigth" width: 250; heigth: 175

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)
ekhumoro
  • 115,249
  • 20
  • 229
  • 336
  • 1
    “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.” It does not for me, I actually have to print errors manually. – Gallaecio Oct 16 '16 at 10:15
  • @Gallaecio. The example still works exactly the same for me using Qt-5.7.0 and PyQt-5.7. – ekhumoro Oct 16 '16 at 16:56
  • I am not saying the example does not work, I am saying that if you want just the error output and not to raise an exception, you cannot just do nothing, as the quoted text suggests. You have to use code like the one in your example and print the error messages yourself. At least that is what I have to do, and I am using the same version of Qt (and Python 3, not sure if that makes a difference). – Gallaecio Oct 16 '16 at 17:03
  • @Gallaecio. Well, I didn't claim you said it didn't work - I just said it worked *the same* for me (i.e. nothing's changed in the meantime). That is, if I run the OPs original example, and just change "height" to "heigth", Qt will print that message on stderr. There's no need for a `statusChanged` handler, or any other additional code. If things don't work that way on your system, I would say it's not set up properly (well, not for development work, anyway). – ekhumoro Oct 16 '16 at 17:28
  • @Gallaecio. PS: possibly your Qt installation doesn't have qml debugging enabled. Do you ever see a message like this: "QML debugging is enabled. Only use this in a safe environment"? – ekhumoro Oct 16 '16 at 17:33
  • I don’t see that message, no; that must be the reason. – Gallaecio Oct 16 '16 at 18:01