Results 1 to 5 of 5

Thread: enum to QString

  1. #1
    Join Date
    Mar 2011
    Posts
    120
    Thanked 1 Time in 1 Post
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default enum to QString

    Hi everyone,
    I am trying to get the string value of enum.
    My classa.h:
    Qt Code:
    1. class classA : public QObject
    2. {
    3. Q_OBJECT
    4. Q_ENUMS(errType)
    5. public:
    6. enum errType{SUCCESS,ERROR};
    7. classA();
    8.  
    9. };
    To copy to clipboard, switch view to plain text mode 

    main.cpp:
    Qt Code:
    1. int index = classA::staticMetaObject.indexOfEnumerator("errType");
    2. qDebug()<<index;
    3.  
    4. const QMetaObject & mo = classA::staticMetaObject;
    5. qDebug() << "Available enums in" << mo.className() << ":" << mo.enumeratorCount();
    To copy to clipboard, switch view to plain text mode 

    If i will compile with Q_OBJECT then undefined reference to classA::staticMetaObject and undefined reference to vtable for classA built error will come.
    If i will compile without Q_OBJECT then compilation will be fine.But index will be -1 and enumetator count will be 0.

    Please tell how i will register the enum and get the string value of enum.

    Thanks.

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

    Default Re: enum to QString

    Rerun qmake.
    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.


  3. #3
    Join Date
    Mar 2011
    Posts
    120
    Thanked 1 Time in 1 Post
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: enum to QString

    Thanks it is working.

    Can i register a global enum using Q_ENUMS ?

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

    Default Re: enum to QString

    What do you consider a "global enum"?
    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
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: enum to QString

    I think the OP means an enum declared outside a class

    The enum must be inside a class with Q_OBJECT or Q_GADGET macros or the supporting infrastructure is not there for QMetaObject and friends.

Similar Threads

  1. How to convert QString to enum?
    By fonzi337 in forum Newbie
    Replies: 4
    Last Post: 21st July 2018, 20:09
  2. Enum To QString Conversion
    By yagabey in forum Qt Programming
    Replies: 6
    Last Post: 5th March 2012, 14:28
  3. Replies: 2
    Last Post: 11th August 2011, 15:42
  4. Comparing a Qstring with a value defined in an enum
    By AndresBarbaRoja in forum Newbie
    Replies: 5
    Last Post: 24th March 2011, 16:11
  5. Replies: 4
    Last Post: 31st January 2008, 20:44

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.