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
qflags.qdoc
Go to the documentation of this file.
1
// Copyright (C) 2022 The Qt Company Ltd.
2
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GFDL-1.3-no-invariants-only
3
4
/*!
5
\class QFlag
6
\inmodule QtCore
7
\brief The QFlag class is a helper data type for QFlags.
8
9
It is equivalent to a plain \c int, except with respect to
10
function overloading and type conversions. You should never need
11
to use this class in your applications.
12
13
\sa QFlags
14
*/
15
16
/*!
17
\fn QFlag::QFlag(int value)
18
19
Constructs a QFlag object that stores the \a value.
20
*/
21
22
/*!
23
\fn QFlag::QFlag(uint value)
24
\since 5.3
25
26
Constructs a QFlag object that stores the \a value.
27
*/
28
29
/*!
30
\fn QFlag::QFlag(short value)
31
\since 5.3
32
33
Constructs a QFlag object that stores the \a value.
34
*/
35
36
/*!
37
\fn QFlag::QFlag(ushort value)
38
\since 5.3
39
40
Constructs a QFlag object that stores the \a value.
41
*/
42
43
/*!
44
\fn QFlag::operator int() const
45
46
Returns the value stored by the QFlag object.
47
*/
48
49
/*!
50
\fn QFlag::operator uint() const
51
\since 5.3
52
53
Returns the value stored by the QFlag object.
54
*/
55
56
/*!
57
\class QFlags
58
\inmodule QtCore
59
\brief The QFlags class provides a type-safe way of storing
60
OR-combinations of enum values.
61
62
63
\ingroup tools
64
65
QFlags<Enum> is a template class where \a Enum is an enumeration type.
66
QFlags is used throughout Qt for storing combinations of enum values.
67
68
The traditional C++ approach for storing OR-combinations of enum
69
values is to use an \c int or \c uint variable. The inconvenience
70
with this approach is that there's no type checking at all; any
71
enum value can be OR'd with any other enum value and passed on to
72
a function that takes an \c int or \c uint.
73
74
Since Qt 6.9, QFlags supports 64-bit enumerations. It is recommended to use
75
explicit (fixed) underlying types when going above 32 bits, to ensure
76
neither the enumeration nor the QFlags type change sizes if an enumerator
77
is removed. Some compilers will also only make the \c enum type larger than
78
32 bits with explicit underlying types.
79
80
Qt uses QFlags to provide type safety. For example, the
81
Qt::Alignment type is simply a typedef for
82
QFlags<Qt::AlignmentFlag>. QLabel::setAlignment() takes a
83
Qt::Alignment parameter, which means that any combination of
84
Qt::AlignmentFlag values, or \c{{ }}, is legal:
85
86
\snippet code/src_corelib_global_qglobal_widgets.cpp 0
87
88
If you try to pass a value from another enum or just a plain
89
integer other than 0, the compiler will report an error. If you
90
need to cast integer values to flags in a untyped fashion, you can
91
use the explicit QFlags constructor as cast operator.
92
93
If you want to use QFlags for your own enum types, use
94
the Q_DECLARE_FLAGS() and Q_DECLARE_OPERATORS_FOR_FLAGS().
95
96
Example:
97
98
\snippet code/src_corelib_global_qglobal.cpp 1
99
100
You can then use the \c MyClass::Options type to store
101
combinations of \c MyClass::Option values.
102
103
\section1 Flags and the Meta-Object System
104
105
The Q_DECLARE_FLAGS() macro does not expose the flags to the meta-object
106
system, so they cannot be used by Qt Script or edited in \QD.
107
To make the flags available for these purposes, the Q_FLAG() macro must
108
be used:
109
110
\snippet code/src_corelib_global_qglobal.cpp meta-object flags
111
112
\section1 Naming Convention
113
114
A sensible naming convention for enum types and associated QFlags
115
types is to give a singular name to the enum type (e.g., \c
116
Option) and a plural name to the QFlags type (e.g., \c Options).
117
When a singular name is desired for the QFlags type (e.g., \c
118
Alignment), you can use \c Flag as the suffix for the enum type
119
(e.g., \c AlignmentFlag).
120
*/
121
122
/*!
123
\typedef QFlags::Int
124
\since 5.0
125
126
Typedef for the integer type used for storage as well as for
127
implicit conversion. Either \c qintXX or \c quintXX, depending on
128
whether the enum's underlying type is signed or unsigned and, since Qt 6.9,
129
the enum's size. Typically, it will be \c qint32 (\c{int}) or \c quint32
130
(\c{unsigned}).
131
*/
132
133
/*!
134
\typedef QFlags::enum_type
135
136
Typedef for the Enum template type.
137
*/
138
139
/*!
140
\fn template<typename Enum> QFlags<Enum>::QFlags(const QFlags &other)
141
142
Constructs a copy of \a other.
143
*/
144
145
/*!
146
\fn template <typename Enum> QFlags<Enum>::QFlags(Enum flags)
147
148
Constructs a QFlags object storing the \a flags.
149
*/
150
151
/*!
152
\fn template <typename Enum> QFlags<Enum>::QFlags()
153
\since 5.15
154
155
Constructs a QFlags object with no flags set.
156
*/
157
158
/*!
159
\fn template <typename Enum> QFlags<Enum>::QFlags(QFlag flag)
160
161
Constructs a QFlags object initialized with the integer \a flag.
162
163
The QFlag type is a helper type. By using it here instead of \c
164
int, we effectively ensure that arbitrary enum values cannot be
165
cast to a QFlags, whereas untyped enum values (i.e., \c int
166
values) can.
167
168
This constructor is only present for 32-bit \c Enum types. To support all
169
enum sizes, consider the constructor using \c{std::in_place_t}.
170
*/
171
172
/*!
173
\fn template <typename Enum> QFlags<Enum>::QFlags(std::in_place_t, Int flags)
174
\since 6.9
175
176
Constructs a QFlags object initialized with the integer \a flags.
177
*/
178
179
/*!
180
\fn template <typename Enum> QFlags<Enum>::QFlags(std::initializer_list<Enum> flags)
181
\since 5.4
182
183
Constructs a QFlags object initialized with all \a flags
184
combined using the bitwise OR operator.
185
186
\sa operator|=(), operator|()
187
*/
188
189
/*!
190
\fn template <typename Enum> QFlags &QFlags<Enum>::operator=(const QFlags &other)
191
192
Assigns \a other to this object and returns a reference to this
193
object.
194
*/
195
196
/*!
197
\fn template <typename Enum> QFlags &QFlags<Enum>::operator&=(int mask)
198
199
Performs a bitwise AND operation with \a mask and stores the
200
result in this QFlags object. Returns a reference to this object.
201
202
//! [unsafe-integer]
203
This operator is disabled if the \c{QT_TYPESAFE_FLAGS} macro is defined.
204
Note that it is not extended to 64-bit for 64-bit QFlags either: for 64-bit
205
support, use the type-safe overload.
206
//! [unsafe-integer]
207
208
\sa operator&(), operator|=(), operator^=()
209
*/
210
211
/*!
212
\fn template <typename Enum> QFlags &QFlags<Enum>::operator&=(uint mask)
213
214
\overload
215
\include qflags.qdoc unsafe-integer
216
*/
217
218
/*!
219
\fn template <typename Enum> QFlags &QFlags<Enum>::operator&=(Enum mask)
220
221
\overload
222
*/
223
224
/*!
225
\fn template <typename Enum> QFlags &QFlags<Enum>::operator&=(QFlags mask)
226
\since 6.2
227
228
\overload
229
*/
230
231
/*!
232
\fn template <typename Enum> QFlags &QFlags<Enum>::operator|=(QFlags other)
233
234
Performs a bitwise OR operation with \a other and stores the
235
result in this QFlags object. Returns a reference to this object.
236
237
\sa operator|(), operator&=(), operator^=()
238
*/
239
240
/*!
241
\fn template <typename Enum> QFlags &QFlags<Enum>::operator|=(Enum other)
242
243
\overload
244
*/
245
246
/*!
247
\fn template <typename Enum> QFlags &QFlags<Enum>::operator^=(QFlags other)
248
249
Performs a bitwise XOR operation with \a other and stores the
250
result in this QFlags object. Returns a reference to this object.
251
252
\sa operator^(), operator&=(), operator|=()
253
*/
254
255
/*!
256
\fn template <typename Enum> QFlags &QFlags<Enum>::operator^=(Enum other)
257
258
\overload
259
*/
260
261
/*!
262
\fn template <typename Enum> QFlags<Enum>::operator Int() const
263
264
Returns the value stored in the QFlags object as an integer.
265
266
\sa Int
267
*/
268
269
/*!
270
\fn template <typename Enum> QFlags QFlags<Enum>::operator|(QFlags other) const
271
272
Returns a QFlags object containing the result of the bitwise OR
273
operation on this object and \a other.
274
275
\sa operator|=(), operator^(), operator&(), operator~()
276
*/
277
278
/*!
279
\fn template <typename Enum> QFlags QFlags<Enum>::operator|(Enum other) const
280
281
\overload
282
*/
283
284
/*!
285
\fn template <typename Enum> QFlags QFlags<Enum>::operator^(QFlags other) const
286
287
Returns a QFlags object containing the result of the bitwise XOR
288
operation on this object and \a other.
289
290
\sa operator^=(), operator&(), operator|(), operator~()
291
*/
292
293
/*!
294
\fn template <typename Enum> QFlags QFlags<Enum>::operator^(Enum other) const
295
296
\overload
297
*/
298
299
/*!
300
\fn template <typename Enum> QFlags QFlags<Enum>::operator&(int mask) const
301
302
Returns a QFlags object containing the result of the bitwise AND
303
operation on this object and \a mask.
304
305
\include qflags.qdoc unsafe-integer
306
307
\sa operator&=(), operator|(), operator^(), operator~()
308
*/
309
310
/*!
311
\fn template <typename Enum> QFlags QFlags<Enum>::operator&(uint mask) const
312
313
\overload
314
\include qflags.qdoc unsafe-integer
315
*/
316
317
/*!
318
\fn template <typename Enum> QFlags QFlags<Enum>::operator&(Enum mask) const
319
320
\overload
321
*/
322
323
/*!
324
\fn template <typename Enum> QFlags QFlags<Enum>::operator&(QFlags mask) const
325
\since 6.2
326
327
\overload
328
*/
329
330
/*!
331
\fn template <typename Enum> QFlags QFlags<Enum>::operator~() const
332
333
Returns a QFlags object that contains the bitwise negation of
334
this object.
335
336
\sa operator&(), operator|(), operator^()
337
*/
338
339
/*!
340
\fn template <typename Enum> bool QFlags<Enum>::operator!() const
341
342
Returns \c true if no flag is set (i.e., if the value stored by the
343
QFlags object is 0); otherwise returns \c false.
344
*/
345
346
/*!
347
\fn template <typename Enum> bool QFlags<Enum>::testFlag(Enum flag) const
348
\since 4.2
349
350
Returns \c true if the flag \a flag is set, otherwise \c false.
351
352
\note if \a flag contains multiple bits set to 1 (for instance, if
353
it's an enumerator equal to the bitwise-OR of other enumerators)
354
then this function will return \c true if and only if all the bits
355
are set in this flags object. On the other hand, if \a flag contains
356
no bits set to 1 (that is, its value as a integer is 0), then this
357
function will return \c true if and only if this flags object also
358
has no bits set to 1.
359
360
\sa testAnyFlag()
361
*/
362
363
/*!
364
\fn template <typename Enum> bool QFlags<Enum>::testFlags(QFlags flags) const noexcept
365
\since 6.2
366
367
Returns \c true if this flags object matches the given \a flags.
368
369
If \a flags has any flags set, this flags object matches precisely
370
if all flags set in \a flags are also set in this flags object.
371
Otherwise, when \a flags has no flags set, this flags object only
372
matches if it also has no flags set.
373
374
\sa testAnyFlags()
375
*/
376
377
/*!
378
\fn template <typename Enum> bool QFlags<Enum>::testAnyFlag(Enum flag) const noexcept
379
\since 6.2
380
381
Returns \c true if \b any flag set in \a flag is also set in this
382
flags object, otherwise \c false. If \a flag has no flags set, the
383
return will always be \c false.
384
385
\sa testFlag()
386
*/
387
388
/*!
389
\fn template <typename Enum> bool QFlags<Enum>::testAnyFlags(QFlags flags) const noexcept
390
\since 6.2
391
392
Returns \c true if \b any flag set in \a flags is also set in this
393
flags object, otherwise \c false. If \a flags has no flags set, the
394
return will always be \c false.
395
396
\sa testFlags()
397
*/
398
399
/*!
400
\fn template <typename Enum> QFlags QFlags<Enum>::setFlag(Enum flag, bool on)
401
\since 5.7
402
403
Sets the flag \a flag if \a on is \c true or unsets it if
404
\a on is \c false. Returns a reference to this object.
405
*/
406
407
/*!
408
\fn template <typename Enum> QFlags<Enum> QFlags<Enum>::fromInt(Int i) noexcept
409
\since 6.2
410
411
Constructs a QFlags object representing the integer value \a i.
412
*/
413
414
/*!
415
\fn template <typename Enum> Int QFlags<Enum>::toInt() const noexcept
416
\since 6.2
417
418
Returns the value stored in the QFlags object as an integer. Note
419
that the returned integer may be signed or unsigned, depending on
420
whether the enum's underlying type is signed or unsigned.
421
422
\sa Int
423
*/
424
425
/*!
426
\fn template <typename Enum> size_t qHash(QFlags<Enum> key, size_t seed)
427
\since 6.2
428
\qhashold{QFlags}
429
*/
430
431
/*!
432
\fn template <typename Enum> bool QFlags<Enum>::operator==(QFlags<Enum> lhs, QFlags<Enum> rhs)
433
\fn template <typename Enum> bool QFlags<Enum>::operator==(QFlags<Enum> lhs, Enum rhs)
434
\fn template <typename Enum> bool QFlags<Enum>::operator==(Enum lhs, QFlags<Enum> rhs)
435
\since 6.2
436
437
Compares \a lhs and \a rhs for equality; the two arguments are
438
considered equal if they represent exactly the same value
439
(bitmask).
440
*/
441
442
/*!
443
\fn template <typename Enum> bool QFlags<Enum>::operator!=(QFlags<Enum> lhs, QFlags<Enum> rhs)
444
\fn template <typename Enum> bool QFlags<Enum>::operator!=(QFlags<Enum> lhs, Enum rhs)
445
\fn template <typename Enum> bool QFlags<Enum>::operator!=(Enum lhs, QFlags<Enum> rhs)
446
\since 6.2
447
448
Compares \a lhs and \a rhs for inequality; the two arguments are
449
considered different if they don't represent exactly the same value
450
(bitmask).
451
*/
452
453
/*!
454
\macro Q_DECLARE_FLAGS(Flags, Enum)
455
\relates QFlags
456
457
The Q_DECLARE_FLAGS() macro expands to
458
459
\snippet code/src_corelib_global_qglobal.cpp 2
460
461
\a Enum is the name of an existing enum type, whereas \a Flags is
462
the name of the QFlags<\e{Enum}> typedef.
463
464
See the QFlags documentation for details.
465
466
\sa Q_DECLARE_OPERATORS_FOR_FLAGS()
467
*/
468
469
/*!
470
\macro Q_DECLARE_OPERATORS_FOR_FLAGS(Flags)
471
\relates QFlags
472
473
The Q_DECLARE_OPERATORS_FOR_FLAGS() macro declares global
474
bitwise operator functions \c operator|(), \c operator&(),
475
\c operator^(), \c operator~() and their assignment forms:
476
&=, |=, and ^=. for \a Flags, which is of type QFlags<T>.
477
478
See the QFlags documentation for details.
479
480
\sa Q_DECLARE_FLAGS()
481
*/
qtbase
src
corelib
global
qflags.qdoc
Generated on
for Qt by
1.16.1