SPlatten
SPlatten

Reputation: 5760

Qt how to translate string to function?

Is it possible to translate a string (QString) from a literal into a function and call it?

For example, I have the a string containing "clicked", I want to translate this into a function and call it, something like:

eval("clicked")()

Upvotes: 0

Views: 366

Answers (1)

of course you can: you have in fact 2 options,

create a Map with string, function. and get the value by key and invoke it

or(a little bit more complex)

the methods must be of a classs that inherits the QObject class and the methods are slots or "invokable" functions,

and you need to use the

QMetaObject::invokeMethod method...

example:

FOO::MyClass obj;
QMetaObject::invokeMethod(&obj, "foo", Qt::DirectConnection);

Upvotes: 1

Related Questions