6.0.0-rc4Source

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>
CMaketarget_link_libraries(app PRIVATE Nt::Core)
ModuleNt Core C++ Classes

List of all members, including inherited members

Public Types

typedefenum_type
typedefInt

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)
QFlagsoperator|(QFlags o) const
QFlagsoperator|(Enum f) const
QFlagsoperator&(QFlags o) const
QFlagsoperator&(Enum f) const
QFlagsoperator^(QFlags o) const
QFlagsoperator~() const
operator Int() const
booloperator!() const
booltestFlag(Enum f) const
QFlags &setFlag(Enum f, bool on = true)
booltestFlags(QFlags flags) const
booltestAnyFlag(Enum f) const
booltestAnyFlags(QFlags flags) const
InttoInt() const

Static Public Members

QFlagsfromInt(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

[constexpr] QFlags::QFlags()

Constructs an object with no flags set.

[constexpr] QFlags::QFlags(Enum f)

Constructs an object with the single flag f set.

[static, constexpr] QFlags QFlags::fromInt(Int v)

Returns a QFlags object holding the integer v unchanged.

This function was introduced in 6.2.

See also toInt().

[constexpr] QFlags::operator Int() const

Returns the stored flags as an integer.

[constexpr] bool QFlags::operator!() const

Returns true if no flag is set.

[constexpr] QFlags QFlags::operator&(QFlags o) const

Returns the flags set in both this object and o.

[constexpr] QFlags QFlags::operator&(Enum f) const

This is an overloaded function.

Returns f if it is set in this object; otherwise returns no flags.

[constexpr] QFlags & QFlags::operator&=(QFlags o)

Keeps only the flags also set in o and returns a reference to this object.

[constexpr] QFlags & QFlags::operator&=(Enum f)

This is an overloaded function.

Keeps only the flag f and returns a reference to this object.

[constexpr] QFlags QFlags::operator^(QFlags o) const

Returns the flags set in exactly one of this object and o.

[constexpr] QFlags & QFlags::operator^=(QFlags o)

Toggles the flags in o and returns a reference to this object.

[constexpr] QFlags QFlags::operator|(QFlags o) const

Returns the flags set in this object or in o.

[constexpr] QFlags QFlags::operator|(Enum f) const

This is an overloaded function.

Returns the flags set in this object plus f.

[constexpr] QFlags & QFlags::operator|=(QFlags o)

Sets the flags in o and returns a reference to this object.

[constexpr] QFlags & QFlags::operator|=(Enum f)

This is an overloaded function.

Sets the flag f and returns a reference to this object.

[constexpr] QFlags QFlags::operator~() const

Returns the bitwise complement of the stored flags.

[constexpr] 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().

[constexpr] 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().

[constexpr] 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().

[constexpr] 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.

[constexpr] 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().

[constexpr] Int QFlags::toInt() const

Returns the stored flags as an integer.

This function was introduced in 6.2.

See also fromInt().