|  | Home · All Classes · Main Classes · Grouped Classes · Modules · Functions |

# QMetaType Class Reference
[QtCore module]

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

    #include <QMetaType>

Note: All the functions in this class are reentrant, except type(), typeName(), and isRegistered().

- List of all members, including inherited members

### Public Types

- enum Type { Void, Bool, Int, UInt, ..., User }

### Static Public Members

- void * construct ( int type, const void * copy )
- void destroy ( int type, void * data )
- bool isRegistered ( int type )
- int type ( const char * typeName )
- const char * typeName ( int type )

### Related Non-Members

- int qMetaTypeId ( T * dummy = 0 )
- int qRegisterMetaType ( const char * typeName, T * dummy = 0 )

### Macros

- Q_DECLARE_METATYPE ( Type )

## 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. Register new types with qRegisterMetaType(). Any class or struct that has a public 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 != 0) {
            void *myClassPtr = QMetaType::construct(id);
            ...
            QMetaType::destroy(id, myClassPtr);
            myClassPtr = 0;
        }

The Q_DECLARE_METATYPE() macro can be used to register a type at compile time. This is required to use the type as custom type in QVariant.

See also Q_DECLARE_METATYPE(), QVariant::setValue(), QVariant::value(), and QVariant::fromValue().

## Member Type Documentation

### enum QMetaType::Type

These are the built-in types supported by QMetaType:

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::Short` | `130` | `short`
`QMetaType::Char` | `131` | `char`
`QMetaType::ULong` | `132` | `unsigned long`
`QMetaType::UShort` | `133` | `unsigned short`
`QMetaType::UChar` | `134` | `unsigned char`
`QMetaType::Float` | `135` | `float`
`QMetaType::QObjectStar` | `136` | QObject *
`QMetaType::QWidgetStar` | `137` | QWidget *
`QMetaType::User` | `256` | Base value for user types

Additional types can be registered using qRegisterMetaType().

See also type() and typeName().

## Member Function Documentation

### void * QMetaType::construct ( int type, const void * copy ) `[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.

### void QMetaType::destroy ( int type, void * data ) `[static]`

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

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

### bool QMetaType::isRegistered ( int type ) `[static]`

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

Note: This function is thread-safe.

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

### int QMetaType::type ( const char * typeName ) `[static]`

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

Note: This function is thread-safe.

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

### const char * QMetaType::typeName ( 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.

Note: This function is thread-safe.

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

## Related Non-Members

### int qMetaTypeId ( T * dummy = 0 )

Returns the meta type id of type `T` at compile time. If the type was not declared with Q_DECLARE_METATYPE(), compilation will fail. The dummy parameter never has to be specified out, it's a workaround for compiler limitations.

Typical usage:

        int id = qMetaTypeId<QString>(); // id is now QMetaType::QString
        id = 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 is thread-safe.

This function was introduced in Qt 4.1.

See also Q_DECLARE_METATYPE() and QMetaType::type().

### int qRegisterMetaType ( const char * typeName, T * dummy = 0 )

Registers the type name typeName to the type `T`. Returns the internal ID used by QMetaType. Any class or struct that has a public 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 class `MyClass`:

        qRegisterMetaType<MyClass>("MyClass");

You don't need to pass any value for the dummy parameter. It is there because of an MSVC 6 limitation.

Note: This function is thread-safe.

See also QMetaType::isRegistered() and Q_DECLARE_METATYPE().

## Macro Documentation

### Q_DECLARE_METATYPE ( Type )

This is an overloaded member function, provided for convenience. It behaves essentially like the above function.

This macro makes the type Type known to QMetaType. 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.

Q_DECLARE_METATYPE() doesn't actually register the type; you must still use qRegisterMetaType() for that.

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