C++ Registered Type QInvokable Call



  • Hello,
    I have built a little QML-Application and have a problem: When i call a C++ Function, exposed by QINVOKABLE macro, the GUI freeze until the call returns.

    Details:
    I build a c++ class database that handles a database connection. The class provides functions like connect or loadDataInRam. These functions are exposed by the Q_INVOKABLE.

    The class database is registered for usage in QML:
    @qmlRegisterType<Database>("com.mycompany.qmlcomponents", 0,5, "Database");@

    The new type "Database" is instantiated in QML.
    @ Database {
    id: database
    }@

    Now i call the functions e.g. in a button slot

    @Button{
    id:btconnect
    text: "Connect"
    onClicked: {
    database.connect()
    ....
    database.loadDataInRam()
    }
    }@

    The GUI freeze until the onClicked block is performed.... I tried to use the worker script for getting a new "buisnesslogic performer thread" but it turns out that this generates a new JS-Engine without access to the QML components.
    Another option is to trigger threads in the database class, so all functions just start threads and return immediately, but i want to avoid that implementation overhead.

    The best way I read is:
    Move the QObject derivate class database to another (new) thread with moveToThread ( QThread * targetThread ). Change the Q_INVOKABLE to exposed slots. And trigger the slots from QML.

    Is it right that the slot calls are stored in a queue and are processed by the registered thread.
    Is possible to return something? Where i can find the signal slot threading behavior documentaion?

    Is this the way to go? I am looking for a kind of "best practice".

    Thanks
    Tobi



  • No one?


  • Moderators

    Hi, and welcome to the Qt Dev Net!

    [quote]The best way I read is:
    Move the QObject derivate class database to another (new) thread with moveToThread ( QThread * targetThread ). Change the Q_INVOKABLE to exposed slots. And trigger the slots from QML.[/quote]Yes, I would recommend this approach too.

    [quote]Is it right that the slot calls are stored in a queue and are processed by the registered thread.[/quote]Correct.

    [quote]Is possible to return something?[/quote]You can emit a C++ signal, and connect it to a QML slot/function. Or, you can invoke the QML function using a queued connection. See "Interacting with QML Objects from C++|Invoking QML Methods":http://qt-project.org/doc/qt-5/qtqml-cppintegration-interactqmlfromcpp.html#invoking-qml-methods

    [quote]Where i can find the signal slot threading behavior documentaion?[/quote]The most important concepts are at "QObject|Thread Affinity":http://qt-project.org/doc/qt-5/QObject.html#thread-affinity

    The "QThread documentation":http://qt-project.org/doc/qt-5/qthread.html has good information too.

    Finally, see "Multithreading Technologies in Qt":http://qt-project.org/doc/qt-5/threads-technologies.html for a general overview.


Log in to reply
 

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