5

I have the following code:

if __name__ == '__main__':
    os.environ["QT_QUICK_CONTROLS_STYLE"] = "Material"
    app = QGuiApplication(sys.argv)
    engine = QQmlApplicationEngine()

    engine.load('./QML/main.qml')

    if not engine.rootObjects():
        sys.exit(-1)

    sys.exit(app.exec_())

As you can see, if `engine.load' fails all I'll see is a '-1' exit code, without any elaboration on why it failed and what the error happened. How can I print the QML error in the python console?

There was a walkaround for this with when using QQuickView instead of QQmlApplicationEngine and is described in this post, however, I wonder if the something similar can be achieved for QQmlApplicationEngine?

eyllanesc
  • 235,170
  • 19
  • 170
  • 241
Curtwagner1984
  • 1,908
  • 4
  • 30
  • 48
  • relative https://stackoverflow.com/questions/45805076/qqmlapplicationengine-not-emitting-warnings-signal – eyllanesc Dec 31 '18 at 22:29
  • @eyllanesc I tried to set `QQmlEngine::setOutputWarningsToStandardError(bool enabled)` to true, and to connect to the `warnings` signal ... No luck so far... – Curtwagner1984 Dec 31 '18 at 22:41
  • Exactly, you have reviewed https://stackoverflow.com/questions/45805076/qqmlapplicationengine-not-emitting-warnings-signal#comment78589856_45813367: this is probably a bug, I recommend you report it. – eyllanesc Dec 31 '18 at 22:43

1 Answers1

4

If you want to know the error message when using QQmlApplicationEngine you should use the warnings signal but it does not seem to work, so a workaround is to use qInstallMessageHandler to get the messages that Qt gives.

import os
import sys
from PySide2 import QtCore, QtGui, QtQml

def qt_message_handler(mode, context, message):
    if mode == QtCore.QtInfoMsg:
        mode = 'Info'
    elif mode == QtCore.QtWarningMsg:
        mode = 'Warning'
    elif mode == QtCore.QtCriticalMsg:
        mode = 'critical'
    elif mode == QtCore.QtFatalMsg:
        mode = 'fatal'
    else:
        mode = 'Debug'
    print("%s: %s (%s:%d, %s)" % (mode, message, context.file, context.line, context.file))


if __name__ == '__main__':
    os.environ["QT_QUICK_CONTROLS_STYLE"] = "Material"
    QtCore.qInstallMessageHandler(qt_message_handler)
    app = QtGui.QGuiApplication(sys.argv)
    engine = QtQml.QQmlApplicationEngine()
    qml_filename = os.path.join(os.path.dirname(__file__), 'QML/main.qml')
    engine.load(QtCore.QUrl.fromLocalFile(qml_filename))
    if not engine.rootObjects():
        sys.exit(-1)
    sys.exit(app.exec_())
eyllanesc
  • 235,170
  • 19
  • 170
  • 241
  • This seems to work when there is an error in the QML files themselves,(For example when I input a string value in a `height:` parameter) however, it doesn't work when there is a problem in the imports. I made a delegate in another file(In the same directory as 'main.qml') and I'm trying to import it into 'main.qml' this is where my `engine.load` is failing. Yet it seems that `qInstallMessageHandler` isn't helpful for this scenario. – Curtwagner1984 Jan 01 '19 at 00:32
  • @Curtwagner1984 You could share your test through gist or github. – eyllanesc Jan 01 '19 at 00:34
  • @eyllansec Yes, Here it is: https://gist.github.com/curtwagner1984/c091e4ec262289e2c59e911137d1b5c6 – Curtwagner1984 Jan 01 '19 at 00:48
  • @Curtwagner1984 Do not you have another simpler example? I do not want to have to get the credentials of reddit to be able to test the problem :-) – eyllanesc Jan 01 '19 at 00:55
  • @eyllansec I've changed the main, added a moc sub, also created a new 'qml' file. Right now it exits with code -1 without telling what the error is. I've updated the gist Does it work for you when you try to import a file that doesn't exist? (IE does it complain that a file doesn't exist, or just exits with -1?) – Curtwagner1984 Jan 01 '19 at 01:13
  • @Curtwagner1984 I think you did not understand me, your code needs client_secret, username, password and I do not want to create a reddit account – eyllanesc Jan 01 '19 at 01:19
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/185993/discussion-between-curtwagner1984-and-eyllanesc). – Curtwagner1984 Jan 01 '19 at 01:20
  • @Curtwagner1984 I get the error: `prawcore.exceptions.RequestException: error with request Invalid return character or leading space in header: User-Agent` nd it's clearly because I'm passing him an empty user_agent – eyllanesc Jan 01 '19 at 01:35
  • @eyllansec I've made a new gist, with only the main function and one qml file: https://gist.github.com/curtwagner1984/e49468256e176867f5c78664eec861a3 It works fine, but if you try to import some file that doesn't exist (Uncomment some of the imports) it will return with -1 without specifying why. – Curtwagner1984 Jan 01 '19 at 01:42
  • @Curtwagner1984 Only for the following link: `import 'D:/Windows/Documents/RedditViewer/QML/TestDelegate.qml'` It does not indicate the cause of the error, for the other 2 cases if it tells me that the file does not exist. – eyllanesc Jan 01 '19 at 01:47