QMetaType ClassThe QMetaType class manages named types in the meta-object system. More... #include <QMetaType> Note: All functions in this class are thread-safe. Public Types
Public Functions
Static Public Members
Macros
Detailed DescriptionThe QMetaType class manages named types in the meta-object system. 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 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 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
|
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 | 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 | ? | signed char |
QMetaType::UChar | 37 | unsigned char |
QMetaType::Float | 38 | float |
QMetaType::QObjectStar | 39 | QObject * |
QMetaType::QWidgetStar | 40 | QWidget * |
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 | ? | 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 | ? | QJsonValue |
QMetaType::QJsonObject | ? | QJsonObject |
QMetaType::QJsonArray | ? | QJsonArray |
QMetaType::QJsonDocument | ? | QJsonDocument |
QMetaType::User | 256 | Base value for user types |
QMetaType::UnknownType | ? | 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 type() and typeName().
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. |
The TypeFlags type is a typedef for QFlags<TypeFlag>. It stores an OR combination of TypeFlag values.
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 destruct() and sizeOf().
Returns a copy of copy, assuming it is of type type. If copy is zero, creates a default type.
See also destroy(), isRegistered(), and Type.
Destroys the data, assuming it is of the type given.
See also create(), isRegistered(), and Type.
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 construct().
Returns true if the datatype with ID type is registered; otherwise returns false.
See also type(), typeName(), and Type.
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 save() and qRegisterMetaTypeStreamOperators().
Writes the object pointed to by data with the ID type to the given stream. Returns true if the object is saved 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 save() to stream custom types.
See also load() and qRegisterMetaTypeStreamOperators().
Returns the size of the given type in bytes (i.e., sizeof(T), where T is the actual type identified by the type argument).
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 5.0.
See also construct().
Returns a handle to the type called typeName, or QMetaType::UnknownType if there is no such type.
See also isRegistered(), typeName(), and Type.
Returns flags of the given type.
This function was introduced in Qt 5.0.
See also TypeFlags().
Returns the type name associated with the given type, or 0 if no matching type was found. The returned pointer must not be deleted.
See also type(), isRegistered(), and 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>();
See also qRegisterMetaType().
This macro enables pointers to forward-declared types to be registered with QMetaType using either Q_DECLARE_METATYPE() or qRegisterMetaType().
See also Q_DECLARE_METATYPE() and qRegisterMetaType().