QMetaType Class▲
-
Header: QMetaType
-
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. 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▲
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::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::QObjectStar |
39 |
QObject * |
QMetaType::QVariant |
41 |
QVariant |
QMetaType::QCursor |
74 |
QCursor |
QMetaType::QDate |
14 |
QDate |
QMetaType::QSize |
21 |
QSize |
QMetaType::QTime |
15 |
QTime |
QMetaType::QVariantList |
9 |
QVariantList |
QMetaType::QPolygon |
71 |
QPolygon |
QMetaType::QPolygonF |
86 |
QPolygonF |
QMetaType::QColor |
67 |
QColor |
QMetaType::QSizeF |
22 |
QSizeF |
QMetaType::QRectF |
20 |
QRectF |
QMetaType::QLine |
23 |
QLine |
QMetaType::QTextLength |
77 |
QTextLength |
QMetaType::QStringList |
11 |
QStringList |
QMetaType::QVariantMap |
8 |
QVariantMap |
QMetaType::QVariantHash |
28 |
QVariantHash |
QMetaType::QIcon |
69 |
QIcon |
QMetaType::QPen |
76 |
QPen |
QMetaType::QLineF |
24 |
QLineF |
QMetaType::QTextFormat |
78 |
QTextFormat |
QMetaType::QRect |
19 |
QRect |
QMetaType::QPoint |
25 |
QPoint |
QMetaType::QUrl |
17 |
QUrl |
QMetaType::QRegExp |
27 |
QRegExp |
QMetaType::QRegularExpression |
44 |
QRegularExpression |
QMetaType::QDateTime |
16 |
QDateTime |
QMetaType::QPointF |
26 |
QPointF |
QMetaType::QPalette |
68 |
QPalette |
QMetaType::QFont |
64 |
QFont |
QMetaType::QBrush |
66 |
QBrush |
QMetaType::QRegion |
72 |
QRegion |
QMetaType::QBitArray |
13 |
QBitArray |
QMetaType::QImage |
70 |
QImage |
QMetaType::QKeySequence |
75 |
QKeySequence |
QMetaType::QSizePolicy |
121 |
QSizePolicy |
QMetaType::QPixmap |
65 |
QPixmap |
QMetaType::QLocale |
18 |
QLocale |
QMetaType::QBitmap |
73 |
QBitmap |
QMetaType::QMatrix |
79 |
QMatrix |
QMetaType::QTransform |
80 |
QTransform |
QMetaType::QMatrix4x4 |
81 |
QMatrix4x4 |
QMetaType::QVector2D |
82 |
QVector2D |
QMetaType::QVector3D |
83 |
QVector3D |
QMetaType::QVector4D |
84 |
QVector4D |
QMetaType::QQuaternion |
85 |
QQuaternion |
QMetaType::QEasingCurve |
29 |
QEasingCurve |
QMetaType::QJsonValue |
45 |
QJsonValue |
QMetaType::QJsonObject |
46 |
QJsonObject |
QMetaType::QJsonArray |
47 |
QJsonArray |
QMetaType::QJsonDocument |
48 |
QJsonDocument |
QMetaType::QModelIndex |
42 |
QModelIndex |
QMetaType::QPersistentModelIndex |
50 |
QPersistentModelIndex (since 5.5) |
QMetaType::QUuid |
30 |
QUuid |
QMetaType::QByteArrayList |
49 |
QByteArrayList |
QMetaType::User |
1024 |
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 Q_DECLARE_METATYPE().
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 non-trivial constructors. If the flag is not set instances can be safely initialized with memset to 0. |
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::MovableType |
0x4 |
An instance of a type having this attribute can be safely moved by memcpy. |
QMetaType::IsEnumeration |
0x10 |
This type is an enumeration |
QMetaType::PointerToQObject |
0x8 |
This type is a pointer to a derived of QObject |
The TypeFlags type is a typedef for QFlags<TypeFlag>. It stores an OR combination of TypeFlag values.
Member Function Documentation▲
[explicit, since 5.0] QMetaType::QMetaType(const int typeId)▲
Constructs a QMetaType object that contains all information about type typeId.
This function was introduced in Qt 5.0.
QMetaType::~QMetaType()▲
Destructs this object.
[static, since 5.2] bool QMetaType::compare(const void *lhs, const void *rhs, int typeId, int *result)▲
Compares the objects at lhs and rhs. Both objects need to be of type typeId. result is set to less than, equal to or greater than zero, if lhs is less than, equal to or greater than rhs. Returns true, if the comparison succeeded, otherwise false.
This function was introduced in Qt 5.2.
[static, since 5.0] void *QMetaType::construct(int type, void *where, const void *copy)▲
Constructs a value of the given type 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 that can store a value of type type, 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).
This function was introduced in Qt 5.0.
See Also▲
[since 5.0] void *QMetaType::construct(void *where, const void *copy = 0) 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).
This function was introduced in Qt 5.0.
[static, since 5.2] bool QMetaType::convert(const void *from, int fromTypeId, void *to, int toTypeId)▲
Converts the object at from from fromTypeId to the preallocated space at to typed toTypeId. Returns true, if the conversion succeeded, otherwise false.
This function was introduced in Qt 5.2.
[static] void *QMetaType::create(int type, const void *copy = nullptr)▲
Returns a copy of copy, assuming it is of type type. If copy is zero, creates a default constructed instance.
See Also▲
See also destroy(), isRegistered(), Type
[since 5.0] void *QMetaType::create(const void *copy = 0) const▲
Returns a copy of copy, assuming it is of the type that this QMetaType instance was created for. If copy is null, creates a default constructed instance.
This function was introduced in Qt 5.0.
See Also▲
See also QMetaType::destroy()
[static, since 5.2] bool QMetaType::debugStream(QDebug &dbg, const void *rhs, int typeId)▲
Streams the object at rhs of type typeId to the debug stream dbg. Returns true on success, otherwise false.
This function was introduced in Qt 5.2.
[static] void QMetaType::destroy(int type, void *data)▲
Destroys the data, assuming it is of the type given.
See Also▲
See also create(), isRegistered(), Type
[since 5.0] void QMetaType::destroy(void *data) const▲
Destroys the data, assuming it is of the type that this QMetaType instance was created for.
This function was introduced in Qt 5.0.
See Also▲
See also QMetaType::create()
[static, since 5.0] void QMetaType::destruct(int type, void *where)▲
Destructs the value of the given type, located at where.
Unlike destroy(), this function only invokes the type's destructor, it doesn't invoke the delete operator.
This function was introduced in Qt 5.0.
See Also▲
See also construct()
[since 5.0] 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.
This function was introduced in Qt 5.0.
See Also▲
See also QMetaType::construct()
[static, since 5.5] bool QMetaType::equals(const void *lhs, const void *rhs, int typeId, int *result)▲
Compares the objects at lhs and rhs. Both objects need to be of type typeId. result is set to zero, if lhs equals to rhs. Returns true, if the comparison succeeded, otherwise false.
This function was introduced in Qt 5.5.
[since 5.0] QMetaType::TypeFlags QMetaType::flags() const▲
Returns flags of the type for which this QMetaType instance was constructed.
This function was introduced in Qt 5.0.
See Also▲
See also QMetaType::TypeFlags, QMetaType::typeFlags()
[static, since 5.2] bool QMetaType::hasRegisteredComparators()▲
Returns true, if the meta type system has registered comparators for type T.
This function was introduced in Qt 5.2.
[static, since 5.2] bool QMetaType::hasRegisteredComparators(int typeId)▲
Returns true, if the meta type system has registered comparators for type id typeId.
This function was introduced in Qt 5.2.
[static, since 5.2] bool QMetaType::hasRegisteredConverterFunction(int fromTypeId, int toTypeId)▲
Returns true, if the meta type system has a registered conversion from meta type id fromTypeId to toTypeId
This function was introduced in Qt 5.2.
[static, since 5.2] 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.
This function was introduced in Qt 5.2.
[static, since 5.2] bool QMetaType::hasRegisteredDebugStreamOperator()▲
Returns true, if the meta type system has a registered debug stream operator for type T.
This function was introduced in Qt 5.2.
[static, since 5.2] bool QMetaType::hasRegisteredDebugStreamOperator(int typeId)▲
Returns true, if the meta type system has a registered debug stream operator for type id typeId.
This function was introduced in Qt 5.2.
[static] bool QMetaType::isRegistered(int type)▲
Returns true if the datatype with ID type is registered; otherwise returns false.
See Also▲
[since 5.0] bool QMetaType::isRegistered() const▲
Returns true if this QMetaType object contains valid information about a type, false otherwise.
This function was introduced in Qt 5.0.
[since 5.0] bool QMetaType::isValid() const▲
Returns true if this QMetaType object contains valid information about a type, false otherwise.
This function was introduced in Qt 5.0.
[static] bool QMetaType::load(QDataStream &stream, int type, void *data)▲
Reads the object of the specified type from the given stream into data. Returns true if the object is loaded successfully; otherwise returns false.
The type must have been registered with qRegisterMetaType() and qRegisterMetaTypeStreamOperators() beforehand.
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▲
See also save(), qRegisterMetaTypeStreamOperators()
[since 5.5] const QMetaObject *QMetaType::metaObject() const▲
return 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 to in combinaison with QMetaObject::construct to create QObject of this type.
If the type is a Q_GADGET, flags() contains QMetaType::IsGadget, and this function returns its QMetaObject. This can be used to retrieve QMetaMethod and QMetaProperty and use them on a pointer of this type. (given by QVariant::data for example)
If the type is an enumeration, flags() contains QMetaType::IsEnumeration, and this function returns the QMetaObject of the enclosing object if the enum was registered as a Q_ENUM or 0 otherwise
This function was introduced in Qt 5.5.
See Also▲
See also QMetaType::metaObjectForType(), QMetaType::flags()
[static, since 5.0] const QMetaObject *QMetaType::metaObjectForType(int type)▲
returns QMetaType::metaObject for type
This function was introduced in Qt 5.0.