QFlags 類提供存儲枚舉值 OR 組閤的類型安全方式。 更多...
| 頭: |
#include <QFlags>
|
| CMake: |
find_package(Qt6 REQUIRED COMPONENTS Core)
target_link_libraries(mytarget PRIVATE Qt6::Core)
|
| qmake: |
QT += core
|
| Int | |
| enum_type |
| QFlags () | |
| QFlags (Enum flags ) | |
| QFlags (QFlag flag ) | |
| QFlags (std::initializer_list<Enum> flags ) | |
| QFlags (const QFlags<T> & other ) | |
| QFlags<T> & | setFlag (Enum flag , bool on = true) |
(從 6.2 起)
bool
|
testAnyFlag (Enum flag ) const |
(從 6.2 起)
bool
|
testAnyFlags (QFlags<T> flags ) const |
| bool | testFlag (Enum flag ) const |
(從 6.2 起)
bool
|
testFlags (QFlags<T> flags ) const |
(從 6.2 起)
QFlags<T>::Int
|
toInt () const |
| QFlags<T>::Int | operator QFlags<T>::Int () const |
| bool | operator! () const |
| QFlags<T> | operator& (int mask ) const |
| QFlags<T> | operator& (Enum mask ) const |
(從 6.2 起)
QFlags<T>
|
operator& (QFlags<T> mask ) const |
| QFlags<T> | operator& (uint mask ) const |
| QFlags<T> & | operator&= (int mask ) |
| QFlags<T> & | operator&= (Enum mask ) |
(從 6.2 起)
QFlags<T> &
|
operator&= (QFlags<T> mask ) |
| QFlags<T> & | operator&= (uint mask ) |
| int & | operator= (const QFlags<T> & 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| (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 |
(從 6.2 起)
QFlags<T>
|
fromInt (QFlags<T>::Int i ) |
(從 6.2 起)
size_t
|
qHash (QFlags<Enum> key , size_t seed = 0) |
(從 6.2 起)
bool
|
operator!= (Enum lhs , QFlags<T> rhs ) |
(從 6.2 起)
bool
|
operator!= (QFlags<T> lhs , Enum rhs ) |
(從 6.2 起)
bool
|
operator!= (QFlags<T> lhs , QFlags<T> rhs ) |
(從 6.2 起)
bool
|
operator== (Enum lhs , QFlags<T> rhs ) |
(從 6.2 起)
bool
|
operator== (QFlags<T> lhs , Enum rhs ) |
(從 6.2 起)
bool
|
operator== (QFlags<T> lhs , QFlags<T> rhs ) |
| 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
值。
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_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.,
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
).
另請參閱 QFlag .
Typedef for the integer type used for storage as well as for implicit conversion. Either
int
or
unsigned int
, depending on whether the enum's underlying type is signed or unsigned.
用於枚舉模闆類型的 Typedef。
[constexpr noexcept]
QFlags::
QFlags
()
構造沒有設置標誌的 QFlags 對象。
[constexpr noexcept]
QFlags::
QFlags
(
Enum
flags
)
構造 QFlags 對象存儲 flags .
[constexpr noexcept]
QFlags::
QFlags
(
QFlag
flag
)
構造 QFlags 對象初始化采用整數 flag .
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.
[constexpr noexcept]
QFlags::
QFlags
(
std::initializer_list
<
Enum
>
flags
)
構造 QFlags 對象初始化采用所有 flags combined using the bitwise OR operator.
另請參閱 operator|= () 和 operator| ().
[default]
QFlags::
QFlags
(const
QFlags
<
T
> &
other
)
構造副本為 other .
[static constexpr noexcept, since 6.2]
QFlags
<
T
> QFlags::
fromInt
(
QFlags
<
T
>
::Int
i
)
構造 QFlags object representing the integer value i .
該函數在 Qt 6.2 引入。
[constexpr noexcept]
QFlags
<
T
> &QFlags::
setFlag
(
Enum
flag
,
bool
on
= true)
Sets the flag
flag
if
on
is
true
or unsets it if
on
is
false
。返迴此對象的引用。
[constexpr noexcept, since 6.2]
bool
QFlags::
testAnyFlag
(
Enum
flag
) const
返迴
true
if
any
flag set in
flag
is also set in this flags object, otherwise
false
。若
flag
has no flags set, the return will always be
false
.
該函數在 Qt 6.2 引入。
另請參閱 testFlag ().
[constexpr noexcept, since 6.2]
bool
QFlags::
testAnyFlags
(
QFlags
<
T
>
flags
) const
返迴
true
if
any
flag set in
flags
is also set in this flags object, otherwise
false
。若
flags
has no flags set, the return will always be
false
.
該函數在 Qt 6.2 引入。
另請參閱 testFlags ().
[constexpr noexcept]
bool
QFlags::
testFlag
(
Enum
flag
) const
返迴
true
若標誌
flag
被設置,否則
false
.
注意:
if
flag
contains multiple bits set to 1 (for instance, if it's an enumerator equal to the bitwise-OR of other enumerators) then this function will return
true
if and only if all the bits are set in this flags object. On the other hand, if
flag
contains no bits set to 1 (that is, its value as a integer is 0), then this function will return
true
if and only if this flags object also has no bits set to 1.
另請參閱 testAnyFlag ().
[constexpr noexcept, since 6.2]
bool
QFlags::
testFlags
(
QFlags
<
T
>
flags
) const
返迴
true
if this flags object matches the given
flags
.
若 flags has any flags set, this flags object matches precisely if all flags set in flags are also set in this flags object. Otherwise, when flags has no flags set, this flags object only matches if it also has no flags set.
該函數在 Qt 6.2 引入。
另請參閱 testAnyFlags ().
[constexpr noexcept, since 6.2]
QFlags
<
T
>
::Int
QFlags::
toInt
() const
返迴值存儲在 QFlags object as an integer. Note that the returned integer may be signed or unsigned, depending on whether the enum's underlying type is signed or unsigned.
該函數在 Qt 6.2 引入。
另請參閱 Int .
[constexpr noexcept]
QFlags
<
T
>
::Int
QFlags::
operator QFlags<T>::Int
() const
返迴值存儲在 QFlags 對象作為整數。
另請參閱 Int .
[constexpr noexcept]
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
.
[constexpr noexcept]
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~ ().
[constexpr noexcept]
QFlags
<
T
> QFlags::
operator&
(
Enum
mask
) const
這是重載函數。
[constexpr noexcept, since 6.2]
QFlags
<
T
> QFlags::
operator&
(
QFlags
<
T
>
mask
) const
這是重載函數。
該函數在 Qt 6.2 引入。
[constexpr noexcept]
QFlags
<
T
> QFlags::
operator&
(
uint
mask
) const
這是重載函數。
[constexpr noexcept]
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^= ().
[constexpr noexcept]
QFlags
<
T
> &QFlags::
operator&=
(
Enum
mask
)
這是重載函數。
[constexpr noexcept, since 6.2]
QFlags
<
T
> &QFlags::
operator&=
(
QFlags
<
T
>
mask
)
這是重載函數。
該函數在 Qt 6.2 引入。
[constexpr noexcept]
QFlags
<
T
> &QFlags::
operator&=
(
uint
mask
)
這是重載函數。
[default]
int
&QFlags::
operator=
(const
QFlags
<
T
> &
other
)
賦值 other 到此對象並返迴此對象的引用。
[constexpr noexcept]
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~ ().
[constexpr noexcept]
QFlags
<
T
> QFlags::
operator^
(
Enum
other
) const
這是重載函數。
[constexpr noexcept]
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|= ().
[constexpr noexcept]
QFlags
<
T
> &QFlags::
operator^=
(
Enum
other
)
這是重載函數。
[constexpr noexcept]
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~ ().
[constexpr noexcept]
QFlags
<
T
> QFlags::
operator|
(
Enum
other
) const
這是重載函數。
[constexpr noexcept]
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^= ().
[constexpr noexcept]
QFlags
<
T
> &QFlags::
operator|=
(
Enum
other
)
這是重載函數。
[constexpr noexcept]
QFlags
<
T
> QFlags::
operator~
() const
返迴 QFlags 對象包含此對象的按位取反。
另請參閱 operator& (), operator| (),和 operator^ ().
[constexpr noexcept, since 6.2]
bool
operator==
(
Enum
lhs
,
QFlags
<
T
>
rhs
)
[constexpr noexcept, since 6.2]
bool
operator==
(
QFlags
<
T
>
lhs
,
Enum
rhs
)
[constexpr noexcept, since 6.2]
bool
operator==
(
QFlags
<
T
>
lhs
,
QFlags
<
T
>
rhs
)
比較 lhs and rhs for equality; the two arguments are considered equal if they represent exactly the same value (bitmask).
該函數在 Qt 6.2 引入。
[constexpr noexcept, since 6.2]
bool
operator!=
(
Enum
lhs
,
QFlags
<
T
>
rhs
)
[constexpr noexcept, since 6.2]
bool
operator!=
(
QFlags
<
T
>
lhs
,
Enum
rhs
)
[constexpr noexcept, since 6.2]
bool
operator!=
(
QFlags
<
T
>
lhs
,
QFlags
<
T
>
rhs
)
比較 lhs and rhs for inequality; the two arguments are considered different if they don't represent exactly the same value (bitmask).
該函數在 Qt 6.2 引入。
[constexpr noexcept, since 6.2]
template <typename Enum>
size_t
qHash
(
QFlags
<
Enum
>
key
,
size_t
seed
= 0)
返迴哈希值為 key ,使用 seed 做計算種子。
該函數在 Qt 6.2 引入。
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 ().