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_qtconcurrentfilter.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]
5bool function(const T &t);
6//! [0]
7
8
9//! [1]
10bool allLowerCase(const QString &string)
11{
12 return string.lowered() == string;
13}
14
16QFuture<QString> lowerCaseStrings = QtConcurrent::filtered(strings, allLowerCase);
17//! [1]
18
19
20//! [2]
22QFuture<void> future = QtConcurrent::filter(strings, allLowerCase);
23//! [2]
24
25
26//! [3]
27V function(T &result, const U &intermediate)
28//! [3]
29
30
31//! [4]
32void addToDictionary(QSet<QString> &dictionary, const QString &string)
33{
34 dictionary.insert(string);
35}
36
38QFuture<QSet<QString>> dictionary = QtConcurrent::filteredReduced(strings, allLowerCase, addToDictionary);
39//! [4]
40
41
42//! [5]
44QFuture<QString> lowerCaseStrings = QtConcurrent::filtered(strings.constBegin(), strings.constEnd(), allLowerCase);
45
46// filter in-place only works on non-const iterators
47QFuture<void> future = QtConcurrent::filter(strings.begin(), strings.end(), allLowerCase);
48
49QFuture<QSet<QString>> dictionary = QtConcurrent::filteredReduced(strings.constBegin(), strings.constEnd(), allLowerCase, addToDictionary);
50//! [5]
51
52
53//! [6]
55
56// each call blocks until the entire operation is finished
57QStringList lowerCaseStrings = QtConcurrent::blockingFiltered(strings, allLowerCase);
58
59
60QtConcurrent::blockingFilter(strings, allLowerCase);
61
62QSet<QString> dictionary = QtConcurrent::blockingFilteredReduced(strings, allLowerCase, addToDictionary);
63//! [6]
64
65
66//! [7]
67// keep only images with an alpha channel
69QFuture<void> alphaImages = QtConcurrent::filter(images, &QImage::hasAlphaChannel);
70
71// retrieve gray scale images
72QList<QImage> images = ...;
73QFuture<QImage> grayscaleImages = QtConcurrent::filtered(images, &QImage::isGrayscale);
74
75// create a set of all printable characters
77QFuture<QSet<QChar>> set = QtConcurrent::filteredReduced(characters, qOverload<>(&QChar::isPrint),
78 qOverload<const QChar&>(&QSet<QChar>::insert));
79//! [7]
80
81
82//! [8]
83// can mix normal functions and member functions with QtConcurrent::filteredReduced()
84
85// create a dictionary of all lower cased strings
86extern bool allLowerCase(const QString &string);
88QFuture<QSet<QString>> lowerCase = QtConcurrent::filteredReduced(strings, allLowerCase,
89 qOverload<const QString&>(&QSet<QString>::insert));
90
91// create a collage of all gray scale images
92extern void addToCollage(QImage &collage, const QImage &grayscaleImage);
93QList<QImage> images = ...;
94QFuture<QImage> collage = QtConcurrent::filteredReduced(images, &QImage::isGrayscale, addToCollage);
95//! [8]
96
97
98//! [9]
99bool QString::contains(const QRegularExpression &regexp) const;
100//! [9]
101
102
103//! [12]
104QStringList strings = ...;
105QFuture<QString> future = QtConcurrent::filtered(list, [](const QString &str) {
106 return str.contains(QRegularExpression("^\\S+$")); // matches strings without whitespace
107});
108//! [12]
109
110//! [13]
112{
113 StartsWith(const QString &string)
114 : m_string(string) { }
115
116 bool operator()(const QString &testString)
117 {
118 return testString.startsWith(m_string);
119 }
120
122};
123
124QList<QString> strings = ...;
125QFuture<QString> fooString = QtConcurrent::filtered(strings, StartsWith(QLatin1String("Foo")));
126//! [13]
127
128//! [14]
130{
131 void operator()(QString &result, const QString &value);
132};
133
135 QtConcurrent::filteredReduced(strings, StartsWith(QLatin1String("Foo")), StringTransform());
136//! [14]
137
138//! [15]
139// keep only even integers
140QList<int> list { 1, 2, 3, 4 };
141QtConcurrent::blockingFilter(list, [](int n) { return (n & 1) == 0; });
142
143// retrieve only even integers
144QList<int> list2 { 1, 2, 3, 4 };
145QFuture<int> future = QtConcurrent::filtered(list2, [](int x) {
146 return (x & 1) == 0;
147});
148QList<int> results = future.results();
149
150// add up all even integers
151QList<int> list3 { 1, 2, 3, 4 };
152QFuture<int> sum = QtConcurrent::filteredReduced(list3,
153 [](int x) {
154 return (x & 1) == 0;
155 },
156 [](int &sum, int x) {
157 sum += x;
158 }
159);
160//! [15]
161
162//! [16]
163void intSumReduce(int &sum, int x)
164{
165 sum += x;
166}
167
168QList<int> list { 1, 2, 3, 4 };
169QFuture<int> sum = QtConcurrent::filteredReduced(list,
170 [] (int x) {
171 return (x & 1) == 0;
172 },
173 intSumReduce
174);
175//! [16]
176
177//! [17]
179{
180 return (x & 1) == 0;
181}
182
183QList<int> list { 1, 2, 3, 4 };
184QFuture<int> sum = QtConcurrent::filteredReduced(list,
185 keepEvenIntegers,
186 [](int &sum, int x) {
187 sum += x;
188 }
189);
190//! [17]
bool keepEvenIntegers(int x)
[16]
QFuture< void > alphaImages
QList< QImage > images
[6]
QFuture< QString > fooString
V function(T &result, const U &intermediate) void addToDictionary(QSet< QString > &dictionary
[2]
QFuture< QImage > collage
[15]
bool function(const T &t)
[0]
QFuture< QImage > grayscaleImages
QList< int > list
[14]
QFuture< void > future
[5]
void addToCollage(QImage &collage, const QImage &grayscaleImage)
QList< QChar > characters
QFuture< QString > lowerCaseStrings
QFuture< QSet< QChar > > set
[10]
QFuture< QSet< QString > > lowerCase
QStringList strings
[1]
bool allLowerCase(const QString &string)
[0]
void intSumReduce(int &sum, int x)
[15]
QFuture< QSet< QString > > dictionary
V const QString & string
bool operator()(const QString &testString)
StartsWith(const QString &string)
void operator()(QString &result, const QString &value)