Results 1 to 4 of 4

Thread: Using QMetaEnum

  1. #1
    Join Date
    Jan 2011
    Posts
    32
    Thanks
    6
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Using QMetaEnum

    Right now I have a header file called enums.h as such:

    enums.h
    Qt Code:
    1. #ifndef ENUMS_H
    2. #define ENUMS_H
    3.  
    4. #include <Qt>
    5.  
    6. enum paper_orient{portrait, landscape};
    7. enum ruler_orient{top_ruler, bottom_ruler, left_ruler, right_ruler};
    8. enum relationships_type{line_item, other};
    9. enum settings_type{line_object, plot, text};
    10.  
    11. #endif // ENUMS_H
    To copy to clipboard, switch view to plain text mode 

    With this setup, wherever I want to use these enums, I just #include "enums.h" and they are available.

    In learning to do things the Qt way, it appears that QMetaEnum would be a more streamlined approach.

    In the docs, I find this example:
    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 

    If I use this approach, does this mean that the Priority enum is only available within MyClass? What if I wish to use this enum in other classes? Ultimately that is what I would like to establish, is a series of global enums that are available anywhere in my project.

  2. #2
    Join Date
    Sep 2009
    Location
    UK
    Posts
    2,447
    Thanks
    6
    Thanked 348 Times in 333 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Using QMetaEnum

    Quote Originally Posted by lxman View Post
    If I use this approach, does this mean that the Priority enum is only available within MyClass? What if I wish to use this enum in other classes? Ultimately that is what I would like to establish, is a series of global enums that are available anywhere in my project.
    Yes, and you simply qualify it by class name, eg: MyClass::Priority priority

  3. #3
    Join Date
    Jan 2011
    Posts
    32
    Thanks
    6
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Using QMetaEnum

    Okay, experimenting a bit with this concept, here is what I see:

    With my current system, when I want to use my enums, I do #include "enums.h" and then when I want to, for instance, declare a variable of type settings_type, I do the following:
    Qt Code:
    1. #include "enums.h"
    2. .
    3. .
    4. .
    5. settings_type vara;
    6. vara = plot;
    To copy to clipboard, switch view to plain text mode 
    Now if I declare a class, MyEnums, in order to use the enums, I now do this:
    Qt Code:
    1. #include "myenums.h"
    2. .
    3. .
    4. .
    5. MyEnums::settings_type vara;
    6. vara = MyEnums::plot;
    To copy to clipboard, switch view to plain text mode 
    This has just increased the amount of my code.

    So, could somebody explain to me what other advantages there might be to using QMetaEnum that make up for the increased complexity?

  4. #4
    Join Date
    Sep 2009
    Location
    UK
    Posts
    2,447
    Thanks
    6
    Thanked 348 Times in 333 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Using QMetaEnum

    Firstly I wouldn't create a class called MyEnums, instead each enum would be relevent to the class they belong to; meaning I can use the same enum value in numerous classes witout worrying about ambiguation between them.

    Secondly, it allows you to easily serialize and deserialize enumerations. At Qt Developer Days 2007, this was described in some detail. If you did not attend, you can download the slides from here: ftp://ftp.qt.nokia.com/videos/DevDay...rets_of_Qt.pdf

Similar Threads

  1. error: invalid use of incomplete type 'struct QMetaEnum'
    By dyngoman in forum Qt Programming
    Replies: 3
    Last Post: 12th March 2010, 14:38
  2. QMetaEnum
    By No-Nonsense in forum Qt Programming
    Replies: 6
    Last Post: 7th February 2007, 16:15

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.