简体   繁体   中英

Set class member value by its name

I've a class, derived of QObject and it has some members. I load a configuration file with param value syntax. Each param is a member of the class and I want to set it's value to 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.

You have two ways of doing this:

  1. Declare your members as properties by using the Q_PROPERTY macro. Then you can set it by name using 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.

It's not possible in c++. The only way is an associative array

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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