Qt 4.8
Classes | Namespaces | Macros | Functions
qmetatype.h File Reference
#include <QtCore/qglobal.h>
#include <QtCore/qatomic.h>
#include <QtCore/qdatastream.h>

Go to the source code of this file.

Classes

class  QMetaType
 The QMetaType class manages named types in the meta-object system. More...
 
struct  QMetaTypeId< T >
 
struct  QMetaTypeId2< T >
 
struct  QtPrivate::QMetaTypeIdHelper< T, Defined >
 
struct  QtPrivate::QMetaTypeIdHelper< T, false >
 

Namespaces

 QtPrivate
 

Macros

#define Q_DECLARE_BUILTIN_METATYPE(TYPE, NAME)
 
#define 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. More...
 

Functions

template<typename T >
void * qMetaTypeConstructHelper (const T *t)
 
template<typename T >
void qMetaTypeDeleteHelper (T *t)
 
template<typename T >
int qMetaTypeId ()
 
template<typename T >
void qMetaTypeLoadHelper (QDataStream &stream, T *t)
 
template<typename T >
void qMetaTypeSaveHelper (QDataStream &stream, const T *t)
 
template<typename T >
int qRegisterMetaType (const char *typeName)
 
template<typename T >
int qRegisterMetaType ()
 
template<typename T >
void qRegisterMetaTypeStreamOperators (const char *typeName)
 
template<typename T >
int qRegisterMetaTypeStreamOperators ()
 

Macro Definition Documentation

◆ Q_DECLARE_BUILTIN_METATYPE

#define Q_DECLARE_BUILTIN_METATYPE (   TYPE,
  NAME 
)
Value:
QT_BEGIN_NAMESPACE \
template<> struct QMetaTypeId2<TYPE> \
{ \
enum { Defined = 1, MetaType = QMetaType::NAME }; \
static inline int qt_metatype_id() { return QMetaType::NAME; } \
}; \
QT_END_NAMESPACE
static int qt_metatype_id()
Definition: qmetatype.h:169

Definition at line 282 of file qmetatype.h.

Referenced by qVariantCanConvert().

◆ Q_DECLARE_METATYPE

#define Q_DECLARE_METATYPE (   TYPE)
Value:
QT_BEGIN_NAMESPACE \
template <> \
{ \
enum { Defined = 1 }; \
static int qt_metatype_id() \
{ \
static QBasicAtomicInt metatype_id = Q_BASIC_ATOMIC_INITIALIZER(0); \
if (!metatype_id) \
metatype_id = qRegisterMetaType< TYPE >(#TYPE, \
reinterpret_cast< TYPE *>(quintptr(-1))); \
return metatype_id; \
} \
}; \
QT_END_NAMESPACE
QIntegerForSizeof< void * >::Unsigned quintptr
Definition: qglobal.h:986
#define Q_BASIC_ATOMIC_INITIALIZER(a)
Definition: qbasicatomic.h:218

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;
...
};

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;
var.setValue(s); // copy s into the variant
...
// retrieve the value
MyStruct s2 = var.value<MyStruct>();
See also
qRegisterMetaType()

Definition at line 265 of file qmetatype.h.

Referenced by QLocalePrivate::digitToCLocale(), operator<<(), operator==(), operator>>(), PQfreemem(), qgraphicsitem_cast(), qIsForwardIteratorEnd(), qmlAttachedPropertiesObject(), qobject_cast< QAxObject * >(), scriptValueToQDBusError(), QDeclarativeGuard< QDeclarativeTransition >::setObject(), QTextOption::setTabStop(), QTestEventList::simulate(), QLocale::toCurrencyString(), and QDeclarativeBinding::update().

Function Documentation

◆ qMetaTypeConstructHelper()

template<typename T >
void* qMetaTypeConstructHelper ( const T *  t)

Definition at line 138 of file qmetatype.h.

139 {
140  if (!t)
141  return new T();
142  return new T(*static_cast<const T*>(t));
143 }

◆ qMetaTypeDeleteHelper()

template<typename T >
void qMetaTypeDeleteHelper ( T *  t)

Definition at line 132 of file qmetatype.h.

133 {
134  delete t;
135 }

◆ qMetaTypeId()

template<typename T >
int qMetaTypeId ( )
related

