Calling qRegisterMetaType multiple times for the same type



  • Suppose we have a class that makes use of a special type in a slot, and to make sure that the type is known by the meta type system, we place the qRegisterMetaType() call in the constructor of that class. As a result the function will be called each time we create an instance of that class.

    Is there any problem or (serious) performance loss with repeatedly calling qRegisterMetaType() for the same type?


  • Lifetime Qt Champion

    Hi,

    You should rather call qRegisterMetaType in the constructor of the class that uses the type that you want to register.

    The best way to know the difference between the two techniques you should implement a benchmark.



  • I solved this dilemma with a small metatype registrar class: A template class which registers the metatype in its constructor

    template<class T>
    class CMetaTypeRegistrar
    {
    
    public:
        CMetaTypeRegistrar(const char* metaTypeName)
        {
            qRegisterMetaType<T>(metaTypeName);
        }
    private:
        CMetaTypeRegistrar();
    };
    

    The class can then be instantiated as a static:

    static CMetaTypeRegistrar<CMyClass*> g_RegisterMyClass("CMyClass*");
    

    Guaranteed registration once.
    I don't use it very often, since Q_REGISTER_METATYPE does the job nicely in most cases.


Log in to reply
 

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