[英]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: 你有两种方法:
Q_PROPERTY
macro. Q_PROPERTY
宏将您的成员声明为属性。 Then you can set it by name using QObject::setProperty(..)
. QObject::setProperty(..)
按名称设置它。 Q_INVOKABLE
macro (or just make it a slot
), then use QMetaObject::invokeMethod(..)
to call it by name. 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.