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
foreach-keyword.qdoc
Go to the documentation of this file.
1
// Copyright (C) 2020 The Qt Company Ltd.
2
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GFDL-1.3-no-invariants-only
3
4
/*!
5
\page foreach-keyword.html
6
\title Qt's foreach Keyword
7
\ingroup groups
8
\ingroup qt-basic-concepts
9
10
\brief Qt's foreach keyword.
11
12
\target foreach-keyword
13
\section1 The foreach Keyword
14
15
\note The foreach keyword was introduced before the C++11 range-based loops
16
existed. New code should prefer C++11 range-based loops.
17
18
The \c foreach keyword is a Qt-specific addition to the C++ language,
19
and is implemented using the preprocessor.
20
21
Its syntax is: \c foreach (\e variable, \e container) \e
22
statement. For example, here's how to use \c foreach to iterate
23
over a QList<QString>:
24
25
\snippet code/doc_src_containers.cpp 15
26
27
The \c foreach code is significantly shorter than the equivalent
28
code that uses iterators:
29
30
\snippet code/doc_src_containers.cpp 16
31
32
Unless the data type contains a comma (e.g., \c{std::pair<int,
33
int>}), the variable used for iteration can be defined within the
34
\c foreach statement:
35
36
\snippet code/doc_src_containers.cpp 17
37
38
And like any other C++ loop construct, you can use braces around
39
the body of a \c foreach loop, and you can use \c break to leave
40
the loop:
41
42
\snippet code/doc_src_containers.cpp 18
43
44
With QMap and QHash, \c foreach accesses the value component of
45
the (key, value) pairs automatically, so you should not call
46
values() on the container (it would generate an unnecessary copy,
47
see below). If you want to iterate over both the keys and the
48
values, you can use iterators (which are faster), or you can
49
obtain the keys, and use them to get the values too:
50
51
\snippet code/doc_src_containers.cpp 19
52
53
For a multi-valued map:
54
55
\snippet code/doc_src_containers.cpp 20
56
57
Qt automatically takes a copy of the container when it enters a
58
\c foreach loop. If you modify the container as you are
59
iterating, that won't affect the loop. (If you do not modify the
60
container, the copy still takes place, but thanks to \l{implicit
61
sharing} copying a container is very fast.)
62
63
Since foreach creates a copy of the container, using a non-const
64
reference for the variable does not allow you to modify the original
65
container. It only affects the copy, which is probably not what you
66
want.
67
68
An alternative to Qt's \c foreach loop is the range-based \c for that is
69
part of C++11 and newer. However, keep in mind that the range-based
70
\c for might force a Qt container to \l{Implicit Sharing}{detach}, whereas
71
\c foreach would not. But using \c foreach always copies the container,
72
which is usually not cheap for STL containers. If in doubt, prefer
73
\c foreach for Qt containers, and range based \c for for STL ones.
74
75
You can remove the availability of the Qt's \c foreach loop by
76
defining the \c{QT_NO_FOREACH} macro.
77
*/
78
79
/*!
80
\macro QT_NO_FOREACH
81
\since 6.0
82
\relates <QtGlobal>
83
84
Defining this macro removes the availability of Qt's \c foreach
85
loop.
86
87
\sa QT_NO_KEYWORDS
88
*/
qtbase
src
corelib
doc
src
foreach-keyword.qdoc
Generated on
for Qt by
1.14.0