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