summaryrefslogtreecommitdiffstats
path: root/src/corelib/doc/snippets
diff options
context:
space:
mode:
authorSona Kurazyan <sona.kurazyan@qt.io>2020-05-06 17:42:21 +0200
committerSona Kurazyan <sona.kurazyan@qt.io>2020-05-15 22:35:06 +0200
commit612f6999c81a500a024f128bdf739342d659754a (patch)
treec31990d9621f5b1c4754a4149222042daafea219 /src/corelib/doc/snippets
parent54aa63be9b74e8de72db9efbe6809ab1a97b29a7 (diff)
Add support of connecting signals to QFuture
Introduced QtFuture::connect(sender, signal) function returning a QFuture object, which is resolved when the signal is emitted. Task-number: QTBUG-81589 Change-Id: Idbe301eb247b468b9b34f3470c3359d6a7af2f3a Reviewed-by: Jarek Kobus <jaroslaw.kobus@qt.io> Reviewed-by: Qt CI Bot <qt_ci_bot@qt-project.org> Reviewed-by: Fabian Kosmale <fabian.kosmale@qt.io>
Diffstat (limited to 'src/corelib/doc/snippets')
-rw-r--r--src/corelib/doc/snippets/code/src_corelib_thread_qfuture.cpp45
1 files changed, 45 insertions, 0 deletions
diff --git a/src/corelib/doc/snippets/code/src_corelib_thread_qfuture.cpp b/src/corelib/doc/snippets/code/src_corelib_thread_qfuture.cpp
index 66fa62f6b3..0580f142d7 100644
--- a/src/corelib/doc/snippets/code/src_corelib_thread_qfuture.cpp
+++ b/src/corelib/doc/snippets/code/src_corelib_thread_qfuture.cpp
@@ -175,3 +175,48 @@ try {
// Handle the exception
}
//! [9]
+
+//! [10]
+class Object : public QObject
+{
+ Q_OBJECT
+ ...
+signals:
+ void noArgSignal();
+ void singleArgSignal(int value);
+ void multipleArgs(int value1, double value2, const QString &value3);
+};
+//! [10]
+
+//! [11]
+Object object;
+QFuture<void> voidFuture = QtFuture::connect(&object, &Object::noArgSignal);
+QFuture<int> intFuture = QtFuture::connect(&object, &Object::singleArgSignal);
+
+using Args = std::tuple<int, double, QString>;
+QFuture<Args> tupleFuture = QtFuture::connect(&object, &Object::multipleArgs)
+//! [11]
+
+//! [12]
+QtFuture::connect(&object, &Object::singleArgSignal).then([](int value) {
+ // do something with the value
+});
+//! [12]
+
+//! [13]
+QtFuture::connect(&object, &Object::singleArgSignal).then(QtFuture::Launch::Async, [](int value) {
+ // this will run in a new thread
+});
+//! [13]
+
+//! [14]
+QtFuture::connect(&object, &Object::singleArgSignal).then([](int value) {
+ ...
+ throw std::exception();
+ ...
+}).onFailed([](const std::exception &e) {
+ // handle the exception
+}).onFailed([] {
+ // handle other exceptions
+});
+//! [14]