Enum value to QString
-
@raven-worx Thank you very much.
-
@raven-worx hi,
I have changed
enum CurrentState { Buzy, StandBy, Error };
to,
enum class CurrentState : qint32{ Buzy, StandBy, Error }; ... qRegisterMetaType<ClassName::CurrentState>(); ... Q_DECLARE_METATYPE(ClassName::CurrentState)
to be able to use the enum from QML
Button{ visible : !ClassName.CurrentState.Busy }
Now i need to adapt the statement you gave me :
QString message = QMetaEnum::fromType<ClassName::CurrentState>().valueToKey(m_state)
output : cannot initialize a parameter of type 'int' with an lvalue of type 'ClassName::CurrentState'
is there still a possibility to do this 'simply' or i will have to use a switch and manually assign values?
switch(m_state){ case CalassName::Busy : message = "Busy";break; ... }
Thank you
-
@LeLev
- did you add the
Q_ENUM
as i said? - try
QMetaEnum::fromType<ClassName::CurrentState>().valueToKey( int(m_state) )
- did you add the
-
@raven-worx
as you can see in my first code the Q_ENUM is there from the beginnig thank you :)QMetaEnum::fromType<ClassName::CurrentState>().valueToKey( int(m_state) )
this worked, now i can create a QString with my state name and get it from QML .
but i have one more thing to solve : i can reach the enum from my QML code :
Connections{ target: machine onStateChanged:{ if(machine.state === Machine.CurrentState.Buzy){ console.log("machine buzy...") } } }
But if i assign the state name to a QML Text i get the index
Text{ text:machine.state // text : Machine.CurrentState.Buzy ? "Buzy" : ... ? }
Thank you
-
@LeLev of course, qml still handles this an integer, internaly
you''ll need someting like
QINVOKABLE QString stateToString(CurrentState state ){return QMetaEnum::fromTypeClassName::CurrentState().valueToKey( int(m_state) );}
Text{ text: Machine.stateToString(machine.state) }
as a JS-expression, the text will update when state changes.
-
@LeLev said in Enum value to QString:
But if i assign the state name to a QML Text i get the index
QML (better said the QML engine) doesn't have this capabilities AFAIK.
You can however create an INVOKABLE C++ method with does the translation for you, following the same principle.Edit: yes, just like @J-Hilk showed :)
-
Thank you @raven-worx and @J-Hilk
Just to be exact if someone reads this in the future
Text{ text: machine.stateToString(machine.state) }
Here machine is instance of class Machine so i use lowercase letter
setContextProperty("machine",&machine);
I belive @J-Hilk just did a missclick.@J.Hilk said in Enum value to QString:
as a JS-expression, the text will update when state changes.
Since this is a Q_INVOKABLE i have to call it every time no ?
this is not refreshing the text :
Text { id: machineState text: machine.stateToString() }
I can do like this :
Connections{ target: machine onStateChanged:{ machineState.text = machine.stateToString() } }
Correct me if im wrong please. Maybe there is a trick with that Capital Machine.stateToString(machine.state) that i don't know yet.
-
@LeLev said in Enum value to QString:
Since this is a Q_INVOKABLE i have to call it every time no ?
if the call contains another property (
machine.state
in your case) the rules for property binding kick in.
So no you do not need to call it every time.works automagically:
machine.stateToString(machine.state)
doesnt work (needs manual execution):machine.stateToString()
-> if you want to use this way, you can create a Q_PROPERTYstateStr
for example which returns the string representation of the current state value. Note in order to take advantage property binding, the property needs to needs to declare aNOTIFY
signal and call it whenever the value has changed (whenever the state property of your machine class has changed)Maybe there is a trick with that Capital Machine.stateToString(machine.state) that i don't know yet.
this would work if the class has been registered as singleton type.
-
@raven-worx said in Enum value to QString:
if you want to use this way, you can create a Q_PROPERTY stateStr for example which returns the string representation of the current state value. Note in order to take advantage property binding, the property needs to needs to declare a NOTIFY signal and call it whenever the value has changed (whenever the state property of your machine class has changed)
This is what i do generally.
@raven-worx said in Enum value to QString:
if the call contains another property (machine.state in your case) the rules for property binding kick in.
@raven-worx said in Enum value to QString:
this would work if the class has been registered as singleton type.
awsome ! Thank you very much !
This is definitely solved!
-
@raven-worx awesome, this pointed me in the right direction. In my case I needed the keyboard combo and key name in string form, so QMetaEnum::fromType was the call I was trying to find, thanks for that.