Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Calling qRegisterMetaType multiple times for the same type
Forum Updated to NodeBB v4.3 + New Features

Calling qRegisterMetaType multiple times for the same type

Scheduled Pinned Locked Moved Unsolved General and Desktop
3 Posts 3 Posters 2.3k Views 3 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • J Offline
    J Offline
    Jakob
    wrote on last edited by
    #1

    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?

    1 Reply Last reply
    0
    • SGaistS Offline
      SGaistS Offline
      SGaist
      Lifetime Qt Champion
      wrote on last edited by
      #2

      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.

      Interested in AI ? www.idiap.ch
      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

      1 Reply Last reply
      0
      • A Offline
        A Offline
        Asperamanca
        wrote on last edited by
        #3

        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.

        1 Reply Last reply
        0

        • Login

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • Users
        • Groups
        • Search
        • Get Qt Extensions
        • Unsolved