QMetaType Class Reference

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

  1. #include <QMetaType>

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

Detailed Description

The 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:

  1. int id = QMetaType::type("MyClass");
  2. if (id == 0)  {
  3.     void *myClassPtr = QMetaType::construct(id);
  4.     ...
  5.     QMetaType::destroy(id, myClassPtr);
  6.     myClassPtr = 0;
  7. }

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

Public Types

Toggle detailsenum QMetaType::

TypeType { Void , Bool , Int , UInt , LongLong , ULongLong , Double , QChar , QVariantMap , QVariantList , QString , QStringList , QByteArray , QBitArray , QDate , QTime , QDateTime , QUrl , QLocale , QRect , QRectF , QSize , QSizeF , QLine , QLineF , QPoint , QPointF , QRegExp , QVariantHash , QEasingCurve , LastCoreType , FirstGuiType , QColorGroup , QFont , QPixmap , QBrush , QColor , QPalette , QIcon , QImage , QPolygon , QRegion , QBitmap , QCursor , QSizePolicy , QKeySequence , QPen , QTextLength , QTextFormat , QMatrix , QTransform , QMatrix4x4 , QVector2D , QVector3D , QVector4D , QQuaternion , LastGuiType , FirstCoreExtType , VoidStar , Long , Short , Char , ULong , UShort , UChar , Float , QObjectStar , QWidgetStar , QVariant , LastCoreExtType , QReal , User 256 ...} { Void , Bool , Int , UInt , LongLong , ULongLong , Double , QChar , QVariantMap , QVariantList , QString , QStringList , QByteArray , QBitArray , QDate , QTime , QDateTime , QUrl , QLocale , QRect , QRectF , QSize , QSizeF , QLine , QLineF , QPoint , QPointF , QRegExp , QVariantHash , QEasingCurve , LastCoreType , FirstGuiType , QColorGroup , QFont , QPixmap , QBrush , QColor , QPalette , QIcon , QImage , QPolygon , QRegion , QBitmap , QCursor , QSizePolicy , QKeySequence , QPen , QTextLength , QTextFormat , QMatrix , QTransform , QMatrix4x4 , QVector2D , QVector3D , QVector4D , QQuaternion , LastGuiType , FirstCoreExtType , VoidStar , Long , Short , Char , ULong , UShort , UChar , Float , QObjectStar , QWidgetStar , QVariant , LastCoreExtType , QReal , User 256 }

These are the built-in types supported by QMetaType:

ConstantValueDescription
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 using Q_DECLARE_METATYPE().

See also type() and typeName().

Look up this member in the source code.

    Public Functions

    Toggle details void * QMetaType

    constructconstruct ( int type , const void *copy=0 ) ( int type , const void *copy=0 ) [static]

    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.

    Look up this member in the source code.

    Toggle details void QMetaType

    destroydestroy ( int type , void *data ) ( int type , void *data ) [static]

    Destroys the data, assuming it is of the type given.

    See also construct(), isRegistered(), and Type.

    Look up this member in the source code.

    Toggle details bool QMetaType

    isRegisteredisRegistered ( int type ) ( int type ) [static]

    Returns true if the datatype with ID type is registered; otherwise returns false.

    See also type(), typeName(), and Type.

    Look up this member in the source code.

    Toggle details bool QMetaType

    loadload ( QDataStream &stream , int type , void *data ...) ( QDataStream &stream , int type , void *data ) [static]

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

    Look up this member in the source code.

    Toggle details bool QMetaType

    savesave ( QDataStream &stream , int type , const void *data ...) ( QDataStream &stream , int type , const void *data ) [static]

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

    Look up this member in the source code.

    Toggle details int QMetaType

    typetype ( const char *typeName ) ( const char *typeName ) [static]

    Returns a handle to the type called typeName, or 0 if there is no such type.

    See also isRegistered(), typeName(), and Type.

    Look up this member in the source code.

    Toggle details const char * QMetaType

    typeNametypeName ( int type ) ( int type ) [static]

    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.

    Look up this member in the source code.

    Toggle details void QMetaType

    unregisterTypeunregisterType ( const char *typeName ) ( const char *typeName ) [static]

    Unregisters a user type, with typeName.

    See also type() and typeName().

    Look up this member in the source code.

      Macros

      Toggle detailsQ_DECLARE_METATYPEQ_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.

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

      1. struct MyStruct
      2.  {
      3.     int i;
      4.     ...
      5. };
      6.  
      7. Q_DECLARE_METATYPE(MyStruct)

      If MyStruct is in a namespace, the Q_DECLARE_METATYPE() macro has to be outside the namespace:

      1. namespace MyNamespace
      2.  {
      3.     ...
      4. }
      5.  
      6. Q_DECLARE_METATYPE(MyNamespace::MyStruct)

      Since MyStruct is now known to QMetaType, it can be used in QVariant:

      1. MyStruct s;
      2. var.setValue(s); // copy s into the variant
      3.  
      4. ...
      5.  
      6. // retrieve the value
      7. MyStruct s2 = var.value<MyStruct>();

      See also qRegisterMetaType().

        Notes provided by the Qt Community

        No notes