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_qtconcurrentmap.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]
5U function(const T &t);
6//! [0]
7
8
9//! [1]
10QImage scaled(const QImage &image)
11{
12 return image.scaled(100, 100);
13}
14
16QFuture<QImage> thumbnails = QtConcurrent::mapped(images, scaled);
17//! [1]
18
19
20//! [2]
21U function(T &t);
22//! [2]
23
24
25//! [3]
26void scale(QImage &image)
27{
28 image = image.scaled(100, 100);
29}
30
31QList<QImage> images = ...;
32QFuture<void> future = QtConcurrent::map(images, scale);
33//! [3]
34
35
36//! [4]
37V function(T &result, const U &intermediate)
38//! [4]
39
40
41//! [5]
42void addToCollage(QImage &collage, const QImage &thumbnail)
43{
44 QPainter p(&collage);
45 static QPoint offset = QPoint(0, 0);
46 p.drawImage(offset, thumbnail);
47 offset += ...;
48}
49
50QList<QImage> images = ...;
51QFuture<QImage> collage = QtConcurrent::mappedReduced(images, scaled, addToCollage);
52//! [5]
53
54
55//! [6]
56QList<QImage> images = ...;
57
58QFuture<QImage> thumbnails = QtConcurrent::mapped(images.constBegin(), images.constEnd(), scaled);
59
60// Map in-place only works on non-const iterators.
61QFuture<void> future = QtConcurrent::map(images.begin(), images.end(), scale);
62
63QFuture<QImage> collage = QtConcurrent::mappedReduced(images.constBegin(), images.constEnd(), scaled, addToCollage);
64//! [6]
65
66
67//! [7]
68QList<QImage> images = ...;
69
70// Each call blocks until the entire operation is finished.
71QList<QImage> future = QtConcurrent::blockingMapped(images, scaled);
72
73QtConcurrent::blockingMap(images, scale);
74
75QImage collage = QtConcurrent::blockingMappedReduced(images, scaled, addToCollage);
76//! [7]
77
78
79//! [8]
80// Squeeze all strings in a QStringList.
82QFuture<void> squeezedStrings = QtConcurrent::map(strings, &QString::squeeze);
83
84// Swap the rgb values of all pixels on a list of images.
85QList<QImage> images = ...;
86QFuture<QImage> bgrImages = QtConcurrent::mapped(images,
87 static_cast<QImage (QImage::*)() const &>(&QImage::rgbSwapped));
88
89// Create a set of the lengths of all strings in a list.
91QFuture<QSet<int>> wordLengths = QtConcurrent::mappedReduced(strings, &QString::length,
92 qOverload<const int&>(&QSet<int>::insert));
93//! [8]
94
95
96//! [9]
97// Can mix normal functions and member functions with QtConcurrent::mappedReduced().
98
99// Compute the average length of a list of strings.
100extern void computeAverage(int &average, int length);
101QStringList strings = ...;
102QFuture<int> averageWordLength = QtConcurrent::mappedReduced(strings, &QString::length, computeAverage);
103
104// Create a set of the color distribution of all images in a list.
105extern int colorDistribution(const QImage &string);
106QList<QImage> images = ...;
107QFuture<QSet<int>> totalColorDistribution = QtConcurrent::mappedReduced(images, colorDistribution,
108 qOverload<const int&>(&QSet<int>::insert));
109//! [9]
110
111
112//! [10]
113QImage QImage::scaledToWidth(int width, Qt::TransformationMode) const;
114//! [10]
115
116//! [11]
118{
119 void operator()(QImage &result, const QImage &value);
120};
121
123 QtConcurrent::mappedReduced(images, Scaled(100), ImageTransform(),
124 QtConcurrent::SequentialReduce);
125//! [11]
126
127//! [13]
128QList<QImage> images = ...;
129std::function<QImage(const QImage &)> scale = [](const QImage &img) {
130 return img.scaledToWidth(100, Qt::SmoothTransformation);
131};
132QFuture<QImage> thumbnails = QtConcurrent::mapped(images, scale);
133//! [13]
134
135//! [14]
136struct Scaled
137{
138 Scaled(int size)
139 : m_size(size) { }
140
141 QImage operator()(const QImage &image)
142 {
143 return image.scaled(m_size, m_size);
144 }
145
147};
148
149QList<QImage> images = ...;
150QFuture<QImage> thumbnails = QtConcurrent::mapped(images, Scaled(100));
151//! [14]
152
153//! [15]
154QList<int> vector { 1, 2, 3, 4 };
155QtConcurrent::blockingMap(vector, [](int &x) { x *= 2; });
156
157int size = 100;
158QList<QImage> images = ...;
159
160QList<QImage> thumbnails = QtConcurrent::mapped(images,
161 [&size](const QImage &image) {
162 return image.scaled(size, size);
163 }
164 ).results();
165//! [15]
166
167//! [16]
168QList<QImage> collage = QtConcurrent::mappedReduced(images,
169 [&size](const QImage &image) {
170 return image.scaled(size, size);
171 },
172 addToCollage
173 ).results();
174//! [16]
175
176//! [17]
177QList<QImage> collage = QtConcurrent::mappedReduced(images,
178 [&size](const QImage &image) {
179 return image.scaled(size, size);
180 },
181 [](QImage &result, const QImage &value) {
182 // do some transformation
183 }
184 ).results();
185//! [17]
186
187//! [18]
188auto process = [](int val) {
189 return val * 2;
190};
191
192QList<int> inputs { 1, 2, 3 };
193auto badFuture = QtConcurrent::mapped(inputs, process)
194 .then([](int val) {
195 qDebug() << val;
196 });
197
198auto goodFuture = QtConcurrent::mapped(inputs, process)
199 .then([](QFuture<int> f) {
200 for (auto r : f.results()) {
201 qDebug() << r;
202 }
203 });
204//! [18]
QList< QImage > images
[6]
V function(T &result, const U &intermediate) void addToDictionary(QSet< QString > &dictionary
[2]
QFuture< QImage > collage
[15]
bool function(const T &t)
[0]
void addToCollage(QImage &collage, const QImage &grayscaleImage)
QStringList strings
[1]
QFuture< void > future
[5]
QList< int > vector
[14]
void computeAverage(int &average, int length)
[8]
QFuture< void > squeezedStrings
std::function< QImage(const QImage &)> scale
void scale(QImage &image)
[2]
QFuture< int > averageWordLength
QFuture< QImage > bgrImages
V const QImage & thumbnail
QFuture< QImage > thumbNails
QImage scaled(const QImage &image)
[0]
static QPoint offset
int colorDistribution(const QImage &string)
U function(T &t)
[1]
QFuture< QSet< int > > wordLengths
QFuture< QSet< int > > totalColorDistribution
QFuture< QImage > thumbnails
void operator()(QImage &result, const QImage &value)
QImage operator()(const QImage &image)