[Qt-interest] Calling qRegisterMetaType on static initialization
Adrien Guinet
adrien at guinet.me
Sun Jan 22 09:07:43 CET 2012
Le 21/01/2012 18:06, Karl Krach a écrit :
> Hello,
>
> "Static member variables are initialized before the 'real program'
> (main()) starts" - right? At least this is how I understand
> http://www.parashift.com/c++-faq-lite/ctors.html#faq-10.14
>
> So I used this nice trick for calling qRegisterMetaType:
Why don't you simply use the Q_DECLARE_METATYPE macro that does this work for
you ? (and works on gcc 4.6 AFAIK :)). Search for it in the Qt assistant for
instance, it provides example for how to use it.
Actually, it is a bit different as what you try to do, as it declares a special
instantiation of the QMetaTypeId template class. See the macro definition :
(from Qt 4.7 sources, qmetatype.h)
#define Q_DECLARE_METATYPE(TYPE) \
QT_BEGIN_NAMESPACE \
template <> \
struct QMetaTypeId< TYPE > \
{ \
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
I assume that qt_metatype_id is called by QVariant (and others) when needed for
a custom type. From what I understand, qRegisterMetaType() will actually
"register" the type only if it has never been registered, and thus return always
the same id. See Qt 4.7 sources' qmetatype.cpp:464 for more informations !
--
Adrien.
More information about the Qt-interest-old
mailing list