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 -
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/noronYou can take a look into Qt's source code. And you find that Qt use same mechanism for QGenericArgument creation.