Results 1 to 3 of 3

Thread: How can we convert enum to int?

Threaded View

Previous Post Previous Post   Next Post Next Post
  1. #3
    Join Date
    Oct 2009
    Location
    Mexico
    Posts
    81
    Thanks
    6
    Thanked 10 Times in 10 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: How can we convert enum to int?

    Qt has a powerfull introspection system. using QMetaObject can access to all data generated for MOC.
    enums, properties, signals and slots declared in the class

    the follow code get the the values of the enum ViewportAnchorin the class QGraphicsView
    Qt Code:
    1. #include <QApplication>
    2. #include <QMetaEnum>
    3. #include <QMetaType>
    4. #include <QGraphicsView>
    5. #include <qDebug>
    6.  
    7.  
    8. int main(int argc, char *argv[])
    9. {
    10. QApplication a(argc, argv);
    11.  
    12. QGraphicsView * examples = new QGraphicsView;
    13.  
    14. const QMetaObject * dataObject = examples->metaObject();
    15.  
    16. //get the index of the "ViewportAnchor" enum in the dataObject
    17. int indexViewport = dataObject->indexOfEnumerator("ViewportAnchor");
    18. QMetaEnum enumData = dataObject->enumerator(indexViewport);
    19.  
    20. qDebug() << "Values of enum " << enumData.name();
    21. for (int x = 0; x < enumData.keyCount(); ++x)
    22. qDebug() << enumData.valueToKey(x);
    23.  
    24. return a.exec();
    25. }
    To copy to clipboard, switch view to plain text mode 

    for use this system in our class in necesary use the macro Q_ENUMS.

    Qt Code:
    1. class MyClass : public QObject
    2. {
    3. Q_OBJECT
    4. Q_ENUMS(Priority)
    5.  
    6. public:
    7. MyClass(QObject *parent = 0);
    8. ~MyClass();
    9.  
    10. enum Priority { High, Low, VeryHigh, VeryLow };
    11. void setPriority(Priority priority);
    12. Priority priority() const;
    13. };
    To copy to clipboard, switch view to plain text mode 


    the previus post resolved the question,

    i read wrong the question. but i hope someone find the post useful.

    sorry my poor english
    Last edited by ecanela; 26th January 2010 at 16:36. Reason: change the example for one more complete

Similar Threads

  1. converting of string to enum
    By jjbabu in forum Qt Programming
    Replies: 2
    Last Post: 24th October 2008, 16:31
  2. static enum in a class
    By mickey in forum General Programming
    Replies: 3
    Last Post: 19th August 2008, 19:02
  3. enum property
    By illuzioner in forum Qt Tools
    Replies: 10
    Last Post: 22nd August 2006, 21:47
  4. enum scope
    By illuzioner in forum General Programming
    Replies: 1
    Last Post: 15th February 2006, 05:39

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.