- Qt 6.8
- Qt Core
- C++ Classes
- QMetaType
- Qt 6.8.1 Reference Documentation

# QMetaType Class

The QMetaType class manages named types in the meta-object system. More...

Header: | `#include <QMetaType>`
CMake: | `find_package(Qt6 REQUIRED COMPONENTS Core)` `target_link_libraries(mytarget PRIVATE Qt6::Core)`
qmake: | `QT += core`

- List of all members, including inherited members
- Deprecated members

This class is equality-comparable.

Note: All functions in this class are thread-safe.

## Public Types

enum | Type { Void, Bool, Int, UInt, Double, …, UnknownType }
enum | TypeFlag { NeedsConstruction, NeedsCopyConstruction, NeedsMoveConstruction, NeedsDestruction, RelocatableType, …, IsConst }
flags | TypeFlags

## Public Functions

`(since 6.0)` | QMetaType()
| QMetaType(int typeId)
`(since 6.0)` qsizetype | alignOf() const
`(since 6.0)` QPartialOrdering | compare(const void *lhs, const void *rhs) const
void * | construct(void *where, const void *copy = nullptr) const
void * | create(const void *copy = nullptr) const
bool | debugStream(QDebug &dbg, const void *rhs)
void | destroy(void *data) const
void | destruct(void *data) const
`(since 6.0)` bool | equals(const void *lhs, const void *rhs) const
QMetaType::TypeFlags | flags() const
`(since 6.1)` bool | hasRegisteredDataStreamOperators() const
`(since 6.0)` bool | hasRegisteredDebugStreamOperator() const
int | id() const
`(since 6.5)` bool | isCopyConstructible() const
`(since 6.5)` bool | isDefaultConstructible() const
`(since 6.5)` bool | isDestructible() const
bool | isEqualityComparable() const
`(since 6.5)` bool | isMoveConstructible() const
bool | isOrdered() const
bool | isRegistered() const
bool | isValid() const
bool | load(QDataStream &stream, void *data) const
const QMetaObject * | metaObject() const
const char * | name() const
`(since 6.5)` void | registerType() const
bool | save(QDataStream &stream, const void *data) const
qsizetype | sizeOf() const
`(since 6.6)` QMetaType | underlyingType() const

## Static Public Members

bool | canConvert(QMetaType fromType, QMetaType toType)
bool | canView(QMetaType fromType, QMetaType toType)
bool | convert(QMetaType fromType, const void *from, QMetaType toType, void *to)
QMetaType | fromName(QByteArrayView typeName)
QMetaType | fromType()
bool | hasRegisteredConverterFunction(QMetaType fromType, QMetaType toType)
bool | hasRegisteredConverterFunction()
bool | hasRegisteredMutableViewFunction(QMetaType fromType, QMetaType toType)
`(since 6.0)` bool | hasRegisteredMutableViewFunction()
bool | isRegistered(int type)
bool | registerConverter()
bool | registerConverter(To (From::*)() const function)
bool | registerConverter(To (From::*)(bool *) const function)
bool | registerConverter(UnaryFunction function)
`(since 6.0)` bool | registerMutableView(To (From::*)() function)
`(since 6.0)` bool | registerMutableView(UnaryFunction function)
`(since 6.0)` bool | view(QMetaType fromType, void *from, QMetaType toType, void *to)

## Related Non-Members

`(since 6.4)` size_t | qHash(QMetaType type, size_t seed = 0)
int | qMetaTypeId()
int | qRegisterMetaType()
`(since 6.5)` int | qRegisterMetaType(QMetaType meta)
bool | operator!=(const QMetaType &lhs, const QMetaType &rhs)
`(since 6.5)` QDebug | operator<<(QDebug d, QMetaType m)
bool | operator==(const QMetaType &lhs, const QMetaType &rhs)

## Macros

| Q_DECLARE_ASSOCIATIVE_CONTAINER_METATYPE(Container)
| Q_DECLARE_METATYPE(Type)
| Q_DECLARE_OPAQUE_POINTER(PointerType)
| Q_DECLARE_SEQUENTIAL_CONTAINER_METATYPE(Container)
| Q_DECLARE_SMART_POINTER_METATYPE(SmartPointer)

## Detailed Description

The class is used as a helper to marshall types in QVariant and in queued signals and slots connections. It associates a type name to a type so that it can be created and destructed dynamically at run-time.

Type names can be registered with QMetaType by using either qRegisterMetaType() or registerType(). Registration is not required for most operations; it's only required for operations that attempt to resolve a type name in string form back to a QMetaType object or the type's ID. Those include some old-style signal-slot connections using QObject::connect(), reading user-types from QDataStream to QVariant, or binding to other languages and IPC mechanisms, like QML, D-Bus, JavaScript, etc.

The following code allocates and destructs an instance of `MyClass` by its name, which requires that `MyClass` have been previously registered:

     QMetaType type = QMetaType::fromName("MyClass");
     if (type.isValid()) {
         void *myClassPtr = type.create();
         ...
         type.destroy(myClassPtr);
         myClassPtr = nullptr;
     }

If we want the stream operators `operator<<()` and `operator>>()` to work on QVariant objects that store custom types, the custom type must provide `operator<<()` and `operator>>()` operators.

See also Q_DECLARE_METATYPE(), QVariant::setValue(), QVariant::value(), and QVariant::fromValue().

## Member Type Documentation

### enum QMetaType::Type

These are the built-in types supported by QMetaType:

Constant | Value | Description
--- | --- | ---
`QMetaType::Void` | `43` | `void`
`QMetaType::Bool` | `1` | `bool`
`QMetaType::Int` | `2` | `int`
`QMetaType::UInt` | `3` | `unsigned int`
`QMetaType::Double` | `6` | `double`
`QMetaType::QChar` | `7` | QChar
`QMetaType::QString` | `10` | QString
`QMetaType::QByteArray` | `12` | QByteArray
`QMetaType::Nullptr` | `51` | `std::nullptr_t`
`QMetaType::VoidStar` | `31` | `void *`
`QMetaType::Long` | `32` | `long`
`QMetaType::LongLong` | `4` | LongLong
`QMetaType::Short` | `33` | `short`
`QMetaType::Char` | `34` | `char`
`QMetaType::Char16` | `56` | `char16_t`
`QMetaType::Char32` | `57` | `char32_t`
`QMetaType::ULong` | `35` | `unsigned long`
`QMetaType::ULongLong` | `5` | ULongLong
`QMetaType::UShort` | `36` | `unsigned short`
`QMetaType::SChar` | `40` | `signed char`
`QMetaType::UChar` | `37` | `unsigned char`
`QMetaType::Float` | `38` | `float`
`QMetaType::Float16` | `63` | qfloat16
`QMetaType::QObjectStar` | `39` | QObject *
`QMetaType::QCursor` | `0x100a` | QCursor
`QMetaType::QDate` | `14` | QDate
`QMetaType::QSize` | `21` | QSize
`QMetaType::QTime` | `15` | QTime
`QMetaType::QVariantList` | `9` | QVariantList
`QMetaType::QPolygon` | `0x1007` | QPolygon
`QMetaType::QPolygonF` | `0x1016` | QPolygonF
`QMetaType::QColor` | `0x1003` | QColor
`QMetaType::QColorSpace` | `0x1017` | QColorSpace (introduced in Qt 5.15)
`QMetaType::QSizeF` | `22` | QSizeF
`QMetaType::QRectF` | `20` | QRectF
`QMetaType::QLine` | `23` | QLine
`QMetaType::QTextLength` | `0x100d` | QTextLength
`QMetaType::QStringList` | `11` | QStringList
`QMetaType::QVariantMap` | `8` | QVariantMap
`QMetaType::QVariantHash` | `28` | QVariantHash
`QMetaType::QVariantPair` | `58` | QVariantPair
`QMetaType::QIcon` | `0x1005` | QIcon
`QMetaType::QPen` | `0x100c` | QPen
`QMetaType::QLineF` | `24` | QLineF
`QMetaType::QTextFormat` | `0x100e` | QTextFormat
`QMetaType::QRect` | `19` | QRect
`QMetaType::QPoint` | `25` | QPoint
`QMetaType::QUrl` | `17` | QUrl
`QMetaType::QRegularExpression` | `44` | QRegularExpression
`QMetaType::QDateTime` | `16` | QDateTime
`QMetaType::QPointF` | `26` | QPointF
`QMetaType::QPalette` | `0x1004` | QPalette
`QMetaType::QFont` | `0x1000` | QFont
`QMetaType::QBrush` | `0x1002` | QBrush
`QMetaType::QRegion` | `0x1008` | QRegion
`QMetaType::QBitArray` | `13` | QBitArray
`QMetaType::QImage` | `0x1006` | QImage
`QMetaType::QKeySequence` | `0x100b` | QKeySequence
`QMetaType::QSizePolicy` | `0x2000` | QSizePolicy
`QMetaType::QPixmap` | `0x1001` | QPixmap
`QMetaType::QLocale` | `18` | QLocale
`QMetaType::QBitmap` | `0x1009` | QBitmap
`QMetaType::QTransform` | `0x1010` | QTransform
`QMetaType::QMatrix4x4` | `0x1011` | QMatrix4x4
`QMetaType::QVector2D` | `0x1012` | QVector2D
`QMetaType::QVector3D` | `0x1013` | QVector3D
`QMetaType::QVector4D` | `0x1014` | QVector4D
`QMetaType::QQuaternion` | `0x1015` | QQuaternion
`QMetaType::QEasingCurve` | `29` | QEasingCurve
`QMetaType::QJsonValue` | `45` | QJsonValue
`QMetaType::QJsonObject` | `46` | QJsonObject
`QMetaType::QJsonArray` | `47` | QJsonArray
`QMetaType::QJsonDocument` | `48` | QJsonDocument
`QMetaType::QCborValue` | `53` | QCborValue
`QMetaType::QCborArray` | `54` | QCborArray
`QMetaType::QCborMap` | `55` | QCborMap
`QMetaType::QCborSimpleType` | `52` | QCborSimpleType
`QMetaType::QModelIndex` | `42` | QModelIndex
`QMetaType::QPersistentModelIndex` | `50` | QPersistentModelIndex (introduced in Qt 5.5)
`QMetaType::QUuid` | `30` | QUuid
`QMetaType::QByteArrayList` | `49` | QByteArrayList
`QMetaType::QVariant` | `41` | QVariant
`QMetaType::User` | `65536` | Base value for user types
`QMetaType::UnknownType` | `0` | This is an invalid type id. It is returned from QMetaType for types that are not registered

Additional types can be registered using qRegisterMetaType() or by calling registerType().

See also type() and typeName().

### enum QMetaType::TypeFlag
flags QMetaType::TypeFlags

The enum describes attributes of a type supported by QMetaType.

Constant | Value | Description
--- | --- | ---
`QMetaType::NeedsConstruction` | `0x1` | This type has a default constructor. If the flag is not set, instances can be safely initialized with memset to 0.
`QMetaType::NeedsCopyConstruction` | `0x4000` | (since 6.5) This type has a non-trivial copy constructor. If the flag is not set, instances can be copied with memcpy.
`QMetaType::NeedsMoveConstruction` | `0x8000` | (since 6.5) This type has a non-trivial move constructor. If the flag is not set, instances can be moved with memcpy.
`QMetaType::NeedsDestruction` | `0x2` | This type has a non-trivial destructor. If the flag is not set, calls to the destructor are not necessary before discarding objects.
`QMetaType::RelocatableType` | `0x4` | An instance of a type having this attribute can be safely moved to a different memory location using memcpy.
`QMetaType::IsEnumeration` | `0x10` | This type is an enumeration.
`QMetaType::IsUnsignedEnumeration` | `0x100` | If the type is an Enumeration, its underlying type is unsigned.
`QMetaType::PointerToQObject` | `0x8` | This type is a pointer to a class derived from QObject.
`QMetaType::IsPointer` | `0x800` | This type is a pointer to another type.
`QMetaType::IsConst` | `0x2000` | Indicates that values of this type are immutable; for instance, because they are pointers to const objects.

Note: Before Qt 6.5, both the NeedsConstruction and NeedsDestruction flags were incorrectly set if the either copy construtor or destructor were non-trivial (that is, if the type was not trivial).

Note that the Needs flags may be set but the meta type may not have a publicly-accessible constructor of the relevant type or a publicly-accessible destructor.

The TypeFlags type is a typedef for QFlags<TypeFlag>. It stores an OR combination of TypeFlag values.

## Member Function Documentation

### `[constexpr noexcept, since 6.0]` QMetaType::QMetaType()

Constructs a default, invalid, QMetaType object.

This function was introduced in Qt 6.0.

### `[explicit]` QMetaType::QMetaType(int typeId)

Constructs a QMetaType object that contains all information about type typeId.

### `[constexpr, since 6.0]` qsizetype QMetaType::alignOf() const

Returns the alignment of the type in bytes (i.e. alignof(T), where T is the actual type for which this QMetaType instance was constructed for).

This function is typically used together with construct() to perform low-level management of the memory used by a type.

This function was introduced in Qt 6.0.

See also QMetaType::construct() and QMetaType::sizeOf().

### `[static]` bool QMetaType::canConvert(QMetaType fromType, QMetaType toType)

Returns `true` if QMetaType::convert can convert from fromType to toType.

The following conversions are supported by Qt:

Type | Automatically Cast To
--- | ---
QMetaType::Bool | QMetaType::QChar, QMetaType::Double, QMetaType::Int, QMetaType::LongLong, QMetaType::QString, QMetaType::UInt, QMetaType::ULongLong
QMetaType::QByteArray | QMetaType::Double, QMetaType::Int, QMetaType::LongLong, QMetaType::QString, QMetaType::UInt, QMetaType::ULongLong, QMetaType::QUuid
QMetaType::QChar | QMetaType::Bool, QMetaType::Int, QMetaType::UInt, QMetaType::LongLong, QMetaType::ULongLong
QMetaType::QColor | QMetaType::QString
QMetaType::QDate | QMetaType::QDateTime, QMetaType::QString
QMetaType::QDateTime | QMetaType::QDate, QMetaType::QString, QMetaType::QTime
QMetaType::Double | QMetaType::Bool, QMetaType::Int, QMetaType::LongLong, QMetaType::QString, QMetaType::UInt, QMetaType::ULongLong
QMetaType::QFont | QMetaType::QString
QMetaType::Int | QMetaType::Bool, QMetaType::QChar, QMetaType::Double, QMetaType::LongLong, QMetaType::QString, QMetaType::UInt, QMetaType::ULongLong
QMetaType::QKeySequence | QMetaType::Int, QMetaType::QString
QMetaType::QVariantList | QMetaType::QStringList (if the list's items can be converted to QStrings)
QMetaType::LongLong | QMetaType::Bool, QMetaType::QByteArray, QMetaType::QChar, QMetaType::Double, QMetaType::Int, QMetaType::QString, QMetaType::UInt, QMetaType::ULongLong
QMetaType::QPoint | QMetaType::QPointF
QMetaType::QRect | QMetaType::QRectF
QMetaType::QString | QMetaType::Bool, QMetaType::QByteArray, QMetaType::QChar, QMetaType::QColor, QMetaType::QDate, QMetaType::QDateTime, QMetaType::Double, QMetaType::QFont, QMetaType::Int, QMetaType::QKeySequence, QMetaType::LongLong, QMetaType::QStringList, QMetaType::QTime, QMetaType::UInt, QMetaType::ULongLong, QMetaType::QUuid
QMetaType::QStringList | QMetaType::QVariantList, QMetaType::QString (if the list contains exactly one item)
QMetaType::QTime | QMetaType::QString
QMetaType::UInt | QMetaType::Bool, QMetaType::QChar, QMetaType::Double, QMetaType::Int, QMetaType::LongLong, QMetaType::QString, QMetaType::ULongLong
QMetaType::ULongLong | QMetaType::Bool, QMetaType::QChar, QMetaType::Double, QMetaType::Int, QMetaType::LongLong, QMetaType::QString, QMetaType::UInt
QMetaType::QUuid | QMetaType::QByteArray, QMetaType::QString

Casting between primitive type (int, float, bool etc.) is supported.

Converting between pointers of types derived from QObject will also return true for this function if a qobject_cast from the type described by fromType to the type described by toType would succeed.

A cast from a sequential container will also return true for this function if the toType is QVariantList.

Similarly, a cast from an associative container will also return true for this function the toType is QVariantHash or QVariantMap.

See also convert(), QSequentialIterable, Q_DECLARE_SEQUENTIAL_CONTAINER_METATYPE(), QAssociativeIterable, and Q_DECLARE_ASSOCIATIVE_CONTAINER_METATYPE().

### `[static]` bool QMetaType::canView(QMetaType fromType, QMetaType toType)

Returns `true` if QMetaType::view can create a mutable view of type toType on type fromType.

Converting between pointers of types derived from QObject will return true for this function if a qobject_cast from the type described by fromType to the type described by toType would succeed.

You can create a mutable view of type QSequentialIterable on any container registered with Q_DECLARE_SEQUENTIAL_CONTAINER_METATYPE().

Similarly you can create a mutable view of type QAssociativeIterable on any container registered with Q_DECLARE_ASSOCIATIVE_CONTAINER_METATYPE().

See also convert(), QSequentialIterable, Q_DECLARE_SEQUENTIAL_CONTAINER_METATYPE(), QAssociativeIterable, and Q_DECLARE_ASSOCIATIVE_CONTAINER_METATYPE().

### `[since 6.0]` QPartialOrdering QMetaType::compare(const void *lhs, const void *rhs) const

Compares the objects at lhs and rhs for ordering.

Returns QPartialOrdering::Unordered if comparison is not supported or the values are unordered. Otherwise, returns QPartialOrdering::Less, QPartialOrdering::Equivalent or QPartialOrdering::Greater if lhs is less than, equivalent to or greater than rhs, respectively.

Both objects must be of the type described by this metatype. If either lhs or rhs is `nullptr`, the values are unordered. Comparison is only supported if the type's less than operator was visible to the metatype declaration.

If the type's equality operator was also visible, values will only compare equal if the equality operator says they are. In the absence of an equality operator, when neither value is less than the other, values are considered equal; if equality is also available and two such values are not equal, they are considered unordered, just as NaN (not a number) values of a floating point type lie outside its ordering.

Note: If no less than operator was visible to the metatype declaration, values are unordered even if an equality operator visible to the declaration considers them equal: `compare() == 0` only agrees with equals() if the less than operator was visible.

This function was introduced in Qt 6.0.

See also equals() and isOrdered().

### void *QMetaType::construct(void *where, const void *copy = nullptr) const

Constructs a value of the type that this QMetaType instance was constructed for in the existing memory addressed by where, that is a copy of copy, and returns where. If copy is zero, the value is default constructed.

This is a low-level function for explicitly managing the memory used to store the type. Consider calling create() if you don't need this level of control (that is, use "new" rather than "placement new").

You must ensure that where points to a location where the new value can be stored and that where is suitably aligned. The type's size can be queried by calling sizeOf().

The rule of thumb for alignment is that a type is aligned to its natural boundary, which is the smallest power of 2 that is bigger than the type, unless that alignment is larger than the maximum useful alignment for the platform. For practical purposes, alignment larger than 2 * sizeof(void*) is only necessary for special hardware instructions (e.g., aligned SSE loads and stores on x86).

### `[static]` bool QMetaType::convert(QMetaType fromType, const void *from, QMetaType toType, void *to)

Converts the object at from from fromType to the preallocated space at to typed toType. Returns `true`, if the conversion succeeded, otherwise false.

Both from and to have to be valid pointers.

### void *QMetaType::create(const void *copy = nullptr) const

Returns a copy of copy, assuming it is of the type that this QMetaType instance was created for. If copy is `nullptr`, creates a default constructed instance.

See also QMetaType::destroy().

### bool QMetaType::debugStream(QDebug &dbg, const void *rhs)

Streams the object at rhs to the debug stream dbg. Returns `true` on success, otherwise false.

### void QMetaType::destroy(void *data) const

Destroys the data, assuming it is of the type that this QMetaType instance was created for.

See also QMetaType::create().

### void QMetaType::destruct(void *data) const

Destructs the value, located at data, assuming that it is of the type for which this QMetaType instance was constructed for.

Unlike destroy(), this function only invokes the type's destructor, it doesn't invoke the delete operator.

See also QMetaType::construct().

### `[since 6.0]` bool QMetaType::equals(const void *lhs, const void *rhs) const

Compares the objects at lhs and rhs for equality.

Both objects must be of the type described by this metatype. Can only compare the two objects if a less than or equality operator for the type was visible to the metatype declaration. Otherwise, the metatype never considers values equal. When an equality operator was visible to the metatype declaration, it is authoritative; otherwise, if less than is visible, when neither value is less than the other, the two are considered equal. If values are unordered (see compare() for details) they are not equal.

Returns true if the two objects compare equal, otherwise false.

This function was introduced in Qt 6.0.

See also isEqualityComparable() and compare().

### `[constexpr]` QMetaType::TypeFlags QMetaType::flags() const

Returns flags of the type for which this QMetaType instance was constructed. To inspect specific type traits, prefer using one of the "is-" functions rather than the flags directly.

See also QMetaType::TypeFlags, QMetaType::flags(), isDefaultConstructible(), isCopyConstructible(), isMoveConstructible(), isDestructible(), isEqualityComparable(), and isOrdered().

### `[static]` QMetaType QMetaType::fromName(QByteArrayView typeName)

Returns a QMetaType matching typeName. The returned object is not valid if the typeName is not known to QMetaType

### `[static constexpr]` template <typename T> QMetaType QMetaType::fromType()

Returns the QMetaType corresponding to the type in the template parameter.

### `[static]` bool QMetaType::hasRegisteredConverterFunction(QMetaType fromType, QMetaType toType)

Returns `true`, if the meta type system has a registered conversion from meta type id fromType to toType

### `[static]` template <typename From, typename To> bool QMetaType::hasRegisteredConverterFunction()

Returns `true`, if the meta type system has a registered conversion from type From to type To.

This is an overloaded function.

### `[since 6.1]` bool QMetaType::hasRegisteredDataStreamOperators() const

Returns `true`, if the meta type system has registered data stream operators for this meta type.

This function was introduced in Qt 6.1.

### `[since 6.0]` bool QMetaType::hasRegisteredDebugStreamOperator() const

Returns `true`, if the meta type system has a registered debug stream operator for this meta type.

This function was introduced in Qt 6.0.

### `[static]` bool QMetaType::hasRegisteredMutableViewFunction(QMetaType fromType, QMetaType toType)

Returns `true`, if the meta type system has a registered mutable view on meta type id fromType of meta type id toType.

### `[static, since 6.0]` template <typename From, typename To> bool QMetaType::hasRegisteredMutableViewFunction()

Returns `true`, if the meta type system has a registered mutable view on type From of type To.

This is an overloaded function.

This function was introduced in Qt 6.0.

### int QMetaType::id() const

Returns id type held by this QMetatype instance.

### `[noexcept, since 6.5]` bool QMetaType::isCopyConstructible() const

Returns true if this type can be copy-constructed. If it can be, then construct() and create() can be used with a `copy` parameter that is not null.

This function was introduced in Qt 6.5.

See also flags(), isDefaultConstructible(), isMoveConstructible(), and isDestructible().

### `[noexcept, since 6.5]` bool QMetaType::isDefaultConstructible() const

Returns true if this type can be default-constructed. If it can be, then construct() and create() can be used with a `copy` parameter that is null.

This function was introduced in Qt 6.5.

See also flags(), isCopyConstructible(), isMoveConstructible(), and isDestructible().

### `[noexcept, since 6.5]` bool QMetaType::isDestructible() const

Returns true if this type can be destroyed. If it can be, then destroy() and destruct() can be called.

This function was introduced in Qt 6.5.

See also flags(), isDefaultConstructible(), isCopyConstructible(), and isMoveConstructible().

### bool QMetaType::isEqualityComparable() const

Returns `true` if a less than or equality operator for the type described by this metatype was visible to the metatype declaration, otherwise `false`.

See also equals() and isOrdered().

### `[noexcept, since 6.5]` bool QMetaType::isMoveConstructible() const

Returns true if this type can be move-constructed. QMetaType currently does not have an API to make use of this trait.

This function was introduced in Qt 6.5.

See also flags(), isDefaultConstructible(), isCopyConstructible(), and isDestructible().

### bool QMetaType::isOrdered() const

Returns `true` if a less than operator for the type described by this metatype was visible to the metatype declaration, otherwise `false`.

See also compare() and isEqualityComparable().

### bool QMetaType::isRegistered() const

Returns `true` if this QMetaType object has been registered with the Qt global metatype registry. Registration allows the type to be found by its name (using QMetaType::fromName()) or by its ID (using the constructor).

See also qRegisterMetaType() and isValid().

### `[static]` bool QMetaType::isRegistered(int type)

Returns `true` if the datatype with ID type is registered; otherwise returns `false`.

See also type(), typeName(), and Type.

### bool QMetaType::isValid() const

Returns `true` if this QMetaType object contains valid information about a type, false otherwise.

See also isRegistered().

### bool QMetaType::load(QDataStream &stream, void *data) const

Reads the object of this type from the given stream into data. Returns `true` if the object is loaded successfully; otherwise returns `false`.

Normally, you should not need to call this function directly. Instead, use QVariant's `operator>>()`, which relies on load() to stream custom types.

See also save().

### `[constexpr]` const QMetaObject *QMetaType::metaObject() const

Returns a QMetaObject relative to this type.

If the type is a pointer type to a subclass of QObject, flags() contains QMetaType::PointerToQObject and this function returns the corresponding QMetaObject. This can be used in combination with QMetaObject::newInstance() to create QObjects of this type.

If the type is a Q_GADGET, flags() contains QMetaType::IsGadget. If the type is a pointer to a Q_GADGET, flags() contains QMetaType::PointerToGadget. In both cases, this function returns its QMetaObject. This can be used to retrieve QMetaMethod and QMetaProperty and use them on a pointer of this type for example, as given by QVariant::data().

If the type is an enumeration, flags() contains QMetaType::IsEnumeration. In this case, this function returns the QMetaObject of the enclosing object if the enum was registered as a Q_ENUM or `nullptr` otherwise.

See also QMetaType::flags().

### `[constexpr]` const char *QMetaType::name() const

Returns the type name associated with this QMetaType, or a null pointer if no matching type was found. The returned pointer must not be deleted.

See also typeName().

### `[static]` template <typename From, typename To> bool QMetaType::registerConverter()

Registers the possibility of an implicit conversion from type From to type To in the meta type system. Returns `true` if the registration succeeded, otherwise false.

     class Counter {
       int number = 0;
     public:
       int value() const { return number; }
       operator int() const { return value(); }
       void increment() {++number;}
     };
     QMetaType::registerConverter<Counter, int>();

### `[static]` template <typename From, typename To> bool QMetaType::registerConverter(To (From::*)() const function)

This is an overloaded function.

Registers a method function like To From::function() const as converter from type From to type To in the meta type system. Returns `true` if the registration succeeded, otherwise false.

     struct Coordinates {
       int x;
       int y;
       int z;

       QString toString() const { return u"[x: %1; y: %2, z: %3]"_s.arg(QString::number(x),
         QString::number(y),
         QString::number(z)); }
     };
     QMetaType::registerConverter<Coordinates, QString>(&Coordinates::toString);

### `[static]` template <typename From, typename To> bool QMetaType::registerConverter(To (From::*)(bool *) const function)

This is an overloaded function.

Registers a method function like To From::function(bool *ok) const as converter from type From to type To in the meta type system. Returns `true` if the registration succeeded, otherwise false.

The `ok` pointer can be used by the function to indicate whether the conversion succeeded.

     struct BigNumber {
         long long l;

         int toInt(bool *ok = nullptr) const {
           const bool canConvertSafely = l < std::numeric_limits<int>::max();
           if (ok)
             *ok = canConvertSafely;
           return l;
         }
     };
     QMetaType::registerConverter<BigNumber, int>(&BigNumber::toInt);

### `[static]` template <typename From, typename To, typename UnaryFunction> bool QMetaType::registerConverter(UnaryFunction function)

This is an overloaded function.

Registers a unary function object function as converter from type From to type To in the meta type system. Returns `true` if the registration succeeded, otherwise false.

function must take an instance of type `From` and return an instance of `To`. It can be a function pointer, a lambda or a functor object. Since Qt 6.5, the function can also return an instance of `std::optional<To>` to be able to indicate failed conversions.

     QMetaType::registerConverter<CustomStringType, QString>([](const CustomStringType &str) {
         return QString::fromUtf8(str.data());
     });
     QMetaType::registerConverter<QJsonValue, QPointF>(
               [](const QJsonValue &value) -> std::optional<QPointF> {
         const auto object = value.toObject();
         if (!object.contains("x") || !object.contains("y"))
             return std::nullopt;  // The conversion fails if the required properties are missing
         return QPointF{object["x"].toDouble(), object["y"].toDouble()};
     });

### `[static, since 6.0]` template <typename From, typename To> bool QMetaType::registerMutableView(To (From::*)() function)

This is an overloaded function.

Registers a method function like `To From::function()` as mutable view of type `To` on type `From` in the meta type system. Returns `true` if the registration succeeded, otherwise `false`.

This function was introduced in Qt 6.0.

### `[static, since 6.0]` template <typename From, typename To, typename UnaryFunction> bool QMetaType::registerMutableView(UnaryFunction function)

This is an overloaded function.

Registers a unary function object function as mutable view of type To on type From in the meta type system. Returns `true` if the registration succeeded, otherwise `false`.

This function was introduced in Qt 6.0.

### `[since 6.5]` void QMetaType::registerType() const

Registers this QMetaType with the type registry so it can be found by name, using QMetaType::fromName().

This function was introduced in Qt 6.5.

See also qRegisterMetaType().

### bool QMetaType::save(QDataStream &stream, const void *data) const

Writes the object pointed to by data to the given stream. Returns `true` if the object is saved successfully; otherwise returns `false`.

Normally, you should not need to call this function directly. Instead, use QVariant's `operator<<()`, which relies on save() to stream custom types.

See also load().

### `[constexpr]` qsizetype QMetaType::sizeOf() const

Returns the size of the type in bytes (i.e. sizeof(T), where T is the actual type for which this QMetaType instance was constructed for).

This function is typically used together with construct() to perform low-level management of the memory used by a type.

See also QMetaType::construct(), QMetaType::sizeOf(), and QMetaType::alignOf().

### `[since 6.6]` QMetaType QMetaType::underlyingType() const

If this metatype represents an enumeration, this method returns a metatype of a numeric class of the same signedness and size as the enums underlying type. If it represents a QFlags type, it returns QMetaType::Int. In all other cases an invalid QMetaType is returned.

This function was introduced in Qt 6.6.

### `[static, since 6.0]` bool QMetaType::view(QMetaType fromType, void *from, QMetaType toType, void *to)

Creates a mutable view on the object at from of fromType in the preallocated space at to typed toType. Returns `true` if the conversion succeeded, otherwise false.

This function was introduced in Qt 6.0.

## Related Non-Members

### `[since 6.4]` size_t qHash(QMetaType type, size_t seed = 0)

Returns the hash value for the type, using seed to seed the calculation.

This function was introduced in Qt 6.4.

### `[constexpr]` template <typename T> int qMetaTypeId()

Returns the meta type id of type `T` at compile time. If the type was not declared with Q_DECLARE_METATYPE(), compilation will fail.

Typical usage:

     int id = qMetaTypeId<QString>();    // id is now QMetaType::QString
     id = qMetaTypeId<MyStruct>();       // compile error if MyStruct not declared

QMetaType::type() returns the same ID as qMetaTypeId(), but does a lookup at runtime based on the name of the type. QMetaType::type() is a bit slower, but compilation succeeds if a type is not registered.

See also Q_DECLARE_METATYPE() and QMetaType::type().

### `[constexpr]` template <typename T> int qRegisterMetaType()

Call this function to register the type `T`. Returns the meta type Id.

Example:

     int id = qRegisterMetaType<MyStruct>();

This function requires that `T` is a fully defined type at the point where the function is called. For pointer types, it also requires that the pointed to type is fully defined. Use Q_DECLARE_OPAQUE_POINTER() to be able to register pointers to forward declared types.

To use the type `T` in QMetaType, QVariant, or with the QObject::property() API, registration is not necessary.

To use the type `T` in queued signal and slot connections, `qRegisterMetaType<T>()` must be called before the first connection is established. That is typically done in the constructor of the class that uses `T`, or in the `main()` function.

After a type has been registered, it can be found by its name using QMetaType::fromName().

See also Q_DECLARE_METATYPE().

### `[since 6.5]` int qRegisterMetaType(QMetaType meta)

Registers the meta type meta and returns its type Id.

This function requires that `T` is a fully defined type at the point where the function is called. For pointer types, it also requires that the pointed to type is fully defined. Use Q_DECLARE_OPAQUE_POINTER() to be able to register pointers to forward declared types.

To use the type `T` in QMetaType, QVariant, or with the QObject::property() API, registration is not necessary.

To use the type `T` in queued signal and slot connections, `qRegisterMetaType<T>()` must be called before the first connection is established. That is typically done in the constructor of the class that uses `T`, or in the `main()` function.

After a type has been registered, it can be found by its name using QMetaType::fromName().

This function was introduced in Qt 6.5.

### `[noexcept]` bool operator!=(const QMetaType &lhs, const QMetaType &rhs)

This is an overloaded function.

Returns `true` if the QMetaType lhs represents a different type than the QMetaType rhs, otherwise returns `false`.

### `[since 6.5]` QDebug operator<<(QDebug d, QMetaType m)

Writes the QMetaType m to the stream d, and returns the stream.

This function was introduced in Qt 6.5.

### `[noexcept]` bool operator==(const QMetaType &lhs, const QMetaType &rhs)

This is an overloaded function.

Returns `true` if the QMetaType lhs represents the same type as the QMetaType rhs, otherwise returns `false`.

## Macro Documentation

### Q_DECLARE_ASSOCIATIVE_CONTAINER_METATYPE(Container)

This macro makes the container Container known to QMetaType as an associative container. This makes it possible to put an instance of Container<T, U> into a QVariant, if T and U are themselves known to QMetaType.

Note that all of the Qt associative containers already have built-in support, and it is not necessary to use this macro with them. The std::map container also has built-in support.

This example shows a typical use of Q_DECLARE_ASSOCIATIVE_CONTAINER_METATYPE():

     #include <unordered_list>

     Q_DECLARE_ASSOCIATIVE_CONTAINER_METATYPE(std::unordered_map)

     void someFunc()
     {
         std::unordered_map<int, bool> container;
         QVariant var = QVariant::fromValue(container);
         // ...
     }

### Q_DECLARE_METATYPE(Type)

This macro makes the type Type known to QMetaType as long as it provides a public default constructor, a public copy constructor and a public destructor. It is needed to use the type Type as a custom type in QVariant.

This macro requires that Type is a fully defined type at the point where it is used. For pointer types, it also requires that the pointed to type is fully defined. Use in conjunction with Q_DECLARE_OPAQUE_POINTER() to register pointers to forward declared types.

Ideally, this macro should be placed below the declaration of the class or struct. If that is not possible, it can be put in a private header file which has to be included every time that type is used in a QVariant.

Adding a Q_DECLARE_METATYPE() makes the type known to all template based functions, including QVariant. Note that if you intend to use the type in queued signal and slot connections or in QObject's property system, you also have to call qRegisterMetaType() since the names are resolved at runtime.

This example shows a typical use case of Q_DECLARE_METATYPE():

     struct MyStruct
     {
         int i;
         ...
     };

     Q_DECLARE_METATYPE(MyStruct)

If `MyStruct` is in a namespace, the Q_DECLARE_METATYPE() macro has to be outside the namespace:

     namespace MyNamespace
     {
         ...
     }

     Q_DECLARE_METATYPE(MyNamespace::MyStruct)

Since `MyStruct` is now known to QMetaType, it can be used in QVariant:

     MyStruct s;
     QVariant var;
     var.setValue(s); // copy s into the variant

     ...

     // retrieve the value
     MyStruct s2 = var.value<MyStruct>();

Some types are registered automatically and do not need this macro:

- Pointers to classes derived from QObject
- QList<T>, QQueue<T>, QStack<T> or QSet<T> where T is a registered meta type
- QHash<T1, T2>, QMap<T1, T2> or std::pair<T1, T2> where T1 and T2 are registered meta types
- QPointer<T>, QSharedPointer<T>, QWeakPointer<T>, where T is a class that derives from QObject
- Enumerations registered with Q_ENUM or Q_FLAG
- Classes that have a Q_GADGET macro

Note: This method also registers the stream and debug operators for the type if they are visible at registration time. As this is done automatically in some places, it is strongly recommended to declare the stream operators for a type directly after the type itself. Because of the argument dependent lookup rules of C++, it is also strongly recommended to declare the operators in the same namespace as the type itself.

The stream operators should have the following signatures:

     QDataStream &operator<<(QDataStream &out, const MyClass &myObj);
     QDataStream &operator>>(QDataStream &in, MyClass &myObj);

See also qRegisterMetaType().

### Q_DECLARE_OPAQUE_POINTER(PointerType)

This macro enables pointers to forward-declared types (PointerType) to be registered with QMetaType using either Q_DECLARE_METATYPE() or qRegisterMetaType().

See also Q_DECLARE_METATYPE() and qRegisterMetaType().

### Q_DECLARE_SEQUENTIAL_CONTAINER_METATYPE(Container)

This macro makes the container Container known to QMetaType as a sequential container. This makes it possible to put an instance of Container<T> into a QVariant, if T itself is known to QMetaType.

Note that all of the Qt sequential containers already have built-in support, and it is not necessary to use this macro with them. The std::vector and std::list containers also have built-in support.

This example shows a typical use of Q_DECLARE_SEQUENTIAL_CONTAINER_METATYPE():

     #include <deque>

     Q_DECLARE_SEQUENTIAL_CONTAINER_METATYPE(std::deque)

     void someFunc()
     {
         std::deque<QFile*> container;
         QVariant var = QVariant::fromValue(container);
         // ...
     }

### Q_DECLARE_SMART_POINTER_METATYPE(SmartPointer)

This macro makes the smart pointer SmartPointer known to QMetaType as a smart pointer. This makes it possible to put an instance of SmartPointer<T> into a QVariant, if T is a type which inherits QObject.

Note that the QWeakPointer, QSharedPointer and QPointer already have built-in support, and it is not necessary to use this macro with them.

This example shows a typical use of Q_DECLARE_SMART_POINTER_METATYPE():

     #include <memory>

     Q_DECLARE_SMART_POINTER_METATYPE(std::shared_ptr)

     void someFunc()
     {
         auto smart_ptr = std::make_shared<QFile>();
         QVariant var = QVariant::fromValue(smart_ptr);
         // ...
         if (var.canConvert<QObject*>()) {
             QObject *sp = var.value<QObject*>();
             qDebug() << sp->metaObject()->className(); // Prints 'QFile'.
         }
     }