Throwing QML exception from C++ in QtQuick 2 application



  • Hi,

    I am currently using a library that I have exposed to the QML layer. This library uses exceptions to signify many of the error conditions. With a standard Qt GUI application you are able to derive from QApplication and override the notify virtual function to catch exceptions occurred within a slot, but this mechanism doesn't seem to work when using QGuiApplication and QQmlApplicationEngine.

    Is there a way to handle C++ exception that are thrown when accessed via the QML layer? Preferably I don't want to write proxy functions for every property/function exposed to QML with a try/catch handler inside.

    Thanks



  • Update - in fact even a top level exception handler in main seems to be gobbled up by Qt QML engine somewhere and causes the program to exist abruptly by the std c++ handler. So I would say it's pretty critical that exceptions can be somehow handled via QML paths.

    Anyone have any idea how this can be worked around?

    Thanks.



  • What I do is to emit a signal when an error occurs. The net effect is essentially the same as throwing and catching an exception.

    In QML I do things like:

    @Connections {
    target: myCppObject
    onFileNotFoundError: messageBox.show("Could not find file")
    }
    @

    messageBox is a QtQuick item that can be made visible through a show() method. It may be just a Rectangle with a Text and an OK button for example.

    I'm not aware of any way to throw an exception in C++ and have this somehow converted to a javascript exception in QML.



  • Thanks, I'll consider using that once I get to that stage.

    Got a little further. If I enable QML Debugging when launching the app via Qt Creator, the exceptions magically start being caught in my exception handler. So it seems by not having QML debugging on the exceptions get swallowed.


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.