Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Special Interest Groups
  3. C++ Gurus
  4. Q_ENUM and typedef

Q_ENUM and typedef

Scheduled Pinned Locked Moved Unsolved C++ Gurus
4 Posts 2 Posters 4.3k 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.
  • jsulmJ Offline
    jsulmJ Offline
    jsulm
    Lifetime Qt Champion
    wrote on last edited by
    #1

    Hi!
    I would like to use Q_ENUM with a typedef like bellow:

    class TestEnum
    {
        Q_GADGET
    public:
        TestEnum();
    
        typedef enum SomeEnum_
        {
            value1,
            value2,
            value3
        } SomeEnum;
        Q_ENUM(SomeEnum)
    
        void printEnum(SomeEnum value)
        {
            qDebug() << Q_FUNC_INFO;
    
            QMetaEnum metaEnum = QMetaEnum::fromType<SomeEnum>();
            qDebug() << metaEnum.isValid();
            qDebug() << "Key" << metaEnum.key(value);
        }
    };
    

    Without typedef it works as expected (the key name is printed). But with typedef it does not work and metaEnum.isValid() returns false. Is there a way to use Q_ENUM with enums declared using typedef?
    The reason why I would like to use typedef is that we give our own names to enums provided by the supplier using typedef.

    https://forum.qt.io/topic/113070/qt-code-of-conduct

    kshegunovK 1 Reply Last reply
    0
    • jsulmJ jsulm

      Hi!
      I would like to use Q_ENUM with a typedef like bellow:

      class TestEnum
      {
          Q_GADGET
      public:
          TestEnum();
      
          typedef enum SomeEnum_
          {
              value1,
              value2,
              value3
          } SomeEnum;
          Q_ENUM(SomeEnum)
      
          void printEnum(SomeEnum value)
          {
              qDebug() << Q_FUNC_INFO;
      
              QMetaEnum metaEnum = QMetaEnum::fromType<SomeEnum>();
              qDebug() << metaEnum.isValid();
              qDebug() << "Key" << metaEnum.key(value);
          }
      };
      

      Without typedef it works as expected (the key name is printed). But with typedef it does not work and metaEnum.isValid() returns false. Is there a way to use Q_ENUM with enums declared using typedef?
      The reason why I would like to use typedef is that we give our own names to enums provided by the supplier using typedef.

      kshegunovK Offline
      kshegunovK Offline
      kshegunov
      Moderators
      wrote on last edited by
      #2

      @jsulm
      Hi,
      If you're willing to live with the Q_ENUM macro referencing the non-aliased type, the QMetaEnum::fromType function will work as expected. I.e.:

      class TestEnum
      {
          typedef enum SomeEnum_
          {
          } SomeEnum;
          Q_ENUM(SomeEnum_) //< No alias for the macro
      
          void printEnum(SomeEnum value)
          {
              QMetaEnum metaEnum = QMetaEnum::fromType<SomeEnum>(); //< Works okay
          }
      };
      

      But I think you can't use a typedef-ed enum with the macro. If I'm not mistaken there's no way to deduce the corresponding meta-object this way, and consequently it doesn't work.

      Read and abide by the Qt Code of Conduct

      1 Reply Last reply
      0
      • jsulmJ Offline
        jsulmJ Offline
        jsulm
        Lifetime Qt Champion
        wrote on last edited by
        #3

        The Q_ENUMS macro works with typedefs.

        https://forum.qt.io/topic/113070/qt-code-of-conduct

        kshegunovK 1 Reply Last reply
        0
        • jsulmJ jsulm

          The Q_ENUMS macro works with typedefs.

          kshegunovK Offline
          kshegunovK Offline
          kshegunov
          Moderators
          wrote on last edited by kshegunov
          #4

          @jsulm
          Yes, but the new macro is "smart" ... Here's a good article dealing with it, if you haven't stumbled on it, I suggest taking a quick look. If I had to guess what the problem is, I'd say it's the overloading rules that are confused, which is always a danger when working with templates. Most probably the trick with SFINAE they're using doesn't go well with aliased enums.
          I'm not that well versed in the internals, though, so I might simply be wrong.

          PS.
          You might get a more coherent answer on the mailing list.

          Kind regards.

          Read and abide by the 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