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
Forum Updated to NodeBB v4.3 + New Features

Enum value to QString

Scheduled Pinned Locked Moved Solved General and Desktop
12 Posts 4 Posters 19.4k 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.
  • O Offline
    O Offline
    ODБOï
    wrote on 19 Dec 2018, 15:26 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 ?

    R 1 Reply Last reply 19 Dec 2018, 15:52
    0
    • O ODБOï
      19 Dec 2018, 15:26

      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 ?

      R Offline
      R Offline
      raven-worx
      Moderators
      wrote on 19 Dec 2018, 15:52 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

      O 1 Reply Last reply 19 Dec 2018, 15:56
      5
      • R raven-worx
        19 Dec 2018, 15:52

        @LeLev

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

        @raven-worx Thank you very much.

        1 Reply Last reply
        0
        • O Offline
          O Offline
          ODБOï
          wrote on 20 Dec 2018, 11:00 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

          R 1 Reply Last reply 20 Dec 2018, 13:11
          0
          • O ODБOï
            20 Dec 2018, 11:00

            @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

            R Offline
            R Offline
            raven-worx
            Moderators
            wrote on 20 Dec 2018, 13:11 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

            O osirisgothraO 2 Replies Last reply 20 Dec 2018, 13:42
            5
            • R raven-worx
              20 Dec 2018, 13:11

              @LeLev

              1. did you add the Q_ENUM as i said?
              2. try QMetaEnum::fromType<ClassName::CurrentState>().valueToKey( int(m_state) )
              O Offline
              O Offline
              ODБOï
              wrote on 20 Dec 2018, 13:42 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 R 2 Replies Last reply 20 Dec 2018, 13:47
              0
              • O ODБOï
                20 Dec 2018, 13:42

                @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 Offline
                J.HilkJ Offline
                J.Hilk
                Moderators
                wrote on 20 Dec 2018, 13:47 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
                • O ODБOï
                  20 Dec 2018, 13:42

                  @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

                  R Offline
                  R Offline
                  raven-worx
                  Moderators
                  wrote on 20 Dec 2018, 13:48 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
                  • O Offline
                    O Offline
                    ODБOï
                    wrote on 20 Dec 2018, 14:16 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.

                    R 1 Reply Last reply 20 Dec 2018, 14:28
                    0
                    • O ODБOï
                      20 Dec 2018, 14:16

                      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.

                      R Offline
                      R Offline
                      raven-worx
                      Moderators
                      wrote on 20 Dec 2018, 14:28 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

                      O 1 Reply Last reply 20 Dec 2018, 14:53
                      3
                      • R raven-worx
                        20 Dec 2018, 14:28

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

                        O Offline
                        O Offline
                        ODБOï
                        wrote on 20 Dec 2018, 14:53 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
                        • R raven-worx
                          20 Dec 2018, 13:11

                          @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 19 Nov 2024, 18:19 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