Qt
Internal/Contributor docs for the Qt SDK. Note: These are NOT official API docs; those are found at https://doc.qt.io/
Loading...
Searching...
No Matches
QMetaEnum Class Reference

\inmodule QtCore More...

#include <qmetaobject.h>

Collaboration diagram for QMetaEnum:

Public Member Functions

constexpr QMetaEnum ()
const char * name () const
 Returns the name of the type (without the scope).
const char * enumName () const
 Returns the enum name of the flag (without the scope).
QMetaType metaType () const
 Returns the meta type of the enum.
bool isFlag () const
 Returns true if this enumerator is used as a flag; otherwise returns false.
bool isScoped () const
bool is64Bit () const
int keyCount () const
 Returns the number of keys.
const char * key (int index) const
 Returns the key with the given index, or \nullptr if no such key exists.
int value (int index) const
 Returns the value with the given index; or returns -1 if there is no such value.
std::optional< quint64value64 (int index) const
const char * scope () const
 Returns the scope this enumerator was declared in.
int keyToValue (const char *key, bool *ok=nullptr) const
 Returns the integer value of the given enumeration key, or -1 if key is not defined.
int keysToValue (const char *keys, bool *ok=nullptr) const
 Returns the value derived from combining together the values of the keys using the OR operator, or -1 if keys is not defined.
std::optional< quint64keyToValue64 (const char *key) const
std::optional< quint64keysToValue64 (const char *keys) const
 Returns the value derived from combining together the values of the keys using the OR operator, or std::nullopt if keys is not defined.
const char * valueToKey (quint64 value) const
 Returns the string that is used as the name of the given enumeration value, or \nullptr if value is not defined.
QByteArray valueToKeys (quint64 value) const
 Returns a byte array of '|'-separated keys that represents the given value.
const QMetaObjectenclosingMetaObject () const
bool isValid () const
 Returns true if this enum is valid (has a name); otherwise returns false.
template<typename... Mode>
quint64 value_helper (uint index, Mode... modes) const noexcept

Static Public Member Functions

template<typename T>
static QMetaEnum fromType ()

Friends

struct QMetaObject
struct QMetaObjectPrivate

Detailed Description

\inmodule QtCore

The QMetaEnum class provides meta-data about an enumerator.

Use name() for the enumerator's name. The enumerator's keys (names of each enumerated item) are returned by key(); use keyCount() to find the number of keys. isFlag() returns whether the enumerator is meant to be used as a flag, meaning that its values can be combined using the OR operator.

The conversion functions keyToValue(), valueToKey(), keysToValue(), and valueToKeys() allow conversion between the integer representation of an enumeration or set value and its literal representation. The scope() function returns the class scope this enumerator was declared in.

To use QMetaEnum functionality, register the enumerator within the meta-object system using the Q_ENUM macro.

enum AppleType {
Big,
};
Q_ENUM(AppleType)
qDebug() << metaEnum.valueToKey(ModelApple::Big);
\inmodule QtCore
static QMetaEnum fromType()
#define qDebug
Definition qlogging.h:168
@ Small
#define Q_ENUM(x)
QMetaEnum metaEnum(const char *name)
See also
QMetaObject, QMetaMethod, QMetaProperty

Definition at line 269 of file qmetaobject.h.

Constructor & Destructor Documentation

◆ QMetaEnum()

QMetaEnum::QMetaEnum ( )
inlineconstexpr

Definition at line 272 of file qmetaobject.h.

Member Function Documentation

◆ enclosingMetaObject()

const QMetaObject * QMetaEnum::enclosingMetaObject ( ) const
inline

Definition at line 300 of file qmetaobject.h.

◆ enumName()

const char * QMetaEnum::enumName ( ) const

Returns the enum name of the flag (without the scope).

For example, the Qt::AlignmentFlag flag has AlignmentFlag as the enum name, but Alignment as the type name. Non flag enums has the same type and enum names.

Enum names have the same scope as the type name.

Since
5.12
See also
isValid(), name()

Definition at line 3225 of file qmetaobject.cpp.

◆ fromType()

template<typename T>
QMetaEnum QMetaEnum::fromType ( )
inlinestatic
Since
5.5

Returns the QMetaEnum corresponding to the type in the template parameter. The enum needs to be declared with Q_ENUM.

Definition at line 305 of file qmetaobject.h.

◆ is64Bit()

bool QMetaEnum::is64Bit ( ) const
Since
6.9

Returns true if the underlying type of this enumeration is 64 bits wide.

See also
value64()

Definition at line 3411 of file qmetaobject.cpp.

◆ isFlag()

bool QMetaEnum::isFlag ( ) const

Returns true if this enumerator is used as a flag; otherwise returns false.

When used as flags, enumerators can be combined using the OR operator.

See also
keysToValue(), valueToKeys()

Definition at line 3384 of file qmetaobject.cpp.

◆ isScoped()

bool QMetaEnum::isScoped ( ) const
Since
5.8

Returns true if this enumerator is declared as a C++11 enum class; otherwise returns false.

Definition at line 3397 of file qmetaobject.cpp.

◆ isValid()

bool QMetaEnum::isValid ( ) const
inline

Returns true if this enum is valid (has a name); otherwise returns false.

See also
name()

Definition at line 302 of file qmetaobject.h.

◆ key()

const char * QMetaEnum::key ( int index) const

Returns the key with the given index, or \nullptr if no such key exists.

See also
keyCount(), value(), valueToKey()

Definition at line 3275 of file qmetaobject.cpp.

◆ keyCount()

int QMetaEnum::keyCount ( ) const

Returns the number of keys.

See also
key()

Definition at line 3263 of file qmetaobject.cpp.

