Skip to content

QmlTask

ModuleQml
Include
#include <QCoroQmlTask>
CMake
target_link_libraries(myapp QCoro::Qml)
QMake
QT += QCoroQml

QmlTask allows to return QCoro::Tasks directly to QML. It can be constructed from any QCoro::Task that returns a value that can be converted to a QVariant.

#include <QCoroQml>
#include <QCoroQmlTask>

int main()
{
    ...
    qmlRegisterType<Example>("io.me.qmlmodule", 1, 0, "Example");
    QCoro::Qml::registerTypes();
    ...
}

class Example : public QObject
{
    Q_OBJECT

    ...

public:
    Q_INVOKABLE QCoro::QmlTask fetchValue(const QString &name) const
    {
        return database->fetchValue(name);
        // Returns QCoro::Task<QString>
    }
}

QmlTasks can call a JavaScript function when they complete:

Example {
    Component.onCompleted: {
        fetchValue("key").then((result) => console.log("Result", result))
    }
}

They can also set properties when the value is available:

import QCoro 0
import io.me.qmlmodule 1.0

Item {
    Example { id: store }

    Label {
        text: store.fetchValue("key").await().value
    }
}