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_qtconcurrenttask.cpp
Go to the documentation of this file.
1// Copyright (C) 2020 The Qt Company Ltd.
2// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR BSD-3-Clause
3
5QtConcurrent::task([]{ qDebug("Hello, world!"); }).spawn();
7
9auto task = [](const QString &s){ qDebug() << ("Hello, " + s); };
10QtConcurrent::task(std::move(task))
11 .withArguments("world!")
12 .spawn();
14
16QString s("Hello, ");
17QtConcurrent::task([](QString &s){ s.append("world!"); })
18 .withArguments(std::ref(s))
19 .spawn();
21
23auto future = QtConcurrent::task([]{ return 42; }).spawn();
24auto result = future.result(); // result == 42
26
28std::is_invocable_v<std::decay_t<Task>, std::decay_t<Args>...>
30
33auto result = QtConcurrent::task([](const QVariant &var){return qvariant_cast<int>(var);})
34 .withArguments(value)
35 .spawn()
36 .result(); // result == 42
38
40QString result("Hello, world!");
41
43 .withArguments(&result, 8)
44 .spawn()
45 .waitForFinished(); // result == "Hello"
47
49auto result = QtConcurrent::task(std::plus<int>())
50 .withArguments(40, 2)
51 .spawn()
52 .result() // result == 42
54
56struct CallableWithState
57{
58 void operator()(int newState) { state = newState; }
59
60 // ...
61};
62
63// ...
64
65CallableWithState object;
66
67QtConcurrent::task(std::ref(object))
68 .withArguments(42)
69 .spawn()
70 .waitForFinished(); // The object's state is set to 42
72
75QtConcurrent::task([]{ return 42; }).onThreadPool(pool).spawn();
77
79QtConcurrent::task([]{ return 42; }).withPriority(10).spawn();
81
83QtConcurrent::task([]{ qDebug("Hello, world!"); }).spawn(FutureResult::Ignore);
85
87void increment(QPromise<int> &promise, int i)
88{
89 promise.addResult(i + 1);
90}
91
92int result = QtConcurrent::task(&increment).withArguments(10).spawn().result(); // result == 11
\macro QT_RESTRICTED_CAST_FROM_ASCII
Definition qstring.h:129
void chop(qsizetype n)
Removes n characters from the end of the string.
Definition qstring.cpp:6340
\inmodule QtCore
Definition qthreadpool.h:22
\inmodule QtCore
Definition qvariant.h:65
else opt state
[0]
void newState(QList< State > &states, const char *token, const char *lexem, bool pre)
constexpr auto task(Task &&t)
EGLOutputLayerEXT EGLint EGLAttrib value
[5]
#define qDebug
[1]
Definition qlogging.h:164
GLdouble s
[6]
Definition qopenglext.h:235
GLuint64EXT * result
[6]
QThreadPool pool
[8]
QtConcurrent::task( future[](QString &s){ s.append("world!");}) .withArguments(std auto
[2]
QtConcurrent::task QString::chop withArguments result spawn() .waitForFinished()
QtConcurrent::task([]{ qDebug("Hello, world!");}).spawn(FutureResult void increment(QPromise< int > &promise, int i)
[10]
CallableWithState object