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