Viadeo Twitter Google Bookmarks ! Facebook Digg del.icio.us MySpace Yahoo MyWeb Blinklist Netvouz Reddit Simpy StumbleUpon Bookmarks Windows Live Favorites 
Logo Documentation Qt ·  Page d'accueil  ·  Toutes les classes  ·  Toutes les fonctions  ·  Vues d'ensemble  · 

QMetaType Class Reference
[QtCore module]

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

 #include <QMetaType>

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


Public Types

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

Static Public Members

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 )

Related Non-Members

int qMetaTypeId ()
int qRegisterMetaType ( const char * typeName )
int qRegisterMetaType ()
void qRegisterMetaTypeStreamOperators ( const char * typeName )

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. 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 == 0) {
     void *myClassPtr = QMetaType::construct(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

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::LongLong4LongLong
QMetaType::Short130short
QMetaType::Char131char
QMetaType::ULong132unsigned long
QMetaType::ULongLong5ULongLong
QMetaType::UShort133unsigned short
QMetaType::UChar134unsigned char
QMetaType::Float135float
QMetaType::QObjectStar136QObject *
QMetaType::QWidgetStar137QWidget *
QMetaType::QColorGroup63QColorGroup
QMetaType::QCursor74QCursor
QMetaType::QDate14QDate
QMetaType::QSize21QSize
QMetaType::QTime15QTime
QMetaType::QVariantList9QVariantList
QMetaType::QPolygon71QPolygon
QMetaType::QColor67QColor
QMetaType::QSizeF22QSizeF
QMetaType::QRectF20QRectF
QMetaType::QLine23QLine
QMetaType::QTextLength78QTextLength
QMetaType::QStringList11QStringList
QMetaType::QVariantMap8QVariantMap
QMetaType::QVariantHash28QVariantHash
QMetaType::QIcon69QIcon
QMetaType::QPen77QPen
QMetaType::QLineF24QLineF
QMetaType::QTextFormat79QTextFormat
QMetaType::QRect19QRect
QMetaType::QPoint25QPoint
QMetaType::QUrl17QUrl
QMetaType::QRegExp27QRegExp
QMetaType::QDateTime16QDateTime
QMetaType::QPointF26QPointF
QMetaType::QPalette68QPalette
QMetaType::QFont64QFont
QMetaType::QBrush66QBrush
QMetaType::QRegion72QRegion
QMetaType::QBitArray13QBitArray
QMetaType::QImage70QImage
QMetaType::QKeySequence76QKeySequence
QMetaType::QSizePolicy75QSizePolicy
QMetaType::QPixmap65QPixmap
QMetaType::QLocale18QLocale
QMetaType::QBitmap73QBitmap
QMetaType::QMatrix80QMatrix
QMetaType::QTransform81QTransform
QMetaType::QMatrix4x482QMatrix4x4
QMetaType::QVector2D83QVector2D
QMetaType::QVector3D84QVector3D
QMetaType::QVector4D85QVector4D
QMetaType::QQuaternion86QQuaternion
QMetaType::User256Base value for user types

Additional types can be registered using Q_DECLARE_METATYPE().

See also type() and typeName().


Member Function Documentation

void * QMetaType::construct ( 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.

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 datatype with ID type is registered; otherwise returns false.

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

bool QMetaType::load ( 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().

bool QMetaType::save ( 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().

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.

void QMetaType::unregisterType ( const char * typeName )   [static]

Unregisters a user type, with typeName.

This function was introduced in Qt 4.4.

See also type() and typeName().


Related Non-Members

int qMetaTypeId ()

Returns the meta type id of type T at compile time. If the type was not declared with Q_DECLARE_METATYPE(), compilation will fail.

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 )

Registers the type name typeName for the type T. Returns the internal ID used by QMetaType. 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 class MyClass:

 qRegisterMetaType<MyClass>("MyClass");

Note: This function is thread-safe.

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

int qRegisterMetaType ()

Call this function to register the type T. T must be declared with Q_DECLARE_METATYPE(). Returns the meta type Id.

Example:

 int id = qRegisterMetaType<MyStruct>();

To use the type T in QVariant, using Q_DECLARE_METATYPE() is sufficient. To use the type T in queued signal and slot connections, qRegisterMetaType<T>() must be called before the first connection is established.

Also, to use type T with the QObject::property() API, qRegisterMetaType<T>() must be called before it is used, typically in the constructor of the class that uses T, or in the main() function.

Note: This function is thread-safe.

This function was introduced in Qt 4.2.

See also Q_DECLARE_METATYPE().

void qRegisterMetaTypeStreamOperators ( const char * typeName )

Registers the stream operators for the type T called typeName.

Afterward, the type can be streamed using QMetaType::load() and QMetaType::save(). These functions are used when streaming a QVariant.

 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 is thread-safe.

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


Macro Documentation

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

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

Publicité

Best Of

Actualités les plus lues

Semaine
Mois
Année
  1. « Quelque chose ne va vraiment pas avec les développeurs "modernes" », un développeur à "l'ancienne" critique la multiplication des bibliothèques 94
  2. Apercevoir la troisième dimension ou l'utilisation multithreadée d'OpenGL dans Qt, un article des Qt Quarterly traduit par Guillaume Belz 0
  3. Les développeurs ignorent-ils trop les failles découvertes dans leur code ? Prenez-vous en compte les remarques des autres ? 17
  4. Pourquoi les programmeurs sont-ils moins payés que les gestionnaires de programmes ? Manquent-ils de pouvoir de négociation ? 42
  5. Quelles nouveautés de C++11 Visual C++ doit-il rapidement intégrer ? Donnez-nous votre avis 10
  6. Adieu qmake, bienvenue qbs : Qt Building Suite, un outil déclaratif et extensible pour la compilation de projets Qt 17
  7. 2017 : un quinquennat pour une nouvelle version du C++ ? Possible, selon Herb Sutter 9
Page suivante

Le blog Digia au hasard

Logo

Créer des applications avec un style Metro avec Qt, exemples en QML et C++, un article de Digia Qt traduit par Thibaut Cuvelier

Le blog Digia est l'endroit privilégié pour la communication sur l'édition commerciale de Qt, où des réponses publiques sont apportées aux questions les plus posées au support. Lire l'article.

Communauté

Ressources

Liens utiles

Contact

  • Vous souhaitez rejoindre la rédaction ou proposer un tutoriel, une traduction, une question... ? Postez dans le forum Contribuez ou contactez-nous par MP ou par email (voir en bas de page).

Qt dans le magazine

Cette page est une traduction d'une page de la documentation de Qt, écrite par Nokia Corporation and/or its subsidiary(-ies). Les éventuels problèmes résultant d'une mauvaise traduction ne sont pas imputables à Nokia. Qt 4.6-snapshot
Copyright © 2012 Developpez LLC. Tous droits réservés Developpez LLC. Aucune reproduction, même partielle, ne peut être faite de ce site et de l'ensemble de son contenu : textes, documents et images sans l'autorisation expresse de Developpez LLC. Sinon, vous encourez selon la loi jusqu'à 3 ans de prison et jusqu'à 300 000 E de dommages et intérêts. Cette page est déposée à la SACD.
Vous avez déniché une erreur ? Un bug ? Une redirection cassée ? Ou tout autre problème, quel qu'il soit ? Ou bien vous désirez participer à ce projet de traduction ? N'hésitez pas à nous contacter ou par MP !
 
 
 
 
Partenaires

Hébergement Web