Reputation: 5760
Is it possible to call a static method?
I am using:
QMetaObject::invokeMethod(this
,strThread.toLatin1()
,Qt::DirectionConnection
,Q_ARG(clsThread*, this));
This works, however I want to call a static method and that doesn't work, is it possible to invoke a static method?
I've tried assigning to strThread: "clsScriptHelper::threadFun"
, but this doesn't work.
I couldn't get the static method to work, so I've implemented an alternative solution. In my derived thread class I added a member which has the type:
QObject* mpobjClass;
I then added a method to set this:
void setClassPtr(QObject* pobjClass) { mpobjClass = pobjClass; }
My invoke now looks like this:
QMetaObject::invokeMethod(mpobjClass
,strThread.toLatin1()
,Qt::DirectConnection
,Q_ARG(clsThread*, this));
This works for me.
Upvotes: 1
Views: 2422
Reputation: 98445
Why would you do that? invokeMethod
is for when the object has a dynamic type and you got an instance and want to call a method on that instance in spite of not knowing anything about the type.
What you seem to want to do is to dispatch static methods based on a string name. That's not hard and doesn't require invokeMethod
:
class Class {
public:
static void method1();
static void method2();
static void dispatchByName(const char *name) {
if (QByteArrayLiteral("method1") == name) method1();
else if (QByteArrayLiteral("method2") == name) method2();
}
};
Upvotes: 0
Reputation: 48278
yes, you can, but the method must be annotates as invocable i.e Q_INVOKABLE
see what qt documented about it...
Foo obj;
QMetaObject::invokeMethod(&obj, "amSomething", Qt::DirectConnection);
and Foo should look like:
class Foo : public QObject
{
Q_OBJECT
public:
explicit Foo(QObject *parent = nullptr);
Q_INVOKABLE static void amSomething(){ qDebug() << "am in static";}
signals:
public slots:
};
Upvotes: 2