◆ qMetaTypeLoadHelper()

template<typename T >
void qMetaTypeLoadHelper ( QDataStream stream,
T *  t 
)

Definition at line 153 of file qmetatype.h.

154 {
155  stream >> *t;
156 }

◆ qMetaTypeSaveHelper()

template<typename T >
void qMetaTypeSaveHelper ( QDataStream stream,
const T *  t 
)

Definition at line 147 of file qmetatype.h.

148 {
149  stream << *t;
150 }

◆ qRegisterMetaType() [1/2]

template<typename T >
int qRegisterMetaType ( const char *  typeName)
related

Definition at line 185 of file qmetatype.h.

Referenced by QDeclarativePrivate::qmlRegisterUncreatableType().

190 {
191  const int typedefOf = dummy ? -1 : QtPrivate::QMetaTypeIdHelper<T>::qt_metatype_id();
192  if (typedefOf != -1)
193  return QMetaType::registerTypedef(typeName, typedefOf);
194 
195  typedef void*(*ConstructPtr)(const T*);
196  ConstructPtr cptr = qMetaTypeConstructHelper<T>;
197  typedef void(*DeletePtr)(T*);
198  DeletePtr dptr = qMetaTypeDeleteHelper<T>;
199 
200  return QMetaType::registerType(typeName, reinterpret_cast<QMetaType::Destructor>(dptr),
201  reinterpret_cast<QMetaType::Constructor>(cptr));
202 }
static int registerTypedef(const char *typeName, int aliasId)
Definition: qmetatype.cpp:515
const char * typeName
Definition: qmetatype.cpp:239
static int registerType(const char *typeName, Destructor destructor, Constructor constructor)
Registers a user type for marshalling, with typeName, a destructor, and a constructor.
Definition: qmetatype.cpp:477

◆ qRegisterMetaType() [2/2]

template<typename T >
int qRegisterMetaType ( )
related

Definition at line 234 of file qmetatype.h.

239 {
240 #ifdef Q_CC_SUN
241  return qMetaTypeId(static_cast<T *>(0));
242 #else
243  return qMetaTypeId(dummy);
244 #endif
245 }
int qMetaTypeId()
Returns the meta type id of type T at compile time.
Definition: qmetatype.h:224

◆ qRegisterMetaTypeStreamOperators() [1/2]

template<typename T >
void qRegisterMetaTypeStreamOperators ( const char *  typeName)
related

Definition at line 206 of file qmetatype.h.

211 {
212  typedef void(*SavePtr)(QDataStream &, const T *);
213  typedef void(*LoadPtr)(QDataStream &, T *);
214  SavePtr sptr = qMetaTypeSaveHelper<T>;
215  LoadPtr lptr = qMetaTypeLoadHelper<T>;
216 
217  qRegisterMetaType<T>(typeName);
218  QMetaType::registerStreamOperators(typeName, reinterpret_cast<QMetaType::SaveOperator>(sptr),
219  reinterpret_cast<QMetaType::LoadOperator>(lptr));
220 }
const char * typeName
Definition: qmetatype.cpp:239
static void registerStreamOperators(const char *typeName, SaveOperator saveOp, LoadOperator loadOp)
Definition: qmetatype.cpp:374
The QDataStream class provides serialization of binary data to a QIODevice.
Definition: qdatastream.h:71

◆ qRegisterMetaTypeStreamOperators() [2/2]

template<typename T >
int qRegisterMetaTypeStreamOperators ( )
inline

Definition at line 249 of file qmetatype.h.

250 {
251  typedef void(*SavePtr)(QDataStream &, const T *);
252  typedef void(*LoadPtr)(QDataStream &, T *);
253  SavePtr sptr = qMetaTypeSaveHelper<T>;
254  LoadPtr lptr = qMetaTypeLoadHelper<T>;
255 
256  register int id = qMetaTypeId<T>();
258  reinterpret_cast<QMetaType::SaveOperator>(sptr),
259  reinterpret_cast<QMetaType::LoadOperator>(lptr));
260 
261  return id;
262 }
static void registerStreamOperators(const char *typeName, SaveOperator saveOp, LoadOperator loadOp)
Definition: qmetatype.cpp:374
The QDataStream class provides serialization of binary data to a QIODevice.
Definition: qdatastream.h:71