QMetaType Class▲
-
Header: QMetaType
-
CMake:
find_package(Qt6 REQUIRED COMPONENTS Core)
target_link_libraries(mytarget PRIVATE Qt6::Core)
-
qmake: QT += core
-
Group: QMetaType is part of objectmodel
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▲
See also Q_DECLARE_METATYPE(), QVariant::setValue(), QVariant::value(), 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▲
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. |
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▲
[explicit] QMetaType::QMetaType(int typeId)▲
Constructs a QMetaType object that contains all information about type typeId.
[constexpr, since 6.0] QMetaType::QMetaType()▲
Constructs a default, invalid, QMetaType object.
This function was introduced in Qt 6.0.
[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▲
See also QMetaType::construct(), 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:
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▲
[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▲
[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.
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▲
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▲
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▲
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▲
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