1// Copyright (C) 2022 The Qt Company Ltd.
2// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GFDL-1.3-no-invariants-only
7 \brief The QFlag class is a helper data type for QFlags.
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.
17 \fn QFlag::QFlag(int value)
19 Constructs a QFlag object that stores the \a value.
23 \fn QFlag::QFlag(uint value)
26 Constructs a QFlag object that stores the \a value.
30 \fn QFlag::QFlag(short value)
33 Constructs a QFlag object that stores the \a value.
37 \fn QFlag::QFlag(ushort value)
40 Constructs a QFlag object that stores the \a value.
44 \fn QFlag::operator int() const
46 Returns the value stored by the QFlag object.
50 \fn QFlag::operator uint() const
53 Returns the value stored by the QFlag object.
59 \brief The QFlags class provides a type-safe way of storing
60 OR-combinations of enum values.
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
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.
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:
81 \snippet code/src_corelib_global_qglobal.cpp 0
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.
88 If you want to use QFlags for your own enum types, use
89 the Q_DECLARE_FLAGS() and Q_DECLARE_OPERATORS_FOR_FLAGS().
93 \snippet code/src_corelib_global_qglobal.cpp 1
95 You can then use the \c MyClass::Options type to store
96 combinations of \c MyClass::Option values.
98 \section1 Flags and the Meta-Object System
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 Qt Designer.
102 To make the flags available for these purposes, the Q_FLAG() macro must
105 \snippet code/src_corelib_global_qglobal.cpp meta-object flags
107 \section1 Naming Convention
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).
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.
129 \typedef QFlags::enum_type
131 Typedef for the Enum template type.
135 \fn template<typename Enum> QFlags<Enum>::QFlags(const QFlags &other)
137 Constructs a copy of \a other.
141 \fn template <typename Enum> QFlags<Enum>::QFlags(Enum flags)
143 Constructs a QFlags object storing the \a flags.
147 \fn template <typename Enum> QFlags<Enum>::QFlags()
150 Constructs a QFlags object with no flags set.
154 \fn template <typename Enum> QFlags<Enum>::QFlags(QFlag flag)
156 Constructs a QFlags object initialized with the integer \a flag.
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
165 \fn template <typename Enum> QFlags<Enum>::QFlags(std::initializer_list<Enum> flags)
168 Constructs a QFlags object initialized with all \a flags
169 combined using the bitwise OR operator.
171 \sa operator|=(), operator|()
175 \fn template <typename Enum> QFlags &QFlags<Enum>::operator=(const QFlags &other)
177 Assigns \e other to this object and returns a reference to this
182 \fn template <typename Enum> QFlags &QFlags<Enum>::operator&=(int mask)
184 Performs a bitwise AND operation with \a mask and stores the
185 result in this QFlags object. Returns a reference to this object.
187 \sa operator&(), operator|=(), operator^=()
191 \fn template <typename Enum> QFlags &QFlags<Enum>::operator&=(uint mask)
197 \fn template <typename Enum> QFlags &QFlags<Enum>::operator&=(Enum mask)
203 \fn template <typename Enum> QFlags &QFlags<Enum>::operator&=(QFlags mask)
210 \fn template <typename Enum> QFlags &QFlags<Enum>::operator|=(QFlags other)
212 Performs a bitwise OR operation with \a other and stores the
213 result in this QFlags object. Returns a reference to this object.
215 \sa operator|(), operator&=(), operator^=()
219 \fn template <typename Enum> QFlags &QFlags<Enum>::operator|=(Enum other)
225 \fn template <typename Enum> QFlags &QFlags<Enum>::operator^=(QFlags other)
227 Performs a bitwise XOR operation with \a other and stores the
228 result in this QFlags object. Returns a reference to this object.
230 \sa operator^(), operator&=(), operator|=()
234 \fn template <typename Enum> QFlags &QFlags<Enum>::operator^=(Enum other)
240 \fn template <typename Enum> QFlags<Enum>::operator Int() const
242 Returns the value stored in the QFlags object as an integer.
248 \fn template <typename Enum> QFlags QFlags<Enum>::operator|(QFlags other) const
250 Returns a QFlags object containing the result of the bitwise OR
251 operation on this object and \a other.
253 \sa operator|=(), operator^(), operator&(), operator~()
257 \fn template <typename Enum> QFlags QFlags<Enum>::operator|(Enum other) const
263 \fn template <typename Enum> QFlags QFlags<Enum>::operator^(QFlags other) const
265 Returns a QFlags object containing the result of the bitwise XOR
266 operation on this object and \a other.
268 \sa operator^=(), operator&(), operator|(), operator~()
272 \fn template <typename Enum> QFlags QFlags<Enum>::operator^(Enum other) const
278 \fn template <typename Enum> QFlags QFlags<Enum>::operator&(int mask) const
280 Returns a QFlags object containing the result of the bitwise AND
281 operation on this object and \a mask.
283 \sa operator&=(), operator|(), operator^(), operator~()
287 \fn template <typename Enum> QFlags QFlags<Enum>::operator&(uint mask) const
293 \fn template <typename Enum> QFlags QFlags<Enum>::operator&(Enum mask) const
299 \fn template <typename Enum> QFlags QFlags<Enum>::operator&(QFlags mask) const
306 \fn template <typename Enum> QFlags QFlags<Enum>::operator~() const
308 Returns a QFlags object that contains the bitwise negation of
311 \sa operator&(), operator|(), operator^()
315 \fn template <typename Enum> bool QFlags<Enum>::operator!() const
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.
322 \fn template <typename Enum> bool QFlags<Enum>::testFlag(Enum flag) const
325 Returns \c true if the flag \a flag is set, otherwise \c false.
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.
339 \fn template <typename Enum> bool QFlags<Enum>::testFlags(QFlags flags) const noexcept
342 Returns \c true if this flags object matches the given \a flags.
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.
353 \fn template <typename Enum> bool QFlags<Enum>::testAnyFlag(Enum flag) const noexcept
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.
364 \fn template <typename Enum> bool QFlags<Enum>::testAnyFlags(QFlags flags) const noexcept
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.
375 \fn template <typename Enum> QFlags QFlags<Enum>::setFlag(Enum flag, bool on)
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.
383 \fn template <typename Enum> QFlags<Enum> QFlags<Enum>::fromInt(Int i) noexcept
386 Constructs a QFlags object representing the integer value \a i.
390 \fn template <typename Enum> Int QFlags<Enum>::toInt() const noexcept
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.
401 \fn template <typename Enum> size_t qHash(QFlags<Enum> flags, size_t seed = 0) noexcept
405 Calculates the hash for the flags \a flags, using \a seed
406 to seed the calculation.
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)
416 Compares \a lhs and \a rhs for equality; the two arguments are
417 considered equal if they represent exactly the same value
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)
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
434 \macro Q_DECLARE_FLAGS(Flags, Enum)
437 The Q_DECLARE_FLAGS() macro expands to
439 \snippet code/src_corelib_global_qglobal.cpp 2
441 \a Enum is the name of an existing enum type, whereas \a Flags is
442 the name of the QFlags<\e{Enum}> typedef.
444 See the QFlags documentation for details.
446 \sa Q_DECLARE_OPERATORS_FOR_FLAGS()
450 \macro Q_DECLARE_OPERATORS_FOR_FLAGS(Flags)
453 The Q_DECLARE_OPERATORS_FOR_FLAGS() macro declares global \c
454 operator|() functions for \a Flags, which is of type QFlags<T>.
456 See the QFlags documentation for details.
458 \sa Q_DECLARE_FLAGS()