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. Using QVariant value on QMetaObject::invokeMethod
Forum Update on Monday, May 27th 2025

Using QVariant value on QMetaObject::invokeMethod

Scheduled Pinned Locked Moved General and Desktop
5 Posts 4 Posters 7.1k 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.
  • M Offline
    M Offline
    marcomeinhardt
    wrote on last edited by
    #1

    Hello!

    I would like to dynamically call a method using invokeMethod and also have the parameters passed in dynamically.
    The solution I've found looks pretty much like this:

    @
    void COperation::execute(QObject* Obj, const char* InvokableMethod, QVariant Var)
    {
    QGenericArgument Argument;

    if (Var.isValid())
    {
    Argument = QGenericArgument(Var.typeName(), Var.data());
    }

    QMetaObject::invokeMethod(Obj, InvokableMethod, Argument);
    }
    @

    The solution is working but there is one thing that worries me: The Qt docs say that QGenericArgument is an internal helper class that should not be used directly but only by calling Q_ARG ("QGenericArgument":http://doc.qt.nokia.com/4.7-snapshot/qgenericargument.html#details). However, I don't see how to turn a QVariant into a QGenericArgument instance using Q_ARG. So I ended up using the QGenericArgument constructor.

    Now my question is whether this is a legitimate solution or if it is critical to bypass the Q_ARG macro?
    Any hints or alternatives would would be appreciated.

    Thanks,
    Marco

    1 Reply Last reply
    0
    • G Offline
      G Offline
      giesbert
      wrote on last edited by
      #2

      If it's internal, it means it may change.
      You can use it but no garantuee that it will work with the next release.
      But I think it will work, due to one fact:

      Q_ARG is not an interbal macro. And QArg will use some of those constructors :-)

      Nokia Certified Qt Specialist.
      Programming Is Like Sex: One mistake and you have to support it for the rest of your life. (Michael Sinz)

      1 Reply Last reply
      0
      • A Offline
        A Offline
        andre
        wrote on last edited by
        #3

        Are you sure the code above is what you actually do? Because it seems you never actually use the Argument variable you initialized. I would have expected this to appear on line 10:

        @
        QMetaObject::invokeMethod(Obj, InvokableMethod, Argument);
        @

        1 Reply Last reply
        0
        • M Offline
          M Offline
          marcomeinhardt
          wrote on last edited by
          #4

          Thanks Andre,

          you are actually right. I just tried to strip off all unnecessary code for this example and this how the mistake came in. Sorry.
          Note: I now updated my first post.

          1 Reply Last reply
          0
          • Hamed.MasafiH Offline
            Hamed.MasafiH Offline
            Hamed.Masafi
            wrote on last edited by
            #5

            @marcomeinhardt : Your code is correct. There are no other solution for this case. I'v been used this solution in my project and works finely.
            https://github.com/HamedMasafi/noron

            You can take a look into Qt's source code. And you find that Qt use same mechanism for QGenericArgument creation.

            Remote object sharing (OO RPC)
            http://forum.qt.io/topic/60680/remote-object-sharing-oo-rpc-solved

            Advanced, Powerful and easy to use ORM for Qt5
            https://forum.qt.io/topic/67417/advanced-powerful-and-easy-to-use-orm-for-qt5

            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