File: qmltask.md

package info (click to toggle)
qcoro 0.12.0-3
  • links: PTS, VCS
  • area: main
  • in suites: forky
  • size: 1,700 kB
  • sloc: cpp: 8,573; python: 32; xml: 26; makefile: 23; sh: 15
file content (65 lines) | stat: -rw-r--r-- 1,271 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
<!--
SPDX-FileCopyrightText: 2022 Jonah BrĂ¼chert <jbb@kaidan.im>
SPDX-License-Identifier: GFDL-1.3-or-later
-->

# QmlTask

{{ doctable("Qml", "QCoroQmlTask") }}

QmlTask allows to return [QCoro::Task][qcoro-task]s directly to QML.
It can be constructed from any [QCoro::Task][qcoro-task] that returns a value that can be converted to a [QVariant][qdoc-qml].

```C++
#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:
```QML
Example {
    Component.onCompleted: {
        fetchValue("key").then((result) => console.log("Result", result))
    }
}
```

They can also set properties when the value is available:
```QML
import QCoro 0
import io.me.qmlmodule 1.0

Item {
    Example { id: store }

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


[qdoc-qml]: https://doc.qt.io/qt-5/qvariant.html
[qcoro-task]: ../coro/task.md