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. [SOLVED] How does Q_DECLARE_METATYPE work?
Forum Update on Monday, May 27th 2025

[SOLVED] How does Q_DECLARE_METATYPE work?

Scheduled Pinned Locked Moved General and Desktop
9 Posts 3 Posters 14.2k 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.
  • W Offline
    W Offline
    willypuzzle
    wrote on last edited by
    #1

    I read on documentation that Q_DECLARE_METATYPE makes available type to QVariant, for example (I got it from documentation):

    @

    struct MyStruct
    {
    int i;
    ...
    };

    Q_DECLARE_METATYPE(MyStruct)

    @

    @
    MyStruct s;
    QVariant var;
    var.setValue(s); // copy s into the variant

    ...

    // retrieve the value
    MyStruct s2 = var.value<MyStruct>();
    @

    but can I call, for example, var.toString()?
    How do I implement it?

    I can't get the usefulness about all above and how it works ....
    Does someone explain me?

    1 Reply Last reply
    0
    • sierdzioS Offline
      sierdzioS Offline
      sierdzio
      Moderators
      wrote on last edited by
      #2

      I don't think ::toString() will work. But you know what your struct is, so you can stringify it yourself :)

      The usefulness (although many will disagree) is that you can have a single type to pass between various parts of your code, without bothering about strict typing too much. Think about widget property browser in Qt Designer - some properties contain text, other just a boolean value, others might use some struct or binary data (ok, maybe not in Designer. But there are use cases where that would be true). You could write a super flexible set of classes and interfaces to make it work in "standard c++ way", but using a QVariant is simpler + the resulting code is in many cases easier to understand later.

      (Z(:^

      1 Reply Last reply
      0
      • W Offline
        W Offline
        willypuzzle
        wrote on last edited by
        #3

        So, for example, I can create a var (see above) as QVariant and pass it to all my functions without bothering about type ...
        That could be complete if QVariant has a function to determine the type of object it carries inside ..
        Have I understood?

        1 Reply Last reply
        0
        • sierdzioS Offline
          sierdzioS Offline
          sierdzio
          Moderators
          wrote on last edited by
          #4

          Yes you are correct.

          QVariant does have "::typeName()":http://qt-project.org/doc/qt-4.8/qvariant.html#typeName method, but of course it works only for built-in types. If your custom type is a QObject, you can always get the class name using QMetaObject.

          (Z(:^

          1 Reply Last reply
          0
          • W Offline
            W Offline
            willypuzzle
            wrote on last edited by
            #5

            But if I have to call for example "var.value<MyStruct>()" I have to know MyStruct name first to obtain the object ... how can I use QMetaObject?

            1 Reply Last reply
            0
            • sierdzioS Offline
              sierdzioS Offline
              sierdzio
              Moderators
              wrote on last edited by
              #6

              Ah, there, you've hit the limit of QVariant :) In use cases I've encountered that has not been a problem, though. When I pass a special value to QVariant, I usually know about it, and know what to cast it to. Maybe passing the struct itself is better in your case?

              (Z(:^

              1 Reply Last reply
              0
              • sierdzioS Offline
                sierdzioS Offline
                sierdzio
                Moderators
                wrote on last edited by
                #7

                I was wrong - you've almost hit the limit. There is "QVariant::userType()":http://qt-project.org/doc/qt-4.8/qvariant.html#userType and user type constructor: "QVariant":http://qt-project.org/doc/qt-4.8/qvariant.html#QVariant-3.

                (Z(:^

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

                  to make sure what type your QVariant (with custom object data) has, you can do the following:
                  @int metaTypeId = qRegisterMetaType<MyType>("MyType"); //will return a value >QVariant::UserType
                  ....
                  QVariant v = ... ;//get your variant
                  if( v.userType() == metaTypeId )
                  ...
                  @
                  Since QVariant::userType() will retun the MetaObject id if it's not a built in type.

                  Note: it's also safe to call qRegisterMetaType() multiple times. It will return the same result if it was called before. So you can call it from your constructor.

                  --- 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
                  • W Offline
                    W Offline
                    willypuzzle
                    wrote on last edited by
                    #9

                    Thanks very much to all for explanations :)

                    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