调用QMetaObject :: invokeMethod()与可变量的参数 [英] calling QMetaObject::invokeMethod() with variable amount of parameters

查看:182
本文介绍了调用QMetaObject :: invokeMethod()与可变量的参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在移植FitNesse的Slim服务器,我现在卡住了。
我得到的是以下字符串:

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")

其中setNumerator和setSomethingElse是应该调用的方法的名称,20,10和8我传递的参数。


所以我现在的问题是,我不知道如何使用一个调用invokeMethod两种方法。我当前的解决方法是这样的:

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

等等...


所以一方面,似乎每个invokeMethod调用都需要非常具体的信息,这使得它很难做它具有可变量的参数。另一方面,必须有一种方法,所以我不必做同样的事情两(或后来:十)次。


所以问题是,是否有另一种方式

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?

推荐答案

如果你看一下函数定义,你会看到只有一个版本:

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

这篇关于调用QMetaObject :: invokeMethod()与可变量的参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