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. [Pretty much Solved] A good place to put qRegisterMetaType?
Forum Updated to NodeBB v4.3 + New Features

[Pretty much Solved] A good place to put qRegisterMetaType?

Scheduled Pinned Locked Moved General and Desktop
8 Posts 5 Posters 8.7k Views 1 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.
  • A Offline
    A Offline
    Asperamanca
    wrote on last edited by
    #1

    Given that main() or something called from main() directly is not an option.

    Is qRegisterMetaType fast enough that I can simply call it when constructing the class? Or is there a very fast lookup function that can check whether the metatype is registered?

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

      Hi,

      It depends on the use case, but I usually put this call in the constructor of the classes that will use the metatype unless this class is instantiated at high rates. In that case I use an initialization function that does the work (but it seems that it's not an option in your case)

      Hope it helps

      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

        So if multiple classes use this one class, I might have my registration code all over the place.

        I think I should try harder to get access to a one-time initializer function.

        Thanks!

        1 Reply Last reply
        0
        • L Offline
          L Offline
          lgeyer
          wrote on last edited by
          #4

          @
          template <typename Type> class MetaTypeRegistration
          {
          public:
          inline MetaTypeRegistration()
          {
          qRegisterMetaType<Type>();
          qRegisterMetaTypeStreamOperators<Type>();
          }
          };
          @

          Put a <code>static MetaTypeRegistration<YourType> registration;</code> in the <code>.cpp</code> file implementing <code>YourType</code>.

          This way your metatype will be registered once at startup, and the registration is close by the implementation and does not require to modify <code>main</code>.

          If you are using those metatypes during static initialization time make sure you don't run into the static initialization fiasco.

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

            Lukas,

            if it's that easy, why isn't there an official Qt method of doing it that way?

            1 Reply Last reply
            0
            • raven-worxR Offline
              raven-worxR Offline
              raven-worx
              Moderators
              wrote on last edited by
              #6

              another possibility would be (since templates are a bit of a overhead for the compiler):
              @
              void registerMetaTypes()
              {
              static bool registered = false;
              if( !registered )
              {
              qRegisterMetaType<MyType>("MyType");
              ....
              registered = true;
              }
              }
              @

              --- SUPPORT REQUESTS VIA CHAT WILL BE IGNORED ---
              If you have a question please use the forum so others can benefit from the solution in the future

              1 Reply Last reply
              0
              • L Offline
                L Offline
                lgeyer
                wrote on last edited by
                #7

                [quote author="Asperamanca" date="1367907144"]...if it's that easy, why isn't there an official Qt method of doing it that way?[/quote]Well, maybe because it is that easy to do on your own. ;-)

                In addition you won't find this pattern in Qt because you don't find this pattern in Qt. And there is the potential that you shoot yourself in the foot if you use the metatype at static initialization time and don't mind static initialization order.

                But feel free to file a change request and see what the Qt Project is thinking (all code I post here is public domain and can be used freely without any restrictions).

                [quote author="raven-worx" date="1367907902"]...another possibility would be (since templates are a bit of a overhead for the compiler)...[/quote]There is indeed a compile time overhead to templates, but it is negligible in this case. Your solution has again the downside of seperating the definition and registration (which means I could do the registration in <code>main</code> anyway).

                1 Reply Last reply
                0
                • M Offline
                  M Offline
                  msdos
                  wrote on last edited by
                  #8

                  [[blank-post-content-placeholder]]

                  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