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. How to use QString in Enum
Forum Updated to NodeBB v4.3 + New Features

How to use QString in Enum

Scheduled Pinned Locked Moved Solved General and Desktop
9 Posts 6 Posters 856 Views 1 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.
  • D Offline
    D Offline
    dan1973
    wrote on 12 Sept 2024, 07:36 last edited by
    #1

    I want to have an Enum of QStrings: like "ON", "OFF", "IDLE"

    how to do it in Qt?

    J A 2 Replies Last reply 12 Sept 2024, 08:16
    0
    • V Offline
      V Offline
      VRonin
      wrote on 12 Sept 2024, 09:11 last edited by
      #3

      JonB is mostly correct but Qt has some extra magic: QMetaEnum

      class StringEnums
      {
      Q_GADGET
      public:
      enum MachineState{
      ON,OFF,IDLE
      };
      Q_ENUM(MachineState)
      };
      

      Now you can use it:

      const auto statesMetaEnum = QMetaEnum::fromType<StringEnums::MachineState>();
      QStringList allStates;
      for (int i = 0; i < statesMetaEnum.keyCount(); ++i) 
      allStates.append(statesMetaEnum.key(i));
      qDebug() << allStates;
      

      "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
      ~Napoleon Bonaparte

      On a crusade to banish setIndexWidget() from the holy land of Qt

      J D 2 Replies Last reply 12 Sept 2024, 09:28
      4
      • D dan1973
        12 Sept 2024, 07:36

        I want to have an Enum of QStrings: like "ON", "OFF", "IDLE"

        how to do it in Qt?

        J Offline
        J Offline
        JonB
        wrote on 12 Sept 2024, 08:16 last edited by JonB 9 Dec 2024, 08:24
        #2

        @dan1973
        You can't have "an Enum of QStrings" since C++ enum only allows integer values for the explicitly-named enumerators. Do you mean you want an integral enum for which code can produce string "names", when requested, like "ON" for an enum value whose definition has an ON constant in its enumerators? Or maybe you don't really want an "enum" but just, say, some named constants for specific QString values wrapped into their own class? Depends what for/how you intend to use these symbols.

        D 1 Reply Last reply 12 Sept 2024, 09:58
        1
        • V Offline
          V Offline
          VRonin
          wrote on 12 Sept 2024, 09:11 last edited by
          #3

          JonB is mostly correct but Qt has some extra magic: QMetaEnum

          class StringEnums
          {
          Q_GADGET
          public:
          enum MachineState{
          ON,OFF,IDLE
          };
          Q_ENUM(MachineState)
          };
          

          Now you can use it:

          const auto statesMetaEnum = QMetaEnum::fromType<StringEnums::MachineState>();
          QStringList allStates;
          for (int i = 0; i < statesMetaEnum.keyCount(); ++i) 
          allStates.append(statesMetaEnum.key(i));
          qDebug() << allStates;
          

          "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
          ~Napoleon Bonaparte

          On a crusade to banish setIndexWidget() from the holy land of Qt

          J D 2 Replies Last reply 12 Sept 2024, 09:28
          4
          • V VRonin
            12 Sept 2024, 09:11

            JonB is mostly correct but Qt has some extra magic: QMetaEnum

            class StringEnums
            {
            Q_GADGET
            public:
            enum MachineState{
            ON,OFF,IDLE
            };
            Q_ENUM(MachineState)
            };
            

            Now you can use it:

            const auto statesMetaEnum = QMetaEnum::fromType<StringEnums::MachineState>();
            QStringList allStates;
            for (int i = 0; i < statesMetaEnum.keyCount(); ++i) 
            allStates.append(statesMetaEnum.key(i));
            qDebug() << allStates;
            
            J Offline
            J Offline
            JonB
            wrote on 12 Sept 2024, 09:28 last edited by
            #4

            @VRonin said in How to use QString in Enum:

            JonB is mostly correct but Qt has some extra magic: QMetaEnum

            As I said and your code shows the enum must be integral constants. I said OP can add "code can produce string "names", when requested" and that is what the call QMetaEnum::fromType<StringEnums::MachineState>() does. I was waiting to hear how the OP wishes to use these strings.

            It also does not affect the fact that OP cannot use these strings in the same way as the actual underlying integral enum, for example in a switch-case statement. Which is why I asked how the OP actually wishes to use the strings for the enumerators.

            1 Reply Last reply
            2
            • D dan1973 has marked this topic as solved on 12 Sept 2024, 09:57
            • J JonB
              12 Sept 2024, 08:16

              @dan1973
              You can't have "an Enum of QStrings" since C++ enum only allows integer values for the explicitly-named enumerators. Do you mean you want an integral enum for which code can produce string "names", when requested, like "ON" for an enum value whose definition has an ON constant in its enumerators? Or maybe you don't really want an "enum" but just, say, some named constants for specific QString values wrapped into their own class? Depends what for/how you intend to use these symbols.

              D Offline
              D Offline
              dan1973
              wrote on 12 Sept 2024, 09:58 last edited by
              #5

              @JonB Thank you. You were right. I gave 3 values as example. in actuals, i will be using many more enum values.
              Thanks again.

              1 Reply Last reply
              0
              • D dan1973 has marked this topic as solved on 12 Sept 2024, 09:58
              • V VRonin
                12 Sept 2024, 09:11

                JonB is mostly correct but Qt has some extra magic: QMetaEnum

                class StringEnums
                {
                Q_GADGET
                public:
                enum MachineState{
                ON,OFF,IDLE
                };
                Q_ENUM(MachineState)
                };
                

                Now you can use it:

                const auto statesMetaEnum = QMetaEnum::fromType<StringEnums::MachineState>();
                QStringList allStates;
                for (int i = 0; i < statesMetaEnum.keyCount(); ++i) 
                allStates.append(statesMetaEnum.key(i));
                qDebug() << allStates;
                
                D Offline
                D Offline
                dan1973
                wrote on 12 Sept 2024, 09:59 last edited by
                #6

                @VRonin thank you. Your example was helpful to me. good thinking and solution.

                1 Reply Last reply
                0
                • R Offline
                  R Offline
                  Redman
                  wrote on 13 Sept 2024, 08:49 last edited by Redman
                  #7
                  // Only works with Q_ENUMS
                  template <typename T>
                  T
                  keyToEnumValue(const QString& key) {
                     QString tmp = key;
                     return static_cast<T>(
                         QMetaEnum::fromType<T>().keyToValue(tmp.toLocal8Bit()));
                  }
                  
                  // Only works with Q_ENUMS
                  template <typename T>
                  QString
                  valueToEnumKey(const T& val) {
                     QMetaEnum mEnum = QMetaEnum::fromType<T>();
                     return mEnum.valueToKey(val);
                  }
                  
                  1 Reply Last reply
                  0
                  • D dan1973
                    12 Sept 2024, 07:36

                    I want to have an Enum of QStrings: like "ON", "OFF", "IDLE"

                    how to do it in Qt?

                    A Offline
                    A Offline
                    allisons
                    wrote on 13 Sept 2024, 10:31 last edited by allisons
                    #8

                    @dan1973 locksmith said in How to use QString in Enum:

                    I want to have an Enum of QStrings: like "ON", "OFF", "IDLE"

                    how to do it in Qt?
                    In Qt, you can create an enum of QStrings by using a Q_ENUMS macro in combination with a QMap or QStringList to map enum values to QStrings. Here’s a simple example using QMap:

                    #include <QMap>
                    #include <QString>

                    enum State {
                    ON,
                    OFF,
                    IDLE
                    };

                    QMap<State, QString> stateToString = {
                    {ON, "ON"},
                    {OFF, "OFF"},
                    {IDLE, "IDLE"}
                    };

                    // Usage example
                    QString stateString = stateToString[ON];
                    This approach allows you to map enum values to corresponding QStrings and use them as needed.

                    J 1 Reply Last reply 26 Oct 2024, 10:05
                    0
                    • A allisons
                      13 Sept 2024, 10:31

                      @dan1973 locksmith said in How to use QString in Enum:

                      I want to have an Enum of QStrings: like "ON", "OFF", "IDLE"

                      how to do it in Qt?
                      In Qt, you can create an enum of QStrings by using a Q_ENUMS macro in combination with a QMap or QStringList to map enum values to QStrings. Here’s a simple example using QMap:

                      #include <QMap>
                      #include <QString>

                      enum State {
                      ON,
                      OFF,
                      IDLE
                      };

                      QMap<State, QString> stateToString = {
                      {ON, "ON"},
                      {OFF, "OFF"},
                      {IDLE, "IDLE"}
                      };

                      // Usage example
                      QString stateString = stateToString[ON];
                      This approach allows you to map enum values to corresponding QStrings and use them as needed.

                      J Offline
                      J Offline
                      janam
                      wrote on 26 Oct 2024, 10:05 last edited by janam
                      #9

                      @allisons said in How to use QString in Enum:

                      @dan1973 ehall login said in How to use QString in Enum:

                      I want to have an Enum of QStrings: like "ON", "OFF", "IDLE"

                      how to do it in Qt?
                      In Qt, you can create an enum of QStrings by using a Q_ENUMS macro in combination with a QMap or QStringList to map enum values to QStrings. Here’s a simple example using QMap:

                      #include <QMap>
                      #include <QString>

                      enum State {
                      ON,
                      OFF,
                      IDLE
                      };

                      QMap<State, QString> stateToString = {
                      {ON, "ON"},
                      {OFF, "OFF"},
                      {IDLE, "IDLE"}
                      };

                      // Usage example
                      QString stateString = stateToString[ON];
                      This approach allows you to map enum values to corresponding QStrings and use them as needed.

                      Thanks! your code worked in enum. I try many time but failed. Now I try with your code. It worked.

                      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