Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

calling QMetaObject::invokeMethod() with variable amount of parameters

Tags:

c++

qt

I´m porting FitNesse´s Slim-server at the moment and I´m kinda stuck right now. What I´m getting are strings like these:

("id_4", "call", "id", "setNumerator", "20")
("id_5", "call", "id", "setSomethingElse", "10", "8")

Where "setNumerator" and "setSomethingElse" are the names of methods that should be invoked and "20","10" and "8" are the arguments that I´m passing.

So my problem right now ist, I don´t know how to use one call to invokeMethod for both methods. My current workaround looks like this:

//(if instructionLength==5)
metaObj->invokeMethod(className, methodName.toAscii().constData(), Qt::DirectConnection,
                                          Q_ARG(QVariant, instructions.at(index).at(4)))

//(if instructionLength==6)
metaObj->invokeMethod(className, methodName.toAscii().constData(), Qt::DirectConnection, Q_RETURN_ARG(QVariant, retArg),
                                          Q_ARG(QVariant, instructions.at(index).at(4)),
                                          Q_ARG(QVariant, instructions.at(index).at(5)))

and so on...

So on the one hand, it seems that every invokeMethod-call needs very specific information, which makes it hard to do it with a variable amount of arguments. On the other hand, there has to be a way so I don´t have to do the same thing two(or later: ten) times.

So the question is, is there another way to do it with one call?

like image 309
LarissaGodzilla Avatar asked Jan 19 '23 13:01

LarissaGodzilla


1 Answers

If you look at the function definition, you'll see there is just one version :

bool QMetaObject::invokeMethod ( QObject * obj, const char * member, QGenericArgument val0 
    = QGenericArgument( 0 ), QGenericArgument val1 = QGenericArgument(), QGenericArgument 
    val2 = QGenericArgument(), QGenericArgument val3 = QGenericArgument(), 
    QGenericArgument val4 = QGenericArgument(), QGenericArgument val5 = 
    QGenericArgument(), QGenericArgument val6 = QGenericArgument(), QGenericArgument val7 
    = QGenericArgument(), QGenericArgument val8 = QGenericArgument(), QGenericArgument 
    val9 = QGenericArgument() )

In your case, this is what I would do :

QGenericArgument argumentTable[ 10 ];

... Fill up your data

QMetaObject::invokeMethod( objet, functionName, argumentTable[ 0 ], argumentTable[ 1 ],
    argumentTable[ 2 ], ... argumentTable[ 9 ] );

The arguments you don't use will be default initialized, which is the default behavior

like image 131
crazyjul Avatar answered Jan 23 '23 14:01

crazyjul