Using async call results in QML
-
in QT6
Q_INVOKABLE QFuture<int> BackendMethod(const QString& addr,const int& port);I don't want to block the UI so I use an asynchronous method call。
When should await / async be used in QtQuick? Is there a roadmap or plan?
Or can I use some syntactic sugar to get asynchronous results? My software has a lot of asynchronous methods, and using signals to connect them all is not a good approach. Are there any standardized or engineered solutions?
Thanks。 -
You could inspire yourself from this : https://github.com/benlau/quickfuture
My guess is that can be simplified in Qt 6 with easier registration.
-
Are you aware of the possibility of calling JS callbacks from C++?
I use something like this:void Misc::doSomething(const QJSValue& callback) { // do sth async here but make sure to call the callback from the main thread callback.call(); }In QML:
Misc.doSomething(() => { console.log("callback!"); })You can also have arguments in the callback funtion, like for result or error.