
We bake cookies in your browser for a better experience. Using this site means that you consent.Read More
TheQMetaType class manages named types in the meta-object system.More...
| Header: | #include <QMetaType> |
Note: All functions in this class arethread-safe.
| enum | Type { Void, Bool, Int, UInt, ..., User } |
| void * | construct(int type, const void * copy = 0) |
| void | destroy(int type, void * data) |
| bool | isRegistered(int type) |
| bool | load(QDataStream & stream, int type, void * data) |
| bool | save(QDataStream & stream, int type, const void * data) |
| int | type(const char * typeName) |
| const char * | typeName(int type) |
| void | unregisterType(const char * typeName) |
| int | qMetaTypeId() |
| int | qRegisterMetaType(const char * typeName) |
| int | qRegisterMetaType() |
| void | qRegisterMetaTypeStreamOperators(const char * typeName) |
| Q_DECLARE_METATYPE( Type) |
TheQMetaType class manages named types in the meta-object system.
The class is used as a helper to marshall types inQVariant 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 withQ_DECLARE_METATYPE() to make them available toQVariant and other template-based functions. CallqRegisterMetaType() to make type 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 ofMyClass:
int id=QMetaType::type("MyClass");if (id!=0) {void*myClassPtr=QMetaType::construct(id);...QMetaType::destroy(id, myClassPtr); myClassPtr=0;}
If we want the stream operatorsoperator<<() andoperator>>() to work onQVariant objects that store custom types, the custom type must provideoperator<<() andoperator>>() operators.
See alsoQ_DECLARE_METATYPE(),QVariant::setValue(),QVariant::value(), andQVariant::fromValue().
These are the built-in types supported byQMetaType:
| Constant | Value | Description |
|---|---|---|
QMetaType::Void | 0 | 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::VoidStar | 128 | void * |
QMetaType::Long | 129 | long |
QMetaType::LongLong | 4 | LongLong |
QMetaType::Short | 130 | short |
QMetaType::Char | 131 | char |
QMetaType::ULong | 132 | unsigned long |
QMetaType::ULongLong | 5 | ULongLong |
QMetaType::UShort | 133 | unsigned short |
QMetaType::UChar | 134 | unsigned char |
QMetaType::Float | 135 | float |
QMetaType::QObjectStar | 136 | QObject * |
QMetaType::QWidgetStar | 137 | QWidget * |
QMetaType::QVariant | 138 | QVariant |
QMetaType::QColorGroup | 63 | QColorGroup |
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::QColor | 67 | QColor |
QMetaType::QSizeF | 22 | QSizeF |
QMetaType::QRectF | 20 | QRectF |
QMetaType::QLine | 23 | QLine |
QMetaType::QTextLength | 78 | QTextLength |
QMetaType::QStringList | 11 | QStringList |
QMetaType::QVariantMap | 8 | QVariantMap |
QMetaType::QVariantHash | 28 | QVariantHash |
QMetaType::QIcon | 69 | QIcon |
QMetaType::QPen | 77 | QPen |
QMetaType::QLineF | 24 | QLineF |
QMetaType::QTextFormat | 79 | QTextFormat |
QMetaType::QRect | 19 | QRect |
QMetaType::QPoint | 25 | QPoint |
QMetaType::QUrl | 17 | QUrl |
QMetaType::QRegExp | 27 | QRegExp |
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 | 76 | QKeySequence |
QMetaType::QSizePolicy | 75 | QSizePolicy |
QMetaType::QPixmap | 65 | QPixmap |
QMetaType::QLocale | 18 | QLocale |
QMetaType::QBitmap | 73 | QBitmap |
QMetaType::QMatrix | 80 | QMatrix |
QMetaType::QTransform | 81 | QTransform |
QMetaType::QMatrix4x4 | 82 | QMatrix4x4 |
QMetaType::QVector2D | 83 | QVector2D |
QMetaType::QVector3D | 84 | QVector3D |
QMetaType::QVector4D | 85 | QVector4D |
QMetaType::QQuaternion | 86 | QQuaternion |
QMetaType::QEasingCurve | 29 | QEasingCurve |
QMetaType::User | 256 | Base value for user types |
Additional types can be registered usingQ_DECLARE_METATYPE().
[static]void * QMetaType::construct(int type, constvoid * copy = 0)Returns a copy ofcopy, assuming it is of typetype. Ifcopy is zero, creates a default type.
See alsodestroy(),isRegistered(), andType.
[static]void QMetaType::destroy(int type,void * data)Destroys thedata, assuming it is of thetype given.
See alsoconstruct(),isRegistered(), andType.
[static]bool QMetaType::isRegistered(int type)Returns true if the datatype with IDtype is registered; otherwise returns false.
See alsotype(),typeName(), andType.
[static]bool QMetaType::load(QDataStream & stream,int type,void * data)Reads the object of the specifiedtype from the givenstream intodata. Returns true if the object is loaded successfully; otherwise returns false.
The type must have been registered withqRegisterMetaType() andqRegisterMetaTypeStreamOperators() beforehand.
Normally, you should not need to call this function directly. Instead, useQVariant'soperator>>(), which relies on load() to stream custom types.
See alsosave() andqRegisterMetaTypeStreamOperators().
[static]bool QMetaType::save(QDataStream & stream,int type, constvoid * data)Writes the object pointed to bydata with the IDtype to the givenstream. Returns true if the object is saved successfully; otherwise returns false.
The type must have been registered withqRegisterMetaType() andqRegisterMetaTypeStreamOperators() beforehand.
Normally, you should not need to call this function directly. Instead, useQVariant'soperator<<(), which relies on save() to stream custom types.
See alsoload() andqRegisterMetaTypeStreamOperators().
[static]int QMetaType::type(constchar * typeName)Returns a handle to the type calledtypeName, or 0 if there is no such type.
See alsoisRegistered(),typeName(), andType.
[static]constchar * QMetaType::typeName(int type)Returns the type name associated with the giventype, or 0 if no matching type was found. The returned pointer must not be deleted.
See alsotype(),isRegistered(), andType.
[static]void QMetaType::unregisterType(constchar * typeName)Unregisters a user type, withtypeName.
This function was introduced in Qt 4.4.
Returns the meta type id of typeT at compile time. If the type was not declared withQ_DECLARE_METATYPE(), compilation will fail.
Typical usage:
int id= qMetaTypeId<QString>();// id is now QMetaType::QStringid= 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.
Note: This function isthread-safe.
This function was introduced in Qt 4.1.
See alsoQ_DECLARE_METATYPE() andQMetaType::type().
Registers the type nametypeName for the typeT. Returns the internal ID used byQMetaType. Any class or struct that has a public default constructor, a public copy constructor and a public destructor can be registered.
After a type has been registered, you can create and destroy objects of that type dynamically at run-time.
This example registers the classMyClass:
qRegisterMetaType<MyClass>("MyClass");
This function is useful to register typedefs so they can be used byQMetaProperty, or in QueuedConnections
typedefQString CustomString;qRegisterMetaType<CustomString>("CustomString");
Note: This function isthread-safe.
See alsoqRegisterMetaTypeStreamOperators(),QMetaType::isRegistered(), andQ_DECLARE_METATYPE().
Call this function to register the typeT.T must be declared withQ_DECLARE_METATYPE(). Returns the meta type Id.
Example:
int id=qRegisterMetaType<MyStruct>();
To use the typeT inQVariant, usingQ_DECLARE_METATYPE() is sufficient. To use the typeT in queued signal and slot connections,qRegisterMetaType<T>() must be called before the first connection is established.
Also, to use typeT with theQObject::property() API,qRegisterMetaType<T>() must be called before it is used, typically in the constructor of the class that usesT, or in themain() function.
Note: This function isthread-safe.
This function was introduced in Qt 4.2.
See alsoQ_DECLARE_METATYPE().
Registers the stream operators for the typeT calledtypeName.
Afterward, the type can be streamed usingQMetaType::load() andQMetaType::save(). These functions are used when streaming aQVariant.
qRegisterMetaTypeStreamOperators<MyClass>("MyClass");
The stream operators should have the following signatures:
QDataStream&operator<<(QDataStream&out,const MyClass&myObj);QDataStream&operator>>(QDataStream&in, MyClass&myObj);
Note: This function isthread-safe.
See alsoqRegisterMetaType(),QMetaType::isRegistered(), andQ_DECLARE_METATYPE().
This macro makes the typeType known toQMetaType as long as it provides a public default constructor, a public copy constructor and a public destructor. It is needed to use the typeType as a custom type inQVariant.
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 aQVariant.
Adding a Q_DECLARE_METATYPE() makes the type known to all template based functions, includingQVariant. Note that if you intend to use the type inqueued signal and slot connections or inQObject's property system, you also have to callqRegisterMetaType() 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)
IfMyStruct is in a namespace, the Q_DECLARE_METATYPE() macro has to be outside the namespace:
namespace MyNamespace{...}Q_DECLARE_METATYPE(MyNamespace::MyStruct)
SinceMyStruct is now known toQMetaType, it can be used inQVariant:
MyStruct s;QVariant var;var.setValue(s);// copy s into the variant...// retrieve the valueMyStruct s2= var.value<MyStruct>();
See alsoqRegisterMetaType().
© 2016 The Qt Company Ltd. Documentation contributions included herein are the copyrights of their respective owners. The documentation provided herein is licensed under the terms of theGNU Free Documentation License version 1.3 as published by the Free Software Foundation. Qt and respective logos are trademarks of The Qt Company Ltd. in Finland and/or other countries worldwide. All other trademarks are property of their respective owners.