Results 1 to 6 of 6

Thread: converting strings into enums

  1. #1
    Join Date
    Jun 2011
    Posts
    56
    Qt products
    Qt4
    Thanks
    7

    Default converting strings into enums

    I'm using the code below to convert strings into the BackgroundFlag enum. The problem is, that I cannot have repeat-x, repeat-y or repeat-xy as enums, due to the minus - sign. Is there a workaround possible?

    Qt Code:
    1. class BackgroundUtility : public QObject
    2. {
    3. Q_OBJECT
    4.  
    5. Q_ENUMS(BackgroundFlag)
    6.  
    7. Q_PROPERTY(enum BackgroundFlag backgroundFlag READ backgroundFlag WRITE setBackgroundFlag)
    8.  
    9. public:
    10. enum BackgroundFlag
    11. {
    12. none = 0,
    13. reset = 0,
    14. repeatx,
    15. repeaty,
    16. repeatxy
    17. };
    18.  
    19. enum BackgroundFlag backgroundFlag() const { return flag; }
    20. void setBackgroundFlag(enum BackgroundFlag f) { flag = f; }
    21.  
    22. private:
    23. enum BackgroundFlag flag;
    24. };
    To copy to clipboard, switch view to plain text mode 

  2. #2
    Join Date
    Jun 2011
    Location
    Chennai, India
    Posts
    30
    Qt products
    Qt4
    Platforms
    Windows
    Thanks
    13
    Thanked 6 Times in 3 Posts

    Default Re: converting strings into enums

    You can use _ (underscore). AFAIK, only valid C++ variable names can be used as enum constants

  3. #3
    Join Date
    Jun 2011
    Posts
    56
    Qt products
    Qt4
    Thanks
    7

    Default Re: converting strings into enums

    Sure, I could use the underscore _, but I want to use the minus -.

  4. #4
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,376
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Thanks
    4
    Thanked 5,019 Times in 4,795 Posts
    Wiki edits
    10

    Default Re: converting strings into enums

    Then find a different mechanism.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  5. #5
    Join Date
    Jun 2011
    Posts
    56
    Qt products
    Qt4
    Thanks
    7

    Default Re: converting strings into enums

    Can you give a suggestion? I like to use the Qt generator mechanism, because I like to avoid all those qstrcmps.

  6. #6
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,376
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Thanks
    4
    Thanked 5,019 Times in 4,795 Posts
    Wiki edits
    10

    Default Re: converting strings into enums

    Those comparisons would be there anyway, just hidden behind autogenerated code. If you really want to use this approach then use underscore as suggested and just replace all hypens from the userland with underscores on the fly when accessing the enum metadata.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


Similar Threads

  1. How to use enums in qml
    By nightroad in forum Qt Quick
    Replies: 0
    Last Post: 29th June 2011, 14:24
  2. Save enums in QSettings
    By redhat in forum Qt Programming
    Replies: 4
    Last Post: 22nd January 2009, 12:22
  3. Q_ENUMS on C-style global enums
    By luf in forum Qt Programming
    Replies: 2
    Last Post: 8th July 2008, 20:11
  4. Using enums in QtScript
    By Orphelic in forum Qt Programming
    Replies: 1
    Last Post: 29th November 2007, 12:55
  5. how to use enums
    By soul_rebel in forum General Programming
    Replies: 3
    Last Post: 23rd March 2006, 22:49

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
  •  
Qt is a trademark of The Qt Company.