Important: Please read the Qt Code of Conduct -

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


    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
        CMetaTypeRegistrar(const char* metaTypeName)

    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