Home · All Classes · Main Classes · Annotated · Grouped Classes · Functions

QMetaType Class Reference

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

#include <QMetaType>

Part of the QtCore module.

Public Types

Static Public Members

Related Non-Members


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

The following code allocates and destructs an instance of MyClass:

    if (QMetaType::isRegistered("MyClass")) {
        int id = QMetaType::type("MyClass");
        void *myClassPtr = QMetaType::construct(id);
        ...
        QMetaType::destroy(id, myClassPtr);
        myClassPtr = 0;
    }

The Q_DECLARE_METATYPE() Macro

The Q_DECLARE_METATYPE() macro makes the type T known to QMetaType. It is needed to use the type T 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.

This example shows a typical use case of Q_DECLARE_METATYPE():

    struct MyStruct
    {
        int i;
        ...
    };

    Q_DECLARE_METATYPE(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>();

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

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


Member Type Documentation

enum QMetaType::Type

These are the built-in types supported by QMetaType:

ConstantValueDescription
QMetaType::Void0void
QMetaType::Bool1bool
QMetaType::Int2int
QMetaType::UInt3unsigned int
QMetaType::Double6double
QMetaType::QChar7QChar
QMetaType::QString10QString
QMetaType::QByteArray12QByteArray
QMetaType::VoidStar128void *
QMetaType::Long129long
QMetaType::Short130short
QMetaType::Char131char
QMetaType::ULong132unsigned long
QMetaType::UShort133unsigned short
QMetaType::UChar134unsigned char
QMetaType::Float135float
QMetaType::QObjectStar136QObject *
QMetaType::QWidgetStar137QWidget *
QMetaType::User256Base 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.

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.

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.

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


Related Non-Members

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

Registers the type name typeName to the type T. Returns the internal ID used by QMetaType.

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.

See also QMetaType::isRegistered().


Copyright © 2005 Trolltech Trademarks
Qt 4.0.0-rc1