4

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?

1 Answer 1

10

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

Sign up to request clarification or add additional context in comments.

2 Comments

It seemed to work at first, but it doesn´t. It tries to invoke the right method with the right argument, but it fails. for example: AddFixture::setSummand1(QVariant) normally works, but now it doesn´t
The problem was that I converted the methodName-String toLower, so the names didn´t match. But now it works, so thank you.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.