Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. QML and Qt Quick
  4. Using async call results in QML

Using async call results in QML

Scheduled Pinned Locked Moved Unsolved QML and Qt Quick
4 Posts 4 Posters 351 Views 2 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • J Offline
    J Offline
    Joey7796
    wrote on last edited by
    #1

    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。

    1 Reply Last reply
    0
    • GrecKoG Offline
      GrecKoG Offline
      GrecKo
      Qt Champions 2018
      wrote on last edited by
      #2

      You could inspire yourself from this : https://github.com/benlau/quickfuture

      My guess is that can be simplified in Qt 6 with easier registration.

      1 Reply Last reply
      0
      • jeremy_kJ Offline
        jeremy_kJ Offline
        jeremy_k
        wrote on last edited by
        #3

        An expedient solution is to wrap the asynchronous behavior in a customized C++ QObject, initiate the call through a property or Q_INVOKABLE, and later return the result through a property or signal.

        Asking a question about code? http://eel.is/iso-c++/testcase/

        1 Reply Last reply
        0
        • K Offline
          K Offline
          Kai Nickel
          wrote on last edited by
          #4

          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.

          1 Reply Last reply
          0

          • Login

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • Users
          • Groups
          • Search
          • Get Qt Extensions
          • Unsolved