Results 1 to 5 of 5

Thread: How to convert QString to enum?

  1. #1
    Join Date
    Oct 2011
    Posts
    35
    Thanked 9 Times in 3 Posts
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11

    Default How to convert QString to enum?

    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!

  2. #2
    Join Date
    Feb 2011
    Location
    Bangalore
    Posts
    207
    Thanks
    20
    Thanked 28 Times in 27 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: How to convert QString to enum?

    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().

  3. #3
    Join Date
    Oct 2011
    Posts
    35
    Thanked 9 Times in 3 Posts
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11

    Default Re: How to convert QString to enum?

    Thank you pkj. Would you mind providing a brief example of how this is done in code?

    Thanks!

  4. #4
    Join Date
    Mar 2016
    Posts
    14
    Thanks
    1
    Thanked 2 Times in 1 Post
    Qt products
    Qt4 Qt5
    Platforms
    Windows

    Default Re: How to convert QString to enum?

    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:
    1. enum MyEnum {VAL_ENUM1, VAL_ENUM2};
    2. m_variable = MyEnum(QString("1").toUInt()); // now m_variable = VAL_ENUM2
    To copy to clipboard, switch view to plain text mode 

    (For all people like me who search for it years after)

  5. #5
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,230
    Thanks
    302
    Thanked 864 Times in 851 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: How to convert QString to enum?

    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.

Similar Threads

  1. convert Qstring into int
    By sabbu in forum Newbie
    Replies: 8
    Last Post: 8th June 2011, 17:31
  2. Comparing a Qstring with a value defined in an enum
    By AndresBarbaRoja in forum Newbie
    Replies: 5
    Last Post: 24th March 2011, 17:11
  3. How can we convert enum to int?
    By learning_qt in forum Qt Programming
    Replies: 2
    Last Post: 26th January 2010, 17:08
  4. convert QString to int
    By mattia in forum Newbie
    Replies: 2
    Last Post: 4th January 2008, 10:10
  5. how to convert int to QString?
    By phillip_Qt in forum Newbie
    Replies: 2
    Last Post: 5th October 2007, 09:07

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.