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. Correct way to register with QMetaType

Correct way to register with QMetaType

Scheduled Pinned Locked Moved Unsolved General and Desktop
metatype
4 Posts 2 Posters 1.7k 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.
  • CJhaC Offline
    CJhaC Offline
    CJha
    wrote on last edited by CJha
    #1

    How should I register my class/struct that is inside a namespace:

    namespace base {
        namespace impl {
            class A
            {
                ...
            };
        }
    }
    Q_DECLARE_METATYPE(base::impl::A);
    

    I want to use this class inside base::impl namespace as well as base::util namespace.
    How would I register this with qRegisterMetaType:

    qRegisterMetaType<base::impl::A>("base::impl::A");
    // or
    qRegisterMetaType<base::impl::A>("A");
    // or
    qRegisterMetaType<base::impl::A>();
    

    How would I register this with qRegisterMetaTypeStreamOperators:

    qRegisterMetaTypeStreamOperators<base::impl::A>("base::impl::A");
    // or
    qRegisterMetaTypeStreamOperators<base::impl::A>("A");
    // or
    qRegisterMetaTypeStreamOperators<base::impl::A>();
    

    Also, I will be using this class in different ways, like QVector<QVector<base::impl::A>>, std::shared_ptr<base::impl::A> and QVector<QVector<std::shared_ptr<base::impl::A>>> should I register these as well with qRegisterMetaType and qRegisterMetaTypeStreamOperators or just registering the class itself (i.e. base::util::A) will be enough? If I should then what will be the signature?

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

      Hi,

      How exactly are you using your class ?
      Because just using it in a different namespace does not require to register it. Are you using it with QVariant ?

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

      CJhaC 1 Reply Last reply
      0
      • SGaistS SGaist

        Hi,

        How exactly are you using your class ?
        Because just using it in a different namespace does not require to register it. Are you using it with QVariant ?

        CJhaC Offline
        CJhaC Offline
        CJha
        wrote on last edited by CJha
        #3

        @SGaist Yes, I am using it with QVariant, mostly with QSettings to save and retrieve data easily by overriding QDataStream operators. I am also using some in queued signal and slot connections. The ones I am registering for are mostly structs for storing data and just a few simple classes.

        Due to these requirements, I need to use all three (Q_DECLARE_METATYPE, qRegisterMetaType and qRegisterMetaTypeStreamOperators) operators, but to me, it seems to work fine if I do not add the string part at all, i.e. register as:

        qRegisterMetaType<base::impl::A>();  // or
        qRegisterMetaType<QVector<base::impl::A>>(); // or
        qRegisterMetaTypeStreamOperators<base::impl::A>(); // or
        qRegisterMetaTypeStreamOperators<QVector<base::impl::A>>();
        ...
        

        But, it also works fine if I add the string part inside the brackets:

        qRegisterMetaType<base::impl::A>("base::impl::A");  // or
        qRegisterMetaType<QVector<base::impl::A>>("QVector<base::impl::A>"); // or
        qRegisterMetaTypeStreamOperators<base::impl::A>("base::impl::A"); // or
        qRegisterMetaTypeStreamOperators<QVector<base::impl::A>>("QVector<base::impl::A");
        ...
        

        The definition as well as most of the examples I see does provide a string part inside the brackets (especially for qRegisterMetaTypeStreamOperators). Which makes me confused as to what is the reason behind adding the string part such as "base::impl::A" inside the brackets and how does it affect the usage?

        Debugging problems caused by these kinds of ambiguity (especially on VS) is difficult and I do not want to make mistakes now which might give me errors later and would consume a lot of my time in figuring out that I registered the type or stream operators incorrectly.

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

          If memory serves well, the version with a QString parameter is geared toward declaring aliases.

          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

          • Login

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