Multiple qRegisterMetaType calls for the same type
-
Hello,
Is it allowed to register in the meta-object system multiple times the same data type? Are there any side effects for that? I need to registerQPointer<QObject>
for a widget I'm developing, and I was wondering if I could call theqRegisterMetaType<QPointer<QObjecT>>()
in the constructor safely.Kind regards.
-
Hi,
Yes you can, the only "drawback" is the time lost reregistering the type. What I usually do is to register the type where it's actually used e.g. a widget that has a slot to handle that kind of data.
-
Hi,
Yes you can, the only "drawback" is the time lost reregistering the type. What I usually do is to register the type where it's actually used e.g. a widget that has a slot to handle that kind of data.
@SGaist
Hello,
Thanks for the input. Since I need that for a deferred handling ofChildAdded
event in a event filter, the constructor seems the most appropriate place. I don't expect to have thousands of that widget so if the speed is the only issue, I can live with that! ;)
Thank you!PS:
The documentation was pretty vague about it to put it mildly.Kind regards.
-
Indeed, it could mention that multiple call to qRegisterMetaType with the same type yields the same result.
One of the most common use of that function though is to register everything in main.cpp.
-
Indeed, it could mention that multiple call to qRegisterMetaType with the same type yields the same result.
One of the most common use of that function though is to register everything in main.cpp.
-
Do you mean a library ?
-
You could have an initialize function but that could be a bit much in your case.
-
You could use a static registration like so:
static CMetaTypeRegistrar<QPointer> g_QPointerRegistrar("QPointer");
The class looks like this:
template<class T> class CMetaTypeRegistrar { public: CMetaTypeRegistrar(const char* metaTypeName) { qRegisterMetaType<T>(metaTypeName); } private: CMetaTypeRegistrar(); };
-
Hi
There are these "mains"
http://tdistler.com/2007/10/05/implementing-dllmain-in-a-linux-shared-libraryI have only used the windows version
WINAPI DllMain
so you mileage may vary with linux :) -
@Asperamanca
I could, however your code is equivalent to this line (without the generation of new classes):static int myTypeId = qRegisterMetaType<MyType>();
Additionally, there's no guarantee that the meta-type system is up and running when this line is executed, since the statics are initialized at the time the loader is running, and the
QApplication
object doesn't exists (which might not be a problem, I'm just not sure).@mrjj
Probably there's such a thing on linux as well, but I for one don't intend to dive into platform-specific code without a good reason, and doing it for a single registration does look like an overkill as @SGaist remarked. Furthermore, my note about the meta-type system initialization from the above paragraph applies here as well.In any case, thank you for the suggestions guys, I'll stick to registering the type in the constructor, as it seems to work quite well and is pretty simple implementation-wise.
Kind regards.