Hi,
I'm wondering if there is a standard method provided to convert a string to its corresponding enum. Something similar to .NET/C#'s Enum.Parse or Enum.TryParse.
Thanks!
Hi,
I'm wondering if there is a standard method provided to convert a string to its corresponding enum. Something similar to .NET/C#'s Enum.Parse or Enum.TryParse.
Thanks!
Qt provides QMetaEnum for the purpose.
for each enum whose name you want automatically at runtime, register it with Q_ENUMS, then retrieve its name with QMetaEnum::name() and its keys' names with QMetaEnum::key().
Thank you pkj. Would you mind providing a brief example of how this is done in code?
Thanks!
Hi,
I'm not an expert, but just a cast works for me, and it's easier to use if only used once in the code...
Qt Code:
enum MyEnum {VAL_ENUM1, VAL_ENUM2};To copy to clipboard, switch view to plain text mode
(For all people like me who search for it years after)
QMetaEnum is the correct way to go. QString::toUInt() works only when the name of the enum is actually its value (eg. "1" -> 1). With QMetaEnum, both the name of the enum and its value can be anything.
<=== The Great Pumpkin says ===>
Please use CODE tags when posting source code so it is more readable. Click "Go Advanced" and then the "#" icon to insert the tags. Paste your code between them.
Bookmarks