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