Qt Forum

    • Login
    • Search
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search
    • Unsolved

    Solved Multiple qRegisterMetaType calls for the same type

    General and Desktop
    4
    11
    2373
    Loading More Posts
    • 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.
    • kshegunov
      kshegunov Moderators last edited by kshegunov

      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.

      Read and abide by the Qt Code of Conduct

      1 Reply Last reply Reply Quote 0
      • SGaist
        SGaist Lifetime Qt Champion last edited by

        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.

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

        kshegunov 1 Reply Last reply Reply Quote 1
        • kshegunov
          kshegunov Moderators @SGaist last edited by kshegunov

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

          Read and abide by the Qt Code of Conduct

          1 Reply Last reply Reply Quote 0
          • SGaist
            SGaist Lifetime Qt Champion last edited by

            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.

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

            kshegunov 1 Reply Last reply Reply Quote 0
            • kshegunov
              kshegunov Moderators @SGaist last edited by

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

              Read and abide by the Qt Code of Conduct

              1 Reply Last reply Reply Quote 0
              • SGaist
                SGaist Lifetime Qt Champion last edited by

                Do you mean a library ?

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

                kshegunov 1 Reply Last reply Reply Quote 0
                • kshegunov
                  kshegunov Moderators @SGaist last edited by

                  @SGaist
                  Yes, a dynamic library.

                  Read and abide by the Qt Code of Conduct

                  1 Reply Last reply Reply Quote 0
                  • SGaist
                    SGaist Lifetime Qt Champion last edited by

                    You could have an initialize function but that could be a bit much in your case.

                    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 Reply Quote 1
                    • A
                      Asperamanca last edited by

                      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();
                      };
                      
                      1 Reply Last reply Reply Quote 1
                      • mrjj
                        mrjj Lifetime Qt Champion last edited by

                        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 :)

                        1 Reply Last reply Reply Quote 0
                        • kshegunov
                          kshegunov Moderators last edited by kshegunov

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

                          Read and abide by the Qt Code of Conduct

                          1 Reply Last reply Reply Quote 0
                          • First post
                            Last post