◆ keysToValue()

int QMetaEnum::keysToValue ( const char * keys,
bool * ok = nullptr ) const

Returns the value derived from combining together the values of the keys using the OR operator, or -1 if keys is not defined.

Note that the strings in keys must be '|'-separated.

If keys is not defined, *{ok} is set to false; otherwise {ok} is set to true.

If this is a 64-bit enumeration (see is64Bit()), this function returns the low 32-bit portion of the value. Use keyToValue64() to obtain the full value instead.

See also
keysToValue64(), isFlag(), valueToKey(), valueToKeys(), is64Bit()

Definition at line 3590 of file qmetaobject.cpp.

◆ keysToValue64()

std::optional< quint64 > QMetaEnum::keysToValue64 ( const char * keys) const

Returns the value derived from combining together the values of the keys using the OR operator, or std::nullopt if keys is not defined.

Note that the strings in keys must be '|'-separated.

qmetaenum-32bit-signextend-64bit

See also
isFlag(), valueToKey(), valueToKeys()

Definition at line 3607 of file qmetaobject.cpp.

◆ keyToValue()

int QMetaEnum::keyToValue ( const char * key,
bool * ok = nullptr ) const

Returns the integer value of the given enumeration key, or -1 if key is not defined.

If key is not defined, *{ok} is set to false; otherwise {ok} is set to true.

For flag types, use keysToValue().

If this is a 64-bit enumeration (see is64Bit()), this function returns the low 32-bit portion of the value. Use keyToValue64() to obtain the full value instead.

See also
keyToValue64, valueToKey(), isFlag(), keysToValue(), is64Bit()

Definition at line 3473 of file qmetaobject.cpp.

◆ keyToValue64()

std::optional< quint64 > QMetaEnum::keyToValue64 ( const char * key) const
Since
6.9

Returns the integer value of the given enumeration key, or std::nullopt if key is not defined.

For flag types, use keysToValue64().

qmetaenum-32bit-signextend-64bit

See also
valueToKey(), isFlag(), keysToValue64()

Definition at line 3493 of file qmetaobject.cpp.

◆ metaType()

QMetaType QMetaEnum::metaType ( ) const

Returns the meta type of the enum.

If the QMetaObject that this enum is part of was generated with Qt 6.5 or earlier, this will be an invalid meta type.

Note
This is the meta type of the enum itself, not of its underlying integral type. You can retrieve the meta type of the underlying type of the enum using \l{QMetaType::underlyingType()}.
Since
6.6

Definition at line 3244 of file qmetaobject.cpp.

◆ name()

const char * QMetaEnum::name ( ) const

Returns the name of the type (without the scope).

For example, the Qt::Key enumeration has Key as the type name and \l Qt as the scope.

For flags this returns the name of the flag type, not the name of the enum type.

See also
isValid(), scope(), enumName()

Definition at line 3206 of file qmetaobject.cpp.

◆ scope()

const char * QMetaEnum::scope ( ) const

Returns the scope this enumerator was declared in.

For example, the Qt::AlignmentFlag enumeration has Qt as the scope and AlignmentFlag as the name.

See also
name()

Definition at line 3426 of file qmetaobject.cpp.

◆ value()

int QMetaEnum::value ( int index) const

Returns the value with the given index; or returns -1 if there is no such value.

If this is an enumeration with a 64-bit underlying type (see is64Bit()), this function returns the low 32-bit portion of the value. Use value64() to obtain the full value instead.

See also
value64(), keyCount(), key(), keyToValue()

Definition at line 3294 of file qmetaobject.cpp.

◆ value64()

std::optional< quint64 > QMetaEnum::value64 ( int index) const
\since 6.9

Returns the value with the given \a index if it exists; or returns a
\c{std::nullopt} if it doesn't.

! [qmetaenum-32bit-signextend-64bit] This function always sign-extends the value of 32-bit enumerations to 64 bits, if their underlying type is signed (e.g., int, short). In most cases, this is the expected behavior.

A notable exception is for flag values that have bit 31 set, like 0x8000'0000, because some compilers (such as Microsoft Visual Studio), do not automatically switch to an unsigned underlying type. To avoid this problem, explicitly specify the underlying type in the enum declaration.

Note
For QMetaObjects compiled prior to Qt 6.6, this function always sign-extends. ! [qmetaenum-32bit-signextend-64bit]
\sa keyCount(), key(), keyToValue(), is64Bit()

Definition at line 3365 of file qmetaobject.cpp.

◆ value_helper()

template<typename... Mode>
quint64 QMetaEnum::value_helper ( uint index,
Mode... modes ) const
inlinenoexcept

Definition at line 3327 of file qmetaobject.cpp.

◆ valueToKey()

const char * QMetaEnum::valueToKey ( quint64 value) const

Returns the string that is used as the name of the given enumeration value, or \nullptr if value is not defined.

For flag types, use valueToKeys().

See also
isFlag(), valueToKeys()

Definition at line 3515 of file qmetaobject.cpp.

◆ valueToKeys()

QByteArray QMetaEnum::valueToKeys ( quint64 value) const

Returns a byte array of '|'-separated keys that represents the given value.

Note
Passing a 64-bit value to an enumeration whose underlying type is 32-bit (that is, if is64Bit() returns false) results in an empty string being returned.
See also
isFlag(), valueToKey(), keysToValue()

Definition at line 3670 of file qmetaobject.cpp.

◆ QMetaObject

friend struct QMetaObject
friend

Definition at line 333 of file qmetaobject.h.

◆ QMetaObjectPrivate

friend struct QMetaObjectPrivate
friend

Definition at line 334 of file qmetaobject.h.


The documentation for this class was generated from the following files: