Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. General talk
  3. Showcase
  4. Project: Quick Future - Using QFuture in QML

Project: Quick Future - Using QFuture in QML

Scheduled Pinned Locked Moved Showcase
5 Posts 4 Posters 3.4k Views
  • 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.
  • benlauB Offline
    benlauB Offline
    benlau
    Qt Champions 2016
    wrote on last edited by kshegunov
    #1

    Project: Quick Future
    Source: https://github.com/benlau/quickfuture

    QuickFuture is a QML wrapper of QFuture. It allows user to access and listen from a QFuture object generated by a QObject class. So that QML could respond to the result of a threaded calculation.

    Example

    import Future 1.0
    
    ...
    
    var future = FileActor.read(“tmp.txt”);
    // FileActor is a C++ class registered as context property
    // QFuture<QString> FileActor::read(const QString&file);
    // It is not a part of the library
    
    Future.onFinished(future, function(value) {
      // do something when it is finished.
    });
    
    Future.promise(future).then(function(value) {
      // Future.promise creates a promise object by using Quick Promise.
      // It is useful when you have multiple asynchronous tasks pending.
    });
    
    ...
    
    

    Installation

    qpm install quick.future.pri
    

    Custom Type Registration

    By default, QFuture<T> is not a standard type recognized by QML. It must be registered as a QMetaType per template type in order to get rid the error message.

    The same rule applies in Quick Future too. Common types are pre-registered already.
    For your own custom type, you can register it by:

    #include <QFFuture>
    Q_DECLARE_METATYPE(QFuture<CustomType>)
    
    ...
    
    int main(int argc, char *argv[])
    {
    
    ...
       QFFuture::registerType<CustomType>();
    ...
    
    }
    

    Pre-registered data type list: bool, int, qreal, QString, QByteArray, QVariantMap, void.

    API

    (More API will be added upon request)

    Future.isFinished(future)

    Returns true if the asynchronous computation represented by this future has finished; otherwise returns false.

    Future.onFinished(future, callback)

    The callback will be invoked when the watched future finishes.

    Future.promise(future)

    Create a promise object which will be resolved when the future has finished. It must have QuickPromise installed and setup properly before using this function.

    [Moved to Showcase ~kshegunov]

    1 Reply Last reply
    2
    • kshegunovK Offline
      kshegunovK Offline
      kshegunov
      Moderators
      wrote on last edited by
      #2

      Pretty neat. Thanks for sharing!

      Read and abide by the Qt Code of Conduct

      1 Reply Last reply
      0
      • L Offline
        L Offline
        Larpon
        wrote on last edited by
        #3

        Thanks for sharing. Your work is much appreciated!

        1 Reply Last reply
        0
        • benlauB Offline
          benlauB Offline
          benlau
          Qt Champions 2016
          wrote on last edited by
          #4

          v1.0.2 has been released!

          Critical Changes

          1. The QML package name has been changed from Future to QuickFuture
          import QuickFuture 1.0
          
          1. Custom type registration is done by QuickFuture::registerType instead of QFFuture::registerType
          #include <QuickFuture>
          Q_DECLARE_METATYPE(QFuture<CustomType>)
          
          ...
          
          int main(int argc, char *argv[])
          {
          
          ...
             QuickFuture::registerType<CustomType>();
          ...
          
          }
          
          

          New API

          Future.isRunning(future)

          Future.isCanceled(future)

          Future.onCanceled(future, callback)

          Future.result(future)

          Future.sync(future, propertyAtFuture, target, propertyAtTarget)

          Synchronize a property in future object to target object.

          Example:

          
          QtObject {
              id: target1
              property var isRunning
              property var isFinished
          }
          
          // Future.sync(future,"isRunning", target1, "isRunning");
          // Future.sync(future,"isFinished", target1);
          

          Supported properties: "isRunning", "isCanceled", "isFinished"

          benlauB 1 Reply Last reply
          0
          • benlauB benlau

            v1.0.2 has been released!

            Critical Changes

            1. The QML package name has been changed from Future to QuickFuture
            import QuickFuture 1.0
            
            1. Custom type registration is done by QuickFuture::registerType instead of QFFuture::registerType
            #include <QuickFuture>
            Q_DECLARE_METATYPE(QFuture<CustomType>)
            
            ...
            
            int main(int argc, char *argv[])
            {
            
            ...
               QuickFuture::registerType<CustomType>();
            ...
            
            }
            
            

            New API

            Future.isRunning(future)

            Future.isCanceled(future)

            Future.onCanceled(future, callback)

            Future.result(future)

            Future.sync(future, propertyAtFuture, target, propertyAtTarget)

            Synchronize a property in future object to target object.

            Example:

            
            QtObject {
                id: target1
                property var isRunning
                property var isFinished
            }
            
            // Future.sync(future,"isRunning", target1, "isRunning");
            // Future.sync(future,"isFinished", target1);
            

            Supported properties: "isRunning", "isCanceled", "isFinished"

            benlauB Offline
            benlauB Offline
            benlau
            Qt Champions 2016
            wrote on last edited by
            #5

            Quick Future v1.0.3 has been released. This version supports a custom type converter for making a QML friendly data structure from QFuture<YourCustomType>. Calling an async function and obtain the result from QML on a C++ class is much easier now.

            New Features

            Custom Converter function

            QuickFuture::registerType() now supports to assign a custom converter for making a QML friendly data structure (e.g QVariantMap) from the custom type. The value could be obtained by using Future.result()

            
            class Actor : public QObject {
            Q_OBJECT
            public:
              class Reply {
                public:
                  int code;
                  QString message;
              };
              QFuture<Reply> read(QString source);
            }
            
            static void init() {
                QuickFuture::registerType<Actor::Reply>([](Actor::Reply reply) -> QVariant {
                    // Optional converter function.
                    QVariantMap map;
                    map["code"] = reply.code;
                    map["message"] = reply.message;
                    return map;
                });
            }
            
            Q_COREAPP_STARTUP_FUNCTION(init)
            
            
            var future = Actor.read(source);
            ....
            console.log(Future.result(future)); // Print { code: 0, message: ""} if the reply is empty
            
            1 Reply Last reply
            1

            • Login

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