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. [Solved]Iterate over enum, using QMetaEnum

[Solved]Iterate over enum, using QMetaEnum

Scheduled Pinned Locked Moved General and Desktop
5 Posts 2 Posters 3.4k 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.
  • ealioneE Offline
    ealioneE Offline
    ealione
    wrote on last edited by
    #1

    Lets say I have something like

    @enum Letters
    { A, B };@

    How can I iterate over them and print each one as a string for example?
    I do know about the existence of QMetaEnum, but I can not find a useful example of it.

    Thank you for your time.

    1 Reply Last reply
    0
    • SGaistS Offline
      SGaistS Offline
      SGaist
      Lifetime Qt Champion
      wrote on last edited by
      #2

      Hi,

      QMetaEnum is used in conjunction with Q_ENUMS. Is it the case of yours ?

      Interested in AI ? www.idiap.ch
      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

      1 Reply Last reply
      0
      • ealioneE Offline
        ealioneE Offline
        ealione
        wrote on last edited by
        #3

        Hi SGaist,
        No I was not using Q_ENUMS, yet I am open to any possible suggestions as I have never dealt with anything similar before.

        1 Reply Last reply
        0
        • ealioneE Offline
          ealioneE Offline
          ealione
          wrote on last edited by
          #4

          After SGaist's hint I ended up with a possible solution

          I first define on my class

          @class SomeClass : public QObject
          {
          Q_OBJECT
          Q_ENUMS(Letters)

          public:
          enum Letters
          { A, B };

          ...@

          then I can simply do

          @QMetaObject meta = SomeClass::staticMetaObject;
          QMetaEnum tmpObj = meta.enumerator(0);
          for (int i = 0; i < tmpObj.keyCount(); i++)
          {
          qDebug() << tmpObj.key(i);
          }@

          If there are any better ways i'd be glad to know.

          1 Reply Last reply
          0
          • SGaistS Offline
            SGaistS Offline
            SGaist
            Lifetime Qt Champion
            wrote on last edited by
            #5

            It depends on you use case. You can also have a simple function with a switch that returns a string

            Interested in AI ? www.idiap.ch
            Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

            1 Reply Last reply
            0

            • Login

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