Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Enum value to QString

Enum value to QString

Scheduled Pinned Locked Moved Solved General and Desktop
12 Posts 4 Posters 21.6k Views 2 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • ODБOïO Offline
    ODБOïO Offline
    ODБOï
    wrote on last edited by
    #1

    Hi,
    In one of my classes i have an enum defined like this :

     Q_PROPERTY(CurrentState state READ state WRITE setState NOTIFY stateChanged)
     enum CurrentState {
            Buzy, StandBy, Error
        };
    Q_ENUM(CurrentState)
        CurrentState state()const{
            return m_state;
        }
        void setState(const CurrentState &st){
            if(m_state!=st){
                m_state=st;
                emit stateChanged(m_state);
            }
        }
    private:
    CurrentState m_state;
    
    

    i have a function that will handle the stateChanged signal

    in that function i can do this :

    qDebug()<<"State changed : " << m_state;
    // output will be states changed : ClassName::Buzy // or StandBy or Error

    QString stateMessage = QString("states changed : %1").arg(m_state);
    // here output is : state changed : 0 // or 1 or 2 (so the index of the m_state )

    How to assigne the enum value rather than index to my QString stateMessage ?

    raven-worxR 1 Reply Last reply
    0
    • ODБOïO ODБOï

      Hi,
      In one of my classes i have an enum defined like this :

       Q_PROPERTY(CurrentState state READ state WRITE setState NOTIFY stateChanged)
       enum CurrentState {
              Buzy, StandBy, Error
          };
      Q_ENUM(CurrentState)
          CurrentState state()const{
              return m_state;
          }
          void setState(const CurrentState &st){
              if(m_state!=st){
                  m_state=st;
                  emit stateChanged(m_state);
              }
          }
      private:
      CurrentState m_state;
      
      

      i have a function that will handle the stateChanged signal

      in that function i can do this :

      qDebug()<<"State changed : " << m_state;
      // output will be states changed : ClassName::Buzy // or StandBy or Error

      QString stateMessage = QString("states changed : %1").arg(m_state);
      // here output is : state changed : 0 // or 1 or 2 (so the index of the m_state )

      How to assigne the enum value rather than index to my QString stateMessage ?

      raven-worxR Offline
      raven-worxR Offline
      raven-worx
      Moderators
      wrote on last edited by raven-worx
      #2

      @LeLev

      QMetaEnum::fromType<ClassName::MyEnum>().valueToKey(value) // MyEnum needs to be declared with Q_ENUM macro
      

      --- SUPPORT REQUESTS VIA CHAT WILL BE IGNORED ---
      If you have a question please use the forum so others can benefit from the solution in the future

      ODБOïO 1 Reply Last reply
      5
      • raven-worxR raven-worx

        @LeLev

        QMetaEnum::fromType<ClassName::MyEnum>().valueToKey(value) // MyEnum needs to be declared with Q_ENUM macro
        
        ODБOïO Offline
        ODБOïO Offline
        ODБOï
        wrote on last edited by
        #3

        @raven-worx Thank you very much.

        1 Reply Last reply
        0
        • ODБOïO Offline
          ODБOïO Offline
          ODБOï
          wrote on last edited by
          #4

          @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

          raven-worxR 1 Reply Last reply
          0
          • ODБOïO ODБOï

            @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

            raven-worxR Offline
            raven-worxR Offline
            raven-worx
            Moderators
            wrote on last edited by
            #5

            @LeLev

            1. did you add the Q_ENUM as i said?
            2. try QMetaEnum::fromType<ClassName::CurrentState>().valueToKey( int(m_state) )

            --- SUPPORT REQUESTS VIA CHAT WILL BE IGNORED ---
            If you have a question please use the forum so others can benefit from the solution in the future

            ODБOïO osirisgothraO 2 Replies Last reply
            5
            • raven-worxR raven-worx

              @LeLev

              1. did you add the Q_ENUM as i said?
              2. try QMetaEnum::fromType<ClassName::CurrentState>().valueToKey( int(m_state) )
              ODБOïO Offline
              ODБOïO Offline
              ODБOï
              wrote on last edited by
              #6

              @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

              J.HilkJ raven-worxR 2 Replies Last reply
              0
              • ODБOïO ODБOï

                @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

                J.HilkJ Online
                J.HilkJ Online
                J.Hilk
                Moderators
                wrote on last edited by J.Hilk
                #7

                @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.


                Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct


                Q: What's that?
                A: It's blue light.
                Q: What does it do?
                A: It turns blue.

                1 Reply Last reply
                1
                • ODБOïO ODБOï

                  @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

                  raven-worxR Offline
                  raven-worxR Offline
                  raven-worx
                  Moderators
                  wrote on last edited by raven-worx
                  #8

                  @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 :)

                  --- SUPPORT REQUESTS VIA CHAT WILL BE IGNORED ---
                  If you have a question please use the forum so others can benefit from the solution in the future

                  1 Reply Last reply
                  1
                  • ODБOïO Offline
                    ODБOïO Offline
                    ODБOï
                    wrote on last edited by ODБOï
                    #9

                    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.

                    raven-worxR 1 Reply Last reply
                    0
                    • ODБOïO ODБOï

                      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.

                      raven-worxR Offline
                      raven-worxR Offline
                      raven-worx
                      Moderators
                      wrote on last edited by raven-worx
                      #10

                      @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_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)

                      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.

                      --- SUPPORT REQUESTS VIA CHAT WILL BE IGNORED ---
                      If you have a question please use the forum so others can benefit from the solution in the future

                      ODБOïO 1 Reply Last reply
                      3
                      • raven-worxR raven-worx

                        @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_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)

                        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.

                        ODБOïO Offline
                        ODБOïO Offline
                        ODБOï
                        wrote on last edited by
                        #11

                        @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!

                        1 Reply Last reply
                        0
                        • raven-worxR raven-worx

                          @LeLev

                          1. did you add the Q_ENUM as i said?
                          2. try QMetaEnum::fromType<ClassName::CurrentState>().valueToKey( int(m_state) )
                          osirisgothraO Offline
                          osirisgothraO Offline
                          osirisgothra
                          wrote on last edited by
                          #12

                          @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.

                          I'm truly glad you r/offmychess t finally, but please don't go too far, because you r/beyondvoxels and that implies that u r/donewithlife. Oh well time to git back to the lab, because azure sea here, I have a lot of work to do...

                          1 Reply Last reply
                          0

                          • Login

                          • Login or register to search.
                          • First post
                            Last post
                          0
                          • Categories
                          • Recent
                          • Tags
                          • Popular
                          • Users
                          • Groups
                          • Search
                          • Get Qt Extensions
                          • Unsolved