简体   繁体   English

按名称设置类成员值

[英]Set class member value by its name

I've a class, derived of QObject and it has some members. 我有一个类,派生自QObject ,它有一些成员。 I load a configuration file with param value syntax. 我加载了一个带有param value语法的配置文件。 Each param is a member of the class and I want to set it's value to value . 每个param都是类的成员,我想将它的值设置为value

How is it possible, if it actually is? 怎么可能,如果它真的是? Thanks much! 非常感谢!

PS I could have made an associative array but it's not the way I prefer. PS我可以制作一个关联数组,但这不是我喜欢的方式。

You have two ways of doing this: 你有两种方法:

  1. Declare your members as properties by using the Q_PROPERTY macro. 使用Q_PROPERTY宏将您的成员声明为属性。 Then you can set it by name using QObject::setProperty(..) . 然后你可以使用QObject::setProperty(..)按名称设置它。
  2. Declare a setter method with the property name (plus a 'set' prepended for example) and the Q_INVOKABLE macro (or just make it a slot ), then use QMetaObject::invokeMethod(..) to call it by name. 声明一个带有属性名称的setter方法(加上例如前面加'set')和Q_INVOKABLE宏(或者只是使它成为一个slot ),然后使用QMetaObject::invokeMethod(..)按名称调用它。

It's not possible in c++. 这在c ++中是不可能的。 The only way is an associative array 唯一的方法是关联数组

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

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