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. Multiple qRegisterMetaType calls for the same type

Multiple qRegisterMetaType calls for the same type

Scheduled Pinned Locked Moved Solved General and Desktop
11 Posts 4 Posters 3.0k Views
  • 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.
  • kshegunovK Offline
    kshegunovK Offline
    kshegunov
    Moderators
    wrote on last edited by kshegunov
    #1

    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
    0
    • SGaistS Offline
      SGaistS Offline
      SGaist
      Lifetime Qt Champion
      wrote on last edited by
      #2

      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

      kshegunovK 1 Reply Last reply
      1
      • SGaistS SGaist

        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.

        kshegunovK Offline
        kshegunovK Offline
        kshegunov
        Moderators
        wrote on last edited by kshegunov
        #3

        @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
        0
        • SGaistS Offline
          SGaistS Offline
          SGaist
          Lifetime Qt Champion
          wrote on last edited by
          #4

          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

          kshegunovK 1 Reply Last reply
          0
          • SGaistS SGaist

            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.

            kshegunovK Offline
            kshegunovK Offline
            kshegunov
            Moderators
            wrote on last edited by
            #5

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

            Read and abide by the Qt Code of Conduct

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

              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

              kshegunovK 1 Reply Last reply
              0
              • SGaistS SGaist

                Do you mean a library ?

                kshegunovK Offline
                kshegunovK Offline
                kshegunov
                Moderators
                wrote on last edited by
                #7

                @SGaist
                Yes, a dynamic library.

                Read and abide by the Qt Code of Conduct

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

                  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
                  1
                  • A Offline
                    A Offline
                    Asperamanca
                    wrote on last edited by
                    #9

                    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
                    1
                    • mrjjM Offline
                      mrjjM Offline
                      mrjj
                      Lifetime Qt Champion
                      wrote on last edited by
                      #10

                      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
                      0
                      • kshegunovK Offline
                        kshegunovK Offline
                        kshegunov
                        Moderators
                        wrote on last edited by kshegunov
                        #11

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

                        • Login

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