Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. How to get no of items defined in enum ?
QtWS25 Last Chance

How to get no of items defined in enum ?

Scheduled Pinned Locked Moved Unsolved General and Desktop
enumcount
3 Posts 3 Posters 2.6k Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • A Offline
    A Offline
    ankit thakar
    wrote on 18 Aug 2016, 02:08 last edited by
    #1

    Hello All,

    I got one situation in which i want total no. of items defined in enum.
    for e.g.

    enum temp
    { A, B, C }

    Now I want it should return value 3.
    I googled it how to do it but everybody did like patch work either defining one extra last element in enum or by subtracting the value from LAST - FIRST + 1 .

    But above mentioned solution is not perfect.

    Does anybody have an idea how to do this ?

    Thanks in advance...

    D K 2 Replies Last reply 18 Aug 2016, 02:35
    0
    • A ankit thakar
      18 Aug 2016, 02:08

      Hello All,

      I got one situation in which i want total no. of items defined in enum.
      for e.g.

      enum temp
      { A, B, C }

      Now I want it should return value 3.
      I googled it how to do it but everybody did like patch work either defining one extra last element in enum or by subtracting the value from LAST - FIRST + 1 .

      But above mentioned solution is not perfect.

      Does anybody have an idea how to do this ?

      Thanks in advance...

      D Offline
      D Offline
      Devopia53
      wrote on 18 Aug 2016, 02:35 last edited by
      #2

      @ankit-thakar

      Hi.

      Yes. You can use the Qt's Meta-Object System(a called QMetaEnum). You can also use QMetaEnum::fromType() to get the QMetaEnum.

      Something like this:

        class MyClass : public QObject
        {
            Q_OBJECT
      
        public:
            MyClass(QObject *parent = 0);
            ~MyClass();
      
            enum Priority { High, Low, VeryHigh, VeryLow };
            Q_ENUM(Priority)
        };
      
      [...]
      #include <QMetaEnum>
      
      QMetaEnum   en = QMetaEnum::fromType<Priority>();
      qDebug() << en.name() << en.keyCount();
      [...]
      
      1 Reply Last reply
      2
      • A ankit thakar
        18 Aug 2016, 02:08

        Hello All,

        I got one situation in which i want total no. of items defined in enum.
        for e.g.

        enum temp
        { A, B, C }

        Now I want it should return value 3.
        I googled it how to do it but everybody did like patch work either defining one extra last element in enum or by subtracting the value from LAST - FIRST + 1 .

        But above mentioned solution is not perfect.

        Does anybody have an idea how to do this ?

        Thanks in advance...

        K Offline
        K Offline
        kshegunov
        Moderators
        wrote on 18 Aug 2016, 12:46 last edited by kshegunov
        #3

        @ankit-thakar

        I don't know why you don't like it, but the usual way is to add a dummy at the end.

        @Devopia53

        My issue with that solution is that you're getting the information at runtime, instead of at compile time. There is increased chance of errors and there isn't really a need to inquire the meta object system in this case.

        Read and abide by the Qt Code of Conduct

        1 Reply Last reply
        0

        1/3

        18 Aug 2016, 02:08

        • Login

        • Login or register to search.
        1 out of 3
        • First post
          1/3
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • Users
        • Groups
        • Search
        • Get Qt Extensions
        • Unsolved