Qt 4.8
|
The QFlags class provides a type-safe way of storing OR-combinations of enum values. More...
#include <qglobal.h>
Public Types | |
typedef Enum | enum_type |
Typedef for the Enum template type. More... | |
Public Functions | |
Q_DECL_CONSTEXPR QFlags | operator & (int mask) const |
Q_DECL_CONSTEXPR QFlags | operator & (uint mask) const |
Q_DECL_CONSTEXPR QFlags | operator & (Enum f) const |
QFlags & | operator &= (int mask) |
QFlags & | operator &= (uint mask) |
Q_DECL_CONSTEXPR | operator int () const |
Returns the value stored in the QFlags object as an integer. More... | |
Q_DECL_CONSTEXPR bool | operator! () const |
Returns true if no flag is set (i. More... | |
QFlags & | operator= (const QFlags &f) |
Assigns other to this object and returns a reference to this object. More... | |
Q_DECL_CONSTEXPR QFlags | operator^ (QFlags f) const |
Returns a QFlags object containing the result of the bitwise XOR operation on this object and other. More... | |
Q_DECL_CONSTEXPR QFlags | operator^ (Enum f) const |
This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts. More... | |
QFlags & | operator^= (QFlags f) |
Performs a bitwise XOR operation with other and stores the result in this QFlags object. More... | |
QFlags & | operator^= (Enum f) |
This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts. More... | |
Q_DECL_CONSTEXPR QFlags | operator| (QFlags f) const |
Returns a QFlags object containing the result of the bitwise OR operation on this object and other. More... | |
Q_DECL_CONSTEXPR QFlags | operator| (Enum f) const |
This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts. More... | |
QFlags & | operator|= (QFlags f) |
Performs a bitwise OR operation with other and stores the result in this QFlags object. More... | |
QFlags & | operator|= (Enum f) |
This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts. More... | |
Q_DECL_CONSTEXPR QFlags | operator~ () const |
Returns a QFlags object that contains the bitwise negation of this object. More... | |
Q_DECL_CONSTEXPR | QFlags (const QFlags &f) |
Constructs a copy of other. More... | |
Q_DECL_CONSTEXPR | QFlags (Enum f) |
Constructs a QFlags object storing the given flag. More... | |
Q_DECL_CONSTEXPR | QFlags (Zero=0) |
Constructs a QFlags object with no flags set. More... | |
QFlags (QFlag f) | |
Constructs a QFlags object initialized with the given integer value. More... | |
bool | testFlag (Enum f) const |
Returns true if the flag is set, otherwise false. More... | |
Private Types | |
typedef void ** | Zero |
Properties | |
int | i |
The QFlags class provides a type-safe way of storing OR-combinations of enum values.
The QFlags<Enum> class is a template class, where Enum is an enum type. QFlags is used throughout Qt for storing combinations of enum values.
The traditional C++ approach for storing OR-combinations of enum values is to use an int
or uint
variable. The inconvenience with this approach is that there's no type checking at all; any enum value can be OR'd with any other enum value and passed on to a function that takes an int
or uint
.
Qt uses QFlags to provide type safety. For example, the Qt::Alignment type is simply a typedef for QFlags<Qt::AlignmentFlag>. QLabel::setAlignment() takes a Qt::Alignment parameter, which means that any combination of Qt::AlignmentFlag values,or 0, is legal:
If you try to pass a value from another enum or just a plain integer other than 0, the compiler will report an error. If you need to cast integer values to flags in a untyped fashion, you can use the explicit QFlags constructor as cast operator.
If you want to use QFlags for your own enum types, use the Q_DECLARE_FLAGS() and Q_DECLARE_OPERATORS_FOR_FLAGS().
Example:
You can then use the MyClass::Options
type to store combinations of MyClass::Option
values.
The Q_DECLARE_FLAGS() macro does not expose the flags to the meta-object system, so they cannot be used by Qt Script or edited in Qt Designer. To make the flags available for these purposes, the Q_FLAGS() macro must be used:
A sensible naming convention for enum types and associated QFlags types is to give a singular name to the enum type (e.g., Option
) and a plural name to the QFlags type (e.g., Options
). When a singular name is desired for the QFlags type (e.g., Alignment
), you can use Flag
as the suffix for the enum type (e.g., AlignmentFlag
).
Constructs a QFlags object initialized with the given integer value.
The QFlag type is a helper type. By using it here instead of int
, we effectively ensure that arbitrary enum values cannot be cast to a QFlags, whereas untyped enum values (i.e., int
values) can.
Definition at line 2322 of file qglobal.h.
|
inline |
Definition at line 2338 of file qglobal.h.
|
inline |
Definition at line 2339 of file qglobal.h.
|
inline |
Definition at line 2340 of file qglobal.h.
|
inline |
|
inline |
Returns a QFlags object containing the result of the bitwise XOR operation on this object and other.
Definition at line 2336 of file qglobal.h.
This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.
Definition at line 2337 of file qglobal.h.
Performs a bitwise XOR operation with other and stores the result in this QFlags object.
Returns a reference to this object.
Definition at line 2329 of file qglobal.h.
Returns a QFlags object containing the result of the bitwise OR operation on this object and other.
Definition at line 2334 of file qglobal.h.
This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.
Definition at line 2335 of file qglobal.h.
Performs a bitwise OR operation with other and stores the result in this QFlags object.
Returns a reference to this object.
Definition at line 2327 of file qglobal.h.
Returns a QFlags object that contains the bitwise negation of this object.
Definition at line 2341 of file qglobal.h.
|
inline |
Returns true if the flag is set, otherwise false.
Definition at line 2345 of file qglobal.h.
Referenced by QPatternist::PatternPlatform::captureCount(), QPatternist::CurrentItemStore::compress(), QPatternist::AccelTree::copyNodeTo(), QPatternist::ParserContext::hasDeclaration(), operator<<(), QPatternist::PatternPlatform::pattern(), QPatternist::FunctionCall::typeCheck(), QPatternist::LetClause::typeCheck(), and QPatternist::TypeChecker::verifyType().
|
private |
Definition at line 2316 of file qglobal.h.
Referenced by QFlags< Flag >::operator=(), QFlags< Flag >::operator^(), QFlags< Flag >::operator^=(), QFlags< Flag >::operator|(), and QFlags< Flag >::operator|=().