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. Q_ENUM(enum_name) works with only enums declared inside class. srsly?
Forum Updated to NodeBB v4.3 + New Features

Q_ENUM(enum_name) works with only enums declared inside class. srsly?

Scheduled Pinned Locked Moved QML and Qt Quick
7 Posts 7 Posters 23.4k Views 3 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.
  • K Offline
    K Offline
    Kofr
    wrote on last edited by
    #1

    Let's look at example from documentation which is copied next.

    class Message : public QObject
     {
         Q_OBJECT
         Q_ENUMS(Status)
         Q_PROPERTY(Status status READ status NOTIFY statusChanged)
     public:
         enum Status {
             Ready,
             Loading,
             Error
         };
         Status status() const;
     signals:
         void statusChanged();
     };
    

    In this example we declare our enumeration declared inside class with Q_ENUM(). It is ok. But what if we want to use Globally declared enumeration. Qt gives errors. How can we achieve operating with enumerations declared outside the class?

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

      Hi,

      You can't, Q_ENUMS need a meta object provided either by the QObject class or the Q_GADGET macro

      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
      3
      • D Offline
        D Offline
        dethtoll
        wrote on last edited by
        #3

        This is an old topic, but still comes up as one of the first Google results. I just wanted to add:

        If the main goal is to use the enum as a property, it is sufficient to just use Q_DECLARE_METATYPE after the globally-declared enumeration.

        If you need the other conveniences of Q_ENUM, then SGaist's reply applies.

        1 Reply Last reply
        0
        • K Kofr

          Let's look at example from documentation which is copied next.

          class Message : public QObject
           {
               Q_OBJECT
               Q_ENUMS(Status)
               Q_PROPERTY(Status status READ status NOTIFY statusChanged)
           public:
               enum Status {
                   Ready,
                   Loading,
                   Error
               };
               Status status() const;
           signals:
               void statusChanged();
           };
          

          In this example we declare our enumeration declared inside class with Q_ENUM(). It is ok. But what if we want to use Globally declared enumeration. Qt gives errors. How can we achieve operating with enumerations declared outside the class?

          raven-worxR Offline
          raven-worxR Offline
          raven-worx
          Moderators
          wrote on last edited by raven-worx
          #4

          @Kofr said in Q_ENUM(enum_name) works with only enums declared inside class. srsly?:

          How can we achieve operating with enumerations declared outside the class?

          This is possible since Qt 5.8 where the Q_NAMESPACE macro was introduced. This enables the generation of a MOC object also for namespaces.

          namespace MyNamespace
          {
              Q_NAMESPACE
           
              enum MyEnum {
                  Foo,
                  Bar
              };
              Q_ENUM_NS(MyEnum)
          }
          

          Note the Q_ENUM_NS macro here. There is also a Q_FLAG_NS macro available.

          --- SUPPORT REQUESTS VIA CHAT WILL BE IGNORED ---
          If you have a question please use the forum so others can benefit from the solution in the future

          1 Reply Last reply
          8
          • D Offline
            D Offline
            Drake Campbell
            wrote on last edited by
            #5

            Let me know if this question does not belong on this post. I am on Windows 10 and my project configuration is set to use Qt 5.11.0 MSVC2015 64 bit. I created a new Qt Quick application and copy pasted the namespace deceleration as posted by raven-worx and compiled. I get the following errors which disappear if I remove the Q_NAMESPACE and Q_ENUM_NS statements.

            qtprojects\QEnum\myenum.h:7: error: C2143: syntax error: missing ';' before 'enum [tag]'
            qtprojects\QEnum\myenum.h:8: error: C4430: missing type specifier - int assumed. Note: C++ does not support default-int
            qtprojects\QEnum\myenum.h:13: error: C2143: syntax error: missing ';' before '}'
            qtprojects\QEnum\myenum.h:13: error: C4430: missing type specifier - int assumed. Note: C++ does not support default-int
            QTProjects\build-QEnum-Desktop_Qt_5_11_0_MSVC2015_64bit-Debug\debug\moc_myenum.cpp:65: error: C2039: 'staticMetaObject': is not a member of 'Enums'
            qtprojects\QEnum\myenum.h:4: see declaration of 'Enums'

            I am also new so I feel like I may be missing something simple. The only things that exist are the default main.cpp and main.qml and a header file with the above pasted code. Thanks in advance.

            1 Reply Last reply
            0
            • J Offline
              J Offline
              Judit
              wrote on last edited by
              #6

              You need to include QObject to make it work

              1 Reply Last reply
              0
              • P Offline
                P Offline
                Pixelgrease
                wrote on last edited by Pixelgrease
                #7

                To add to SGaist' response, below is an example of Q_ENUM in a struct with Q_GADGET:

                struct Message 
                {
                     Q_GADGET
                public:
                     enum Status {
                         Ready,
                         Loading,
                         Error
                     };
                     Q_ENUM(Status)
                };
                

                This precludes using signals and slots within the struct, is a lightweight solution for properties or unit test arguments. This works as far back as Qt 5.5.

                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