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
doc_src_containers.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#undef QT_NO_FOREACH // this file contains unported legacy Q_FOREACH uses
5
6#include <QtCore>
7
8//! [0]
10{
11public:
13 Employee(const Employee &other);
14
15 Employee &operator=(const Employee &other);
16
17private:
18 QString myName;
19 QDate myDateOfBirth;
20};
21//! [0]
22
23void examles()
24{
25 {
26 //! [range_for]
27 QList<QString> list = {"A", "B", "C", "D"};
28 for (const auto &item : list) {
29 //...
30 }
31 //! [range_for]
32 }
33
34 {
35 //! [range_for_as_const]
36 QList<QString> list = {"A", "B", "C", "D"};
37 for (const auto &item : std::as_const(list)) {
38 //...
39 }
40 //! [range_for_as_const]
41 }
42
43 {
44 //! [index]
45 QList<QString> list = {"A", "B", "C", "D"};
46 for (qsizetype i = 0; i < list.size(); ++i) {
47 const auto &item = list.at(i);
48 //...
49 }
50 //! [index]
51 }
52
53 {
54 //! [1]
55 QList<QString> list = {"A", "B", "C", "D"};
56
57 QListIterator<QString> i(list);
58 while (i.hasNext())
59 QString s = i.next();
60 //! [1]
61 }
62
63 {
64 QList<QString> list = {"A", "B", "C", "D"};
65
66 //! [2]
67 QListIterator<QString> i(list);
68 i.toBack();
69 while (i.hasPrevious())
70 QString s = i.previous();
71 //! [2]
72 }
73
74 {
75 QList<int> list = {1, 2, 3, 4};
76 {
77 //! [3]
78 QMutableListIterator<int> i(list);
79 while (i.hasNext()) {
80 if (i.next() % 2 != 0)
81 i.remove();
82 }
83 //! [3]
84 }
85
86 {
87 //! [4]
88 QMutableListIterator<int> i(list);
89 i.toBack();
90 while (i.hasPrevious()) {
91 if (i.previous() % 2 != 0)
92 i.remove();
93 }
94 //! [4]
95 }
96
97 {
98 //! [5]
99 QMutableListIterator<int> i(list);
100 while (i.hasNext()) {
101 if (i.next() > 128)
102 i.setValue(128);
103 }
104 //! [5]
105 }
106
107 {
108 //! [6]
109 QMutableListIterator<int> i(list);
110 while (i.hasNext())
111 i.next() *= 2;
112 //! [6]
113 }
114 }
115
116 {
117 //! [7]
118 QMap<QString, QString> map = {
119 {"Paris", "France"},
120 {"Guatemala City", "Guatemala"},
121 {"Mexico City", "Mexico"},
122 {"Moscow", "Russia"}
123 };
124 //...
125
126 QMutableMapIterator<QString, QString> i(map);
127 while (i.hasNext()) {
128 if (i.next().key().endsWith("City"))
129 i.remove();
130 }
131 //! [7]
132 }
133
134 {
135 //! [10]
136 QList<QString> list = {"A", "B", "C", "D"};
137
138 for (auto i = list.begin(), end = list.end(); i != end; ++i)
139 *i = (*i).toLower();
140 //! [10]
141 }
142
143 {
144 //! [11]
145 QList<QString> list = {"A", "B", "C", "D"};
146
147 for (auto i = list.rbegin(), rend = list.rend(); i != rend; ++i)
148 *i = i->toLower();
149 //! [11]
150
151
152 //! [12]
153 for (auto i = list.cbegin(), end = list.cend(); i != end; ++i)
154 qDebug() << *i;
155 //! [12]
156
157
158 //! [13]
159 QMap<int, int> map;
160 //...
161 for (auto i = map.cbegin(), end = map.cend(); i != end; ++i)
162 qDebug() << i.key() << ':' << i.value();
163 //! [13]
164 }
165
166 {
167 //! [15]
168 QList<QString> values;
169 //...
170 QString str;
171 foreach (str, values)
172 qDebug() << str;
173 //! [15]
174 }
175
176 {
177 //! [16]
178 QList<QString> values;
179 //...
180 QListIterator<QString> i(values);
181 while (i.hasNext()) {
182 QString s = i.next();
183 qDebug() << s;
184 }
185 //! [16]
186 }
187
188 {
189 //! [17]
190 QList<QString> values;
191 //...
192 foreach (const QString &str, values)
193 qDebug() << str;
194 //! [17]
195 }
196
197 {
198 //! [18]
199 QList<QString> values;
200 //...
201 foreach (const QString &str, values) {
202 if (str.isEmpty())
203 break;
204 qDebug() << str;
205 }
206 //! [18]
207 }
208
209 {
210 //! [19]
211 QMap<QString, int> map;
212 //...
213 foreach (const QString &str, map.keys())
214 qDebug() << str << ':' << map.value(str);
215 //! [19]
216 }
217
218 {
219 //! [20]
220 QMultiMap<QString, int> map;
221 //...
222 foreach (const QString &str, map.uniqueKeys()) {
223 foreach (int i, map.values(str))
224 qDebug() << str << ':' << i;
225 }
226 //! [20]
227 }
228
229 {
230 #if 0
231 //! [22]
232 CONFIG += no_keywords
233 //! [22]
234
235
236 //! [cmake_no_keywords]
237 target_compile_definitions(my_app PRIVATE QT_NO_KEYWORDS)
238 //! [cmake_no_keywords]
239 #endif
240 }
241}
242
243//! [23]
244QString onlyLetters(const QString &in)
245{
246 QString out;
247 for (qsizetype j = 0; j < in.size(); ++j) {
248 if (in.at(j).isLetter())
249 out += in.at(j);
250 }
251 return out;
252}
253//! [23]
254
255void wrap()
256{
257 //! [24]
258 QList<int> a, b;
259 a.resize(100000); // make a big list filled with 0.
260
261 QList<int>::iterator i = a.begin();
262 // WRONG way of using the iterator i:
263 b = a;
264 /*
265 Now we should be careful with iterator i since it will point to shared data
266 If we do *i = 4 then we would change the shared instance (both vectors)
267 The behavior differs from STL containers. Avoid doing such things in Qt.
268 */
269
270 a[0] = 5;
271 /*
272 Container a is now detached from the shared data,
273 and even though i was an iterator from the container a, it now works as an iterator in b.
274 Here the situation is that (*i) == 0.
275 */
276
277 b.clear(); // Now the iterator i is completely invalid.
278
279 int j = *i; // Undefined behavior!
280 /*
281 The data from b (which i pointed to) is gone.
282 This would be well-defined with STL containers (and (*i) == 5),
283 but with QList this is likely to crash.
284 */
285 //! [24]
286
287 {
288 //! [25]
289 QList<int> list = {1, 2, 3, 4, 4, 5};
290 QSet<int> set(list.cbegin(), list.cend());
291 /*
292 Will generate a QSet containing 1, 2, 3, 4, 5.
293 */
294 //! [25]
295 }
296
297 //! [26]
298 QList<int> list = {2, 3, 1};
299
300 std::sort(list.begin(), list.end());
301 /*
302 Sort the list, now contains { 1, 2, 3 }
303 */
304
305 std::reverse(list.begin(), list.end());
306 /*
307 Reverse the list, now contains { 3, 2, 1 }
308 */
309
310 int even_elements =
311 std::count_if(list.begin(), list.end(), [](int element) { return (element % 2 == 0); });
312 /*
313 Count how many elements that are even numbers, 1
314 */
315 //! [26]
316}
317
318#if __has_include(<QWidget>)
319
320#include <QWidget>
321#include <QSplitter>
322
323void examples_with_widgets()
324{
325 {
326 //! [8]
327 QMap<int, QWidget *> map;
328 QHash<int, QWidget *> hash;
329
330 QMapIterator<int, QWidget *> i(map);
331 while (i.hasNext()) {
332 i.next();
333 hash.insert(i.key(), i.value());
334 }
335 //! [8]
336 }
337
338 {
339 QMap<int, QWidget *> map;
340 QWidget* widget;
341 //! [9]
342 QMutableMapIterator<int, QWidget *> i(map);
343 while (i.findNext(widget))
344 i.remove();
345 //! [9]
346 }
347
348 QSplitter* splitter;
349
350 //! [14]
351 // RIGHT
352 const QList<int> sizes = splitter->sizes();
353 for (auto i = sizes.begin(), end = sizes.end(); i != end; ++i)
354 {/*...*/}
355
356 // WRONG
357 for (auto i = splitter->sizes().begin();
358 i != splitter->sizes().end(); ++i)
359 {/*...*/}
360 //! [14]
361}
362#endif
Employee & operator=(const Employee &other)
Employee(const Employee &other)
void wrap()
[23]
QString onlyLetters(const QString &in)
[23]
void examles()
[0]
#define __has_include(x)