简体   繁体   English

Qt如何将字符串转换为函数?

[英]Qt how to translate string to function?

Is it possible to translate a string (QString) from a literal into a function and call it? 是否可以将字符串(QString)从文字转换为函数并调用它?

For example, I have the a string containing "clicked", I want to translate this into a function and call it, something like: 例如,我有一个包含“ clicked”的字符串,我想将此转换为一个函数并调用它,例如:

eval("clicked")()

of course you can: you have in fact 2 options, 当然可以:实际上,您有2种选择,

create a Map with string, function. 使用字符串,函数创建Map。 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, 方法必须是继承QObject类的类,并且方法是插槽或“可调用”函数,

and you need to use the 而您需要使用

QMetaObject::invokeMethod method... QMetaObject :: invokeMethod方法...

example: 例:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM