The QFlags class provides a type-safe way of storing OR-combinations of enum values. 更多...
| 頭: | #include <QFlags> |
| typedef | enum_type |
| QFlags (const QFlags & other ) | |
| QFlags (Enum flag ) | |
| QFlags (Zero zero = 0) | |
| QFlags (QFlag value ) | |
| bool | testFlag (Enum flag ) const |
| operator int () const | |
| bool | operator! () const |
| QFlags | operator& (int mask ) const |
| QFlags | operator& (uint mask ) const |
| QFlags | operator& (Enum mask ) const |
| QFlags & | operator&= (int mask ) |
| QFlags & | operator&= (uint mask ) |
| QFlags & | operator= (const QFlags & other ) |
| QFlags | operator^ (QFlags other ) const |
| QFlags | operator^ (Enum other ) const |
| QFlags & | operator^= (QFlags other ) |
| QFlags & | operator^= (Enum other ) |
| QFlags | operator| (QFlags other ) const |
| QFlags | operator| (Enum other ) const |
| QFlags & | operator|= (QFlags other ) |
| QFlags & | operator|= (Enum other ) |
| QFlags | operator~ () const |
| Q_DECLARE_FLAGS ( Flags , Enum ) | |
| Q_DECLARE_OPERATORS_FOR_FLAGS ( Flags ) |
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.
傳統 C++ 存儲枚舉值 OR 組閤的方式是使用
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 使用 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:
label->setAlignment(Qt::AlignLeft | Qt::AlignTop);
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 () 和 Q_DECLARE_OPERATORS_FOR_FLAGS ().
範例:
class MyClass { public: enum Option { NoOptions = 0x0, ShowTabs = 0x1, ShowAll = 0x2, SqueezeBlank = 0x4 }; Q_DECLARE_FLAGS(Options, Option) ... }; Q_DECLARE_OPERATORS_FOR_FLAGS(MyClass::Options)
You can then use the
MyClass::Options
type to store combinations of
MyClass::Option
值。
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:
Q_FLAGS(Options)
A sensible naming convention for enum types and associated
QFlags
types is to give a singular name to the enum type (e.g.,
選項
) and a plural name to the
QFlags
type (e.g.,
選項
). 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
).
另請參閱 QFlag .
用於枚舉模闆類型的 Typedef。
構造副本為 other .
構造 QFlags object storing the given flag .
構造 QFlags object with no flags set. zero must be a literal 0 value.
構造 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.
返迴 true 若 flag is set, otherwise false.
該函數在 Qt 4.2 引入。
返迴值存儲在 QFlags 對象作為整數。
Returns true if no flag is set (i.e., if the value stored by the QFlags object is 0); otherwise returns false.
返迴 QFlags object containing the result of the bitwise AND operation on this object and mask .
另請參閱 operator&= (), operator| (), operator^ (),和 operator~ ().
這是重載函數。
這是重載函數。
Performs a bitwise AND operation with mask and stores the result in this QFlags object. Returns a reference to this object.
另請參閱 operator& (), operator|= (),和 operator^= ().
這是重載函數。
賦值 other 到此對象並返迴此對象的引用。
返迴 QFlags object containing the result of the bitwise XOR operation on this object and other .
另請參閱 operator^= (), operator& (), operator| (),和 operator~ ().
這是重載函數。
Performs a bitwise XOR operation with other and stores the result in this QFlags object. Returns a reference to this object.
另請參閱 operator^ (), operator&= (),和 operator|= ().
這是重載函數。
返迴 QFlags object containing the result of the bitwise OR operation on this object and other .
另請參閱 operator|= (), operator^ (), operator& (),和 operator~ ().
這是重載函數。
Performs a bitwise OR operation with other and stores the result in this QFlags object. Returns a reference to this object.
另請參閱 operator| (), operator&= (),和 operator^= ().
這是重載函數。
返迴 QFlags 對象包含此對象的按位取反。
另請參閱 operator& (), operator| (),和 operator^ ().
The Q_DECLARE_FLAGS() macro expands to
typedef QFlags<Enum> Flags;
Enum is the name of an existing enum type, whereas Flags is the name of the QFlags < Enum > typedef.
見 QFlags 文檔編製瞭解細節。
另請參閱 Q_DECLARE_OPERATORS_FOR_FLAGS ().
Q_DECLARE_OPERATORS_FOR_FLAGS() 宏聲明全局
operator|()
函數為
Flags
, which is of type
QFlags
<T>.
見 QFlags 文檔編製瞭解細節。
另請參閱 Q_DECLARE_FLAGS ().