Using QVariant value on QMetaObject::invokeMethod



  • 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



  • 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 :-)



  • 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);
    @



  • 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.



  • @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.


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.