Qt
Internal/Contributor docs for the Qt SDK. <b>Note:</b> These are NOT official API docs; those are found <a href='https://doc.qt.io/'>here</a>.
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
5extern void aFunction();
8
9
11extern void aFunction();
13QFuture<void> future = QtConcurrent::run(&pool, aFunction);
15
16
18extern void aFunctionWithArguments(int arg1, double arg2, const QString &string);
19
20int integer = ...;
21double floatingPoint = ...;
22QString string = ...;
23
26
27
31...
32QString result = future.result();
34
35
38
40
42...
43QString result = future.result();
45
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...
53
55// call 'void QImage::invertPixels(InvertMode mode)' in a separate thread
58...
59future.waitForFinished();
60// At this point, the pixels in 'image' have been inverted
62
64QFuture<void> future = QtConcurrent::run([=]() {
65 // Code in this block will run in another thread
66});
67...
69
71static void addOne(int &n) { ++n; }
72...
73int n = 42;
74QtConcurrent::run(&addOne, std::ref(n)).waitForFinished(); // n == 43
76
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
100
102extern void aFunction(QPromise<void> &promise);
103QFuture<void> future = QtConcurrent::run(aFunction);
105
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);
114
116void helloWorldFunction(QPromise<QString> &promise)
117{
118 promise.addResult("Hello");
119 promise.addResult("world");
120}
121
123...
126
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
145
146... // user pressed a resume button after 10 seconds
148
149... // user pressed a cancel button after 10 seconds
152
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
167QFutureWatcher<int> watcher;
169 ... ; // update GUI with a progress
170 qDebug() << "current progress:" << progress;
171});
174
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
185
187void foo(int arg);
188void foo(int arg1, int arg2);
189...
190QFuture<void> future = QtConcurrent::run(foo, 42);
192
194QFuture<void> future = QtConcurrent::run([] { foo(42); });
196
198QFuture<void> future = QtConcurrent::run(static_cast<void(*)(int)>(foo), 42);
200
202QFuture<void> future = QtConcurrent::run(qOverload<int>(foo), 42);
\inmodule QtCore
Definition qbytearray.h:57
QList< QByteArray > split(char sep) const
Splits the byte array into subarrays wherever sep occurs, and returns the list of those arrays.
void progressValueChanged(int progressValue)
void cancel()
Definition qfuture.h:64
QList< T > results() const
Definition qfuture.h:114
T result() const
Definition qfuture.h:309
void suspend()
Definition qfuture.h:89
void resume()
Definition qfuture.h:90
\inmodule QtGui
Definition qimage.h:37
@ InvertRgba
Definition qimage.h:40
void invertPixels(InvertMode=InvertRgb)
Inverts all pixel values in the image.
Definition qimage.cpp:1987
static QMetaObject::Connection connect(const QObject *sender, const char *signal, const QObject *receiver, const char *member, Qt::ConnectionType=Qt::AutoConnection)
\threadsafe
Definition qobject.cpp:2960
\macro QT_RESTRICTED_CAST_FROM_ASCII
Definition qstring.h:129
\inmodule QtCore
Definition qthreadpool.h:22
QTCONCURRENT_RUN_NODISCARD auto run(QThreadPool *pool, Function &&f, Args &&...args)
Definition image.cpp:4
#define qDebug
[1]
Definition qlogging.h:164
GLuint GLfloat GLfloat GLfloat GLfloat GLfloat GLfloat GLfloat GLfloat s1
GLfloat GLfloat f
GLfloat n
GLdouble s
[6]
Definition qopenglext.h:235
GLuint res
GLuint GLuint GLuint GLuint arg1
GLuint GLuint GLuint GLuint GLuint GLuint GLuint arg2
GLuint64EXT * result
[6]
GLenum GLenum GLenum input
SSL_CTX int void * arg
#define s1
QByteArray bytearray
[3]
static void addOne(int &n)
[6]
void aFunction()
[0]
void aFunctionWithArguments(int arg1, double arg2, const QString &string)
[explicit-pool-0]
QFuture< void > future
[5]
QList< QString > results
QString functionReturningAString()
[1]
void helloWorldFunction(QPromise< QString > &promise)
[10]
run< double >(f)
QFutureWatcher< int > watcher
QString foo
[0]
void operator()(QPromise< double > &)
void operator()(QPromise< int > &)