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
142
143 QImage operator()(const QImage &image)
144 {
145 return image.scaled(m_size, m_size);
146 }
147
149};
150
151QList<QImage> images = ...;
152QFuture<QImage> thumbnails = QtConcurrent::mapped(images, Scaled(100));
153//! [14]
154
155//! [15]
156QList<int> vector { 1, 2, 3, 4 };
157QtConcurrent::blockingMap(vector, [](int &x) { x *= 2; });
158
159int size = 100;
160QList<QImage> images = ...;
161
162QList<QImage> thumbnails = QtConcurrent::mapped(images,
163 [&size](const QImage &image) {
164 return image.scaled(size, size);
165 }
166 ).results();
167//! [15]
168
169//! [16]
170QList<QImage> collage = QtConcurrent::mappedReduced(images,
171 [&size](const QImage &image) {
172 return image.scaled(size, size);
173 },
174 addToCollage
175 ).results();
176//! [16]
177
178//! [17]
179QList<QImage> collage = QtConcurrent::mappedReduced(images,
180 [&size](const QImage &image) {
181 return image.scaled(size, size);
182 },
183 [](QImage &result, const QImage &value) {
184 // do some transformation
185 }
186 ).results();
187//! [17]
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]
QFuture< void > future
[5]
void addToCollage(QImage &collage, const QImage &grayscaleImage)
QStringList strings
[1]
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)