Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. QML and Qt Quick
  4. Enums in QT
Forum Updated to NodeBB v4.3 + New Features

Enums in QT

Scheduled Pinned Locked Moved Unsolved QML and Qt Quick
4 Posts 2 Posters 1.1k Views 1 Watching
  • 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.
  • B Offline
    B Offline
    Bhushan_Sure
    wrote on 17 Sept 2019, 10:54 last edited by
    #1

    Hi, I have 4 enums in Code like and there messages.

    enum A {
    Message1=0;
    }
    enum B{
    Message2=1;
    }
    enum C {
    Message3=2;
    }
    enum D {
    Message4=3;
    }
    

    Now my Question How can i Defined Generic Method which can take Parameter/Argument as Any enum like A,B,C,D.

    Currently I am Doing this

    void CommonApi(Qvariant message) {
     qDebug()<<message;
    }
    

    But its Printing Enum Values like 0,1,2,etc.

    If i do seperately like

    void Api1(classname::A message) {
    qDebug()<<message;
    }
    void Api2(classname::B message) {
    qDebug()<<message;
    }
    void Api3(classname::C message) {
    qDebug()<<message;
    }
    void Api4(classname::D message) {
    qDebug()<<message;
    }
    

    Then all above is printing Respective Messages. But this is seprate method, i want generic one !

    1 Reply Last reply
    0
    • S Offline
      S Offline
      sierdzio
      Moderators
      wrote on 17 Sept 2019, 11:20 last edited by
      #2

      What happens when you change the enums into enum class?

      enum class A {
        Message1=0
      };
      // ... etc.
      

      The reason why it does not work for QVariant is that enums are really just integers. A generic method does not know which enum did the number come from. So I don't think this can be done with enums in a generic way. That is not what enums are for.

      (Z(:^

      B 1 Reply Last reply 18 Sept 2019, 05:51
      3
      • S sierdzio
        17 Sept 2019, 11:20

        What happens when you change the enums into enum class?

        enum class A {
          Message1=0
        };
        // ... etc.
        

        The reason why it does not work for QVariant is that enums are really just integers. A generic method does not know which enum did the number come from. So I don't think this can be done with enums in a generic way. That is not what enums are for.

        B Offline
        B Offline
        Bhushan_Sure
        wrote on 18 Sept 2019, 05:51 last edited by
        #3

        @sierdzio Okay, Can you suggest me some alternative for this ?

        1 Reply Last reply
        0
        • S Offline
          S Offline
          sierdzio
          Moderators
          wrote on 18 Sept 2019, 05:57 last edited by sierdzio
          #4

          What do you want to achieve? Have some predefined messages? Then you can use a global header where you define them, like:

          namespace Tags {
            const char *Name1 = "Some string";
            const char *Name2 = "Some other string";
          };
          

          If you need to stringify enum names, you can use QMetaEnum like this:

          class Enums {
            Q_GADGET
            enum A {
              Message1,
              Message2
            } Q_ENUM(A);
          
            QString aToString(const A value) {
              const QMetaEnum metaEnum(QMetaEnum::fromType<Enums::A>());
              const QString name(metaEnum.valueToKey(int(value)));
              return name;
            }
          }
          

          (Z(:^

          1 Reply Last reply
          3

          1/4

          17 Sept 2019, 10:54

          • Login

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