How to use QString in Enum
-
wrote on 12 Sept 2024, 07:36 last edited by
I want to have an Enum of QStrings: like "ON", "OFF", "IDLE"
how to do it in Qt?
-
wrote on 12 Sept 2024, 09:11 last edited by
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;
-
wrote on 12 Sept 2024, 08:16 last edited by JonB 9 Dec 2024, 08:24
@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 integralenum
for which code can produce string "names", when requested, like"ON"
for anenum
value whose definition has anON
constant in its enumerators? Or maybe you don't really want an "enum" but just, say, some named constants for specificQString
values wrapped into their own class? Depends what for/how you intend to use these symbols. -
wrote on 12 Sept 2024, 09:11 last edited by
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;
-
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;
wrote on 12 Sept 2024, 09:28 last edited by@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 callQMetaEnum::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 aswitch-case
statement. Which is why I asked how the OP actually wishes to use the strings for the enumerators. -
-
@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 integralenum
for which code can produce string "names", when requested, like"ON"
for anenum
value whose definition has anON
constant in its enumerators? Or maybe you don't really want an "enum" but just, say, some named constants for specificQString
values wrapped into their own class? Depends what for/how you intend to use these symbols. -
-
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;
-
wrote on 13 Sept 2024, 08:49 last edited by Redman
// 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); }
-
wrote on 13 Sept 2024, 10:31 last edited by allisons
@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. -
@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.wrote on 26 Oct 2024, 10:05 last edited by janam@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.