QFlags Class
template <typename Enum> class QFlags
The QFlags class stores OR combinations of enum values in a type-safe way. More…
| Header | #include <QFlags> |
|---|---|
| CMake | target_link_libraries(app PRIVATE Nt::Core) |
| Module | Nt Core C++ Classes |
List of all members, including inherited members
Public Types
Public Functions
| QFlags() | |
| QFlags(Enum f) | |
| QFlags & | operator|=(QFlags o) |
| QFlags & | operator|=(Enum f) |
| QFlags & | operator&=(QFlags o) |
| QFlags & | operator&=(Enum f) |
| QFlags & | operator^=(QFlags o) |
| QFlags | operator|(QFlags o) const |
| QFlags | operator|(Enum f) const |
| QFlags | operator&(QFlags o) const |
| QFlags | operator&(Enum f) const |
| QFlags | operator^(QFlags o) const |
| QFlags | operator~() const |
| operator Int() const | |
| bool | operator!() const |
| bool | testFlag(Enum f) const |
| QFlags & | setFlag(Enum f, bool on = true) |
| bool | testFlags(QFlags flags) const |
| bool | testAnyFlag(Enum f) const |
| bool | testAnyFlags(QFlags flags) const |
| Int | toInt() const |
Static Public Members
| QFlags | fromInt(Int v) |
Detailed Description
Combining plain enum values produces an int, which loses the type and accepts values from any enum. QFlags<Enum> keeps the type, so a function taking QFlags<Enum> rejects unrelated values at compile time.
Declare the flags type next to the enum, then enable the | operator for bare enum values at namespace scope:
class Door {
public:
enum Option { Locked = 0x1, Alarmed = 0x2 };
Q_DECLARE_FLAGS(Options, Option)
};
Q_DECLARE_OPERATORS_FOR_FLAGS(Door::Options)
A value-less flag, such as an enumerator equal to 0, can only be detected with testFlag() when no other flag is set.
Differences from Qt
The stored value is always an int, whatever the underlying type of the enum.
Member Type Documentation
typedef QFlags::enum_type
The enum type this QFlags instantiation wraps.
typedef QFlags::Int
The integer type that stores the flags.
Member Function Documentation
QFlags::QFlags()
Constructs an object with no flags set.
QFlags::QFlags(Enum f)
Constructs an object with the single flag f set.
QFlags QFlags::fromInt(Int v)
Returns a QFlags object holding the integer v unchanged.
This function was introduced in 6.2.
See also toInt().
QFlags::operator Int() const
Returns the stored flags as an integer.
bool QFlags::operator!() const
Returns true if no flag is set.
QFlags QFlags::operator&(QFlags o) const
Returns the flags set in both this object and o.
QFlags QFlags::operator&(Enum f) const
This is an overloaded function.
Returns f if it is set in this object; otherwise returns no flags.
QFlags & QFlags::operator&=(QFlags o)
Keeps only the flags also set in o and returns a reference to this object.
QFlags & QFlags::operator&=(Enum f)
This is an overloaded function.
Keeps only the flag f and returns a reference to this object.
QFlags QFlags::operator^(QFlags o) const
Returns the flags set in exactly one of this object and o.
QFlags & QFlags::operator^=(QFlags o)
Toggles the flags in o and returns a reference to this object.
QFlags QFlags::operator|(QFlags o) const
Returns the flags set in this object or in o.
QFlags QFlags::operator|(Enum f) const
This is an overloaded function.
Returns the flags set in this object plus f.
QFlags & QFlags::operator|=(QFlags o)
Sets the flags in o and returns a reference to this object.
QFlags & QFlags::operator|=(Enum f)
This is an overloaded function.
Sets the flag f and returns a reference to this object.
QFlags QFlags::operator~() const
Returns the bitwise complement of the stored flags.
QFlags & QFlags::setFlag(Enum f, bool on = true)
Sets f if on is true; otherwise clears it. Returns a reference to this object.
See also testFlag().
bool QFlags::testAnyFlag(Enum f) const
Returns true if any bit of f is set; otherwise returns false. A flag with the value 0 is never set.
This function was introduced in 6.2.
See also testFlag() and testAnyFlags().
bool QFlags::testAnyFlags(QFlags flags) const
Returns true if any flag in flags is set; otherwise returns false.
This function was introduced in 6.2.
See also testFlags() and testAnyFlag().
bool QFlags::testFlag(Enum f) const
Returns true if every bit of f is set. If f has the value 0, returns true only when no flag is set.
bool QFlags::testFlags(QFlags flags) const
Returns true if every flag in flags is set. If flags is empty, returns true only when no flag is set.
This function was introduced in 6.2.
See also testFlag() and testAnyFlags().
Int QFlags::toInt() const
Returns the stored flags as an integer.
This function was introduced in 6.2.
See also fromInt().