Set class member value by its name
-
21-07-2021 - |
Domanda
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.
Soluzione
You have two ways of doing this:
- Declare your members as properties by using the
Q_PROPERTY
macro. Then you can set it by name usingQObject::setProperty(..)
. - Declare a setter method with the property name (plus a 'set' prepended for example) and the
Q_INVOKABLE
macro (or just make it aslot
), then useQMetaObject::invokeMethod(..)
to call it by name.
Altri suggerimenti
It's not possible in c++. The only way is an associative array
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow