Multiple qRegisterMetaType calls for the same type


  • Qt Champions 2016

    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 register QPointer<QObject> for a widget I'm developing, and I was wondering if I could call the qRegisterMetaType<QPointer<QObjecT>>() in the constructor safely.

    Kind regards.


  • Lifetime Qt Champion

    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.


  • Qt Champions 2016

    @SGaist
    Hello,
    Thanks for the input. Since I need that for a deferred handling of ChildAdded 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.


  • Lifetime Qt Champion

    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.


  • Qt Champions 2016

    @SGaist
    Unfortunately, there's no main.cpp in a shared object ... :)


  • Lifetime Qt Champion

    Do you mean a library ?


  • Qt Champions 2016

    @SGaist
    Yes, a dynamic library.


  • Lifetime Qt Champion

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

  • Qt Champions 2016

    Hi
    There are these "mains"
    http://tdistler.com/2007/10/05/implementing-dllmain-in-a-linux-shared-library

    I have only used the windows version
    WINAPI DllMain
    so you mileage may vary with linux :)


  • Qt Champions 2016

    @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.


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.