QFlags 类

template <typename Enum> class QFlags

QFlags 类提供存储枚举值 OR 组合的类型安全方式。 更多...

头: #include <QFlags>
qmake: QT += core

公共类型

typedef Int
typedef enum_type

公共函数

QFlags (std::initializer_list<Enum> flags )
QFlags (QFlag flag )
QFlags (Enum flags )
QFlags ()
QFlags (const QFlags<T> & other )
QFlags<T> & operator= (const QFlags<T> & other )
QFlags<T> & setFlag (Enum flag , bool on = true)
bool testFlag (Enum flag ) const
QFlags::Int operator QFlags::Int () const
bool operator! () const
QFlags<T> operator& (int mask ) const
QFlags<T> operator& (uint mask ) const
QFlags<T> operator& (Enum other ) const
QFlags<T> & operator&= (int mask )
QFlags<T> & operator&= (uint mask )
QFlags<T> & operator&= (Enum mask )
QFlags<T> operator^ (QFlags<T> other ) const
QFlags<T> operator^ (Enum other ) const
QFlags<T> & operator^= (QFlags<T> other )
QFlags<T> & operator^= (Enum other )
QFlags<T> operator| (QFlags<T> other ) const
QFlags<T> operator| (Enum other ) const
QFlags<T> & operator|= (QFlags<T> other )
QFlags<T> & operator|= (Enum other )
QFlags<T> operator~ () const

Q_DECLARE_FLAGS ( Flags , Enum )
Q_DECLARE_OPERATORS_FOR_FLAGS ( Flags )

详细描述

QFlags<Enum> 类是模板类,其中 Enum 是枚举类型。QFlags 在整个 Qt 中用于存储枚举值的组合。

传统 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 提供类型安全性。例如, 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 { } , is legal:

label->setAlignment(Qt::AlignLeft | Qt::AlignTop);
label->setAlignment({ });
					

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 值。

标志和元对象系统

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_FLAG () macro must be used:

Q_FLAG(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 QFlags:: Int

Typedef for the integer type used for storage as well as for implicit conversion. Either int or 无符号 int , depending on whether the enum's underlying type is signed or unsigned.

该 typedef 在 Qt 5.0 引入。

typedef QFlags:: enum_type

用于枚举模板类型的 Typedef。

成员函数文档编制

QFlags:: QFlags ( std::initializer_list < Enum > flags )

构造 QFlags 对象初始化采用所有 flags combined using the bitwise OR operator.

该函数在 Qt 5.4 引入。

另请参阅 operator|= () 和 operator| ().

QFlags:: QFlags ( QFlag flag )

构造 QFlags 对象初始化采用整数 flag .

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.

QFlags:: QFlags ( Enum flags )

构造 QFlags 对象存储 flags .

QFlags:: QFlags ()

构造没有设置标志的 QFlags 对象。

该函数在 Qt 5.15 引入。

QFlags:: QFlags (const QFlags < T > & other )

构造副本为 other .

QFlags < T > &QFlags:: operator= (const QFlags < T > & other )

赋值 other 到此对象并返回此对象的引用。

QFlags < T > &QFlags:: setFlag ( Enum flag , bool on = true)

Sets the flag flag if on is true or unsets it if on is false 。返回此对象的引用。

该函数在 Qt 5.7 引入。

bool QFlags:: testFlag ( Enum flag ) const

返回 true 若标志 flag 被设置,否则 false .

该函数在 Qt 4.2 引入。

QFlags::Int QFlags:: operator QFlags::Int () const

返回值存储在 QFlags 对象作为整数。

另请参阅 Int .

bool QFlags:: operator! () const

返回 true if no flag is set (i.e., if the value stored by the QFlags object is 0); otherwise returns false .

QFlags < T > QFlags:: operator& ( int mask ) const

返回 QFlags object containing the result of the bitwise AND operation on this object and mask .

另请参阅 operator&= (), operator| (), operator^ (),和 operator~ ().

QFlags < T > QFlags:: operator& ( uint mask ) const

这是重载函数。

QFlags < T > QFlags:: operator& ( Enum other ) const

这是重载函数。

QFlags < T > &QFlags:: operator&= ( int mask )

Performs a bitwise AND operation with mask and stores the result in this QFlags object. Returns a reference to this object.

另请参阅 operator& (), operator|= (),和 operator^= ().

QFlags < T > &QFlags:: operator&= ( uint mask )

这是重载函数。

QFlags < T > &QFlags:: operator&= ( Enum mask )

这是重载函数。

QFlags < T > QFlags:: operator^ ( QFlags < T > other ) const

返回 QFlags object containing the result of the bitwise XOR operation on this object and other .

另请参阅 operator^= (), operator& (), operator| (),和 operator~ ().

QFlags < T > QFlags:: operator^ ( Enum other ) const

这是重载函数。

QFlags < T > &QFlags:: operator^= ( QFlags < T > other )

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 < T > &QFlags:: operator^= ( Enum other )

这是重载函数。

QFlags < T > QFlags:: operator| ( QFlags < T > other ) const

返回 QFlags object containing the result of the bitwise OR operation on this object and other .

另请参阅 operator|= (), operator^ (), operator& (),和 operator~ ().

QFlags < T > QFlags:: operator| ( Enum other ) const

这是重载函数。

QFlags < T > &QFlags:: operator|= ( QFlags < T > other )

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 < T > &QFlags:: operator|= ( Enum other )

这是重载函数。

QFlags < T > QFlags:: operator~ () const

返回 QFlags 对象包含此对象的按位取反。

另请参阅 operator& (), operator| (),和 operator^ ().

宏文档编制

Q_DECLARE_FLAGS ( Flags , Enum )

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 ( Flags )

Q_DECLARE_OPERATORS_FOR_FLAGS() 宏声明全局 operator|() 函数为 Flags , which is of type QFlags <T>.

QFlags 文档编制了解细节。

另请参阅 Q_DECLARE_FLAGS ().