Home > Back-end >  Howto call c member function as async call from QML
Howto call c member function as async call from QML

Time:01-26

I'm calling a Q_INVOKABLE member function from a QML. The GUI freezes until the function is fully executed. I've some network operations going on in the invokable function therefore I want to GUI to continue exec and don't block for function to be completed.

QT documentation contains WorkerScript QML but that doesn't fulfill my requirement as I cannot call invokable function inside .mjs file.

Is their a QT way of how to deal with such problem ?

PS: I've exposed my C class to QML as below

Engine.rootContext()->setContextProperty("myClass", &obj);

And in the qml, I'm calling the member function as:

onTriggered: {
  
 myClass.myFunction( ... ); // This function needs to be executed async

} 

CodePudding user response:

The async model does not exist in qml, this is why workerscript has been added.

To get a similar behavior on the c side, you would have to use QThread. To pass the results of your computation back to QML once the worker thread finish, you can, on the QML side, connect a callback to a given signal emitted by your class once the worker thread is done. To create the connection in QML, you might have to use the connection class [ https://doc.qt.io/qt-5/qml-qtqml-connections.html ], depending on how your c class is exposed to qml.

  •  Tags:  
  • Related