QMetaType Class

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

Header: #include <QMetaType>
qmake: QT += core

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

Public Types

enum Type { Void, Bool, Int, UInt, Double, …, UnknownType }
enum TypeFlag { NeedsConstruction, NeedsDestruction, MovableType, IsEnumeration, PointerToQObject }

Macros

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. Declare new types with Q_DECLARE_METATYPE() to make them available to QVariant and other template-based functions. Call qRegisterMetaType() to make types available to non-template based functions, such as the queued signal and slot connections.

Any class or struct that has a public default constructor, a public copy constructor, and a public destructor can be registered.

The following code allocates and destructs an instance of MyClass:

 int id = QMetaType::type("MyClass");
 if (id != QMetaType::UnknownType) {
     void *myClassPtr = QMetaType::create(id);
     ...
     QMetaType::destroy(id, myClassPtr);
     myClassPtr = 0;
 }

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:

ConstantValueDescription
QMetaType::Void43void
QMetaType::Bool1bool
QMetaType::Int2int
QMetaType::UInt3unsigned int
QMetaType::Double6double
QMetaType::QChar7QChar
QMetaType::QString10QString
QMetaType::QByteArray12QByteArray
QMetaType::Nullptr51std::nullptr_t
QMetaType::VoidStar31void *
QMetaType::Long32long
QMetaType::LongLong4LongLong
QMetaType::Short33short
QMetaType::Char34char
QMetaType::ULong35unsigned long
QMetaType::ULongLong5ULongLong
QMetaType::UShort36unsigned short
QMetaType::SChar40signed char
QMetaType::UChar37unsigned char
QMetaType::Float38float
QMetaType::QObjectStar39QObject *
QMetaType::QVariant41QVariant
QMetaType::QCursor74QCursor
QMetaType::QDate14QDate
QMetaType::QSize21QSize
QMetaType::QTime15QTime
QMetaType::QVariantList9QVariantList
QMetaType::QPolygon71QPolygon
QMetaType::QPolygonF86QPolygonF
QMetaType::QColor67QColor
QMetaType::QColorSpace87QColorSpace (introduced in Qt 5.15)
QMetaType::QSizeF22QSizeF
QMetaType::QRectF20QRectF
QMetaType::QLine23QLine
QMetaType::QTextLength77QTextLength
QMetaType::QStringList11QStringList
QMetaType::QVariantMap8QVariantMap
QMetaType::QVariantHash28QVariantHash
QMetaType::QIcon69QIcon
QMetaType::QPen76QPen
QMetaType::QLineF24QLineF
QMetaType::QTextFormat78QTextFormat
QMetaType::QRect19QRect
QMetaType::QPoint25QPoint
QMetaType::QUrl17QUrl
QMetaType::QRegExp27QRegExp
QMetaType::QRegularExpression44QRegularExpression
QMetaType::QDateTime16QDateTime
QMetaType::QPointF26QPointF
QMetaType::QPalette68QPalette
QMetaType::QFont64QFont
QMetaType::QBrush66QBrush
QMetaType::QRegion72QRegion
QMetaType::QBitArray13QBitArray
QMetaType::QImage70QImage
QMetaType::QKeySequence75QKeySequence
QMetaType::QSizePolicy121QSizePolicy
QMetaType::QPixmap65QPixmap
QMetaType::QLocale18QLocale
QMetaType::QBitmap73QBitmap
QMetaType::QMatrix79QMatrix
QMetaType::QTransform80QTransform
QMetaType::QMatrix4x481QMatrix4x4
QMetaType::QVector2D82QVector2D
QMetaType::QVector3D83QVector3D
QMetaType::QVector4D84QVector4D
QMetaType::QQuaternion85QQuaternion
QMetaType::QEasingCurve29QEasingCurve
QMetaType::QJsonValue45QJsonValue
QMetaType::QJsonObject46QJsonObject
QMetaType::QJsonArray47QJsonArray
QMetaType::QJsonDocument48QJsonDocument
QMetaType::QCborValue53QCborValue
QMetaType::QCborArray54QCborArray
QMetaType::QCborMap55QCborMap
QMetaType::QCborSimpleType52QCborSimpleType
QMetaType::QModelIndex42QModelIndex
QMetaType::QPersistentModelIndex50QPersistentModelIndex (introduced in Qt 5.5)
QMetaType::QUuid30QUuid
QMetaType::QByteArrayList49QByteArrayList
QMetaType::User1024Base value for user types
QMetaType::UnknownType0This is an invalid type id. It is returned from QMetaType for types that are not registered

Additional types can be registered using Q_DECLARE_METATYPE().

See also type() and typeName().

enum QMetaType::TypeFlag

The enum describes attributes of a type supported by QMetaType.

ConstantValueDescription
QMetaType::NeedsConstruction0x1This type has non-trivial constructors. If the flag is not set instances can be safely initialized with memset to 0.
QMetaType::NeedsDestruction0x2This type has a non-trivial destructor. If the flag is not set calls to the destructor are not necessary before discarding objects.
QMetaType::MovableType0x4An instance of a type having this attribute can be safely moved by memcpy.
QMetaType::IsEnumeration0x10This type is an enumeration
QMetaType::PointerToQObject0x8This type is a pointer to a derived of QObject

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:

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

This function was introduced in Qt 5.0.

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'.
     }
 }