Qt
Internal/Contributor docs for the Qt SDK. Note: These are NOT official API docs; those are found at https://doc.qt.io/
Loading...
Searching...
No Matches
src_concurrent_qtconcurrentrun.cpp
Go to the documentation of this file.
1// Copyright (C) 2016 The Qt Company Ltd.
2// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR BSD-3-Clause
3
4//! [0]
5extern void aFunction();
6QFuture<void> future = QtConcurrent::run(aFunction);
7//! [0]
8
9
10//! [explicit-pool-0]
11extern void aFunction();
13QFuture<void> future = QtConcurrent::run(&pool, aFunction);
14//! [explicit-pool-0]
15
16
17//! [1]
18extern void aFunctionWithArguments(int arg1, double arg2, const QString &string);
19
20int integer = ...;
21double floatingPoint = ...;
23
24QFuture<void> future = QtConcurrent::run(aFunctionWithArguments, integer, floatingPoint, string);
25//! [1]
26
27
28//! [2]
30QFuture<QString> future = QtConcurrent::run(functionReturningAString);
31...
32QString result = future.result();
33//! [2]
34
35
36//! [3]
37extern QString someFunction(const QByteArray &input);
38
40
41QFuture<QString> future = QtConcurrent::run(someFunction, bytearray);
42...
43QString result = future.result();
44//! [3]
45
46//! [4]
47// call 'QList<QByteArray> QByteArray::split(char sep) const' in a separate thread
48QByteArray bytearray = "hello world";
49QFuture<QList<QByteArray> > future = QtConcurrent::run(&QByteArray::split, bytearray, ' ');
50...
51QList<QByteArray> result = future.result();
52//! [4]
53
54//! [5]
55// call 'void QImage::invertPixels(InvertMode mode)' in a separate thread
57QFuture<void> future = QtConcurrent::run(&QImage::invertPixels, &image, QImage::InvertRgba);
58...
59future.waitForFinished();
60// At this point, the pixels in 'image' have been inverted
61//! [5]
62
63//! [6]
64QFuture<void> future = QtConcurrent::run([=]() {
65 // Code in this block will run in another thread
66});
67...
68//! [6]
69
70//! [7]
71static void addOne(int &n) { ++n; }
72...
73int n = 42;
74QtConcurrent::run(&addOne, std::ref(n)).waitForFinished(); // n == 43
75//! [7]
76
77//! [8]
79{
80 void operator()(int s1) { s = s1; }
81 int s = 42;
82};
83
84...
85
86TestClass o;
87
88// Modify original object
89QtConcurrent::run(std::ref(o), 15).waitForFinished(); // o.s == 15
90
91// Modify a copy of the original object
92QtConcurrent::run(o, 42).waitForFinished(); // o.s == 15
93
94// Use a temporary object
95QtConcurrent::run(TestClass(), 42).waitForFinished();
96
97// Ill-formed
98QtConcurrent::run(&o, 42).waitForFinished(); // compilation error
99//! [8]
100
101//! [9]
102extern void aFunction(QPromise<void> &promise);
103QFuture<void> future = QtConcurrent::run(aFunction);
104//! [9]
105
106//! [10]
107extern void aFunction(QPromise<void> &promise, int arg1, const QString &arg2);
108
109int integer = ...;
110QString string = ...;
111
112QFuture<void> future = QtConcurrent::run(aFunction, integer, string);
113//! [10]
114
115//! [11]
116void helloWorldFunction(QPromise<QString> &promise)
117{
118 promise.addResult("Hello");
119 promise.addResult("world");
120}
121
122QFuture<QString> future = QtConcurrent::run(helloWorldFunction);
123...
124QList<QString> results = future.results();
125//! [11]
126
127//! [12]
128void aFunction(QPromise<int> &promise)
129{
130 for (int i = 0; i < 100; ++i) {
131 promise.suspendIfRequested();
132 if (promise.isCanceled())
133 return;
134
135 // computes the next result, may be time consuming like 1 second
136 const int res = ... ;
137 promise.addResult(res);
138 }
139}
140
141QFuture<int> future = QtConcurrent::run(aFunction);
142
143... // user pressed a pause button after 10 seconds
144future.suspend();
145
146... // user pressed a resume button after 10 seconds
147future.resume();
148
149... // user pressed a cancel button after 10 seconds
150future.cancel();
151//! [12]
152
153//! [13]
154void aFunction(QPromise<int> &promise)
155{
156 promise.setProgressRange(0, 100);
157 int result = 0;
158 for (int i = 0; i < 100; ++i) {
159 // computes some part of the task
160 const int part = ... ;
161 result += part;
162 promise.setProgressValue(i);
163 }
164 promise.addResult(result);
165}
166
168QObject::connect(&watcher, &QFutureWatcher::progressValueChanged, [](int progress){
169 ... ; // update GUI with a progress
170 qDebug() << "current progress:" << progress;
171});
172watcher.setFuture(QtConcurrent::run(aFunction));
173//! [13]
174
175//! [14]
176struct Functor {
177 void operator()(QPromise<int> &) { }
178 void operator()(QPromise<double> &) { }
179};
180
182run<double>(f); // this will select the 2nd overload
183// run(f); // error, both candidate overloads potentially match
184//! [14]
185
186//! [15]
187void foo(int arg);
188void foo(int arg1, int arg2);
189...
190QFuture<void> future = QtConcurrent::run(foo, 42);
191//! [15]
192
193//! [16]
194QFuture<void> future = QtConcurrent::run([] { foo(42); });
195//! [16]
196
197//! [17]
198QFuture<void> future = QtConcurrent::run(static_cast<void(*)(int)>(foo), 42);
199//! [17]
200
201//! [18]
202QFuture<void> future = QtConcurrent::run(qOverload<int>(foo), 42);
203//! [18]
QFuture< void > future
[5]
QByteArray bytearray
[3]
void foo(int arg1, int arg2)
void aFunction()
[0]
void aFunctionWithArguments(int arg1, double arg2, const QString &string)
[explicit-pool-0]
void foo(int arg)
[14]
QThreadPool pool
[8]
void aFunction(QPromise< void > &promise)
[8]
void aFunction(QPromise< void > &promise, int arg1, const QString &arg2)
[9]
QString someFunction(const QByteArray &input)
[2]
QString functionReturningAString()
[1]
void helloWorldFunction(QPromise< QString > &promise)
[10]
QFutureWatcher< int > watcher
void operator()(QPromise< int > &)