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. Proper way to add enum class to meta-object Qt 5.15

Proper way to add enum class to meta-object Qt 5.15

Scheduled Pinned Locked Moved Solved General and Desktop
meta-objectsenum classqobject
4 Posts 3 Posters 4.2k 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.
  • R Offline
    R Offline
    raphasauer
    wrote on 14 Jul 2021, 16:57 last edited by raphasauer
    #1

    I'm tinkering a bit with Qt's meta-object system, and I've come across an issue with adding enum class to a meta-object. I have a struct that contain some variables, one of which is an enum class. My goal is to access this enum through metaObject.enumerator(). Here is my code:

    #ifndef EXAMPLE_H
    #define EXAMPLE_H
    #include <QObject>
    
    //Make this enum class accessible through the struct Player
    enum class CarType {
        NO_CAR = 0,
        SLOW_CAR,
        FAST_CAR,
        HYPER_CAR
    };
    
    
    struct Player {
        Q_GADGET
    
        Q_PROPERTY(Player player READ getPlayer)
        Q_PROPERTY(float m_speed READ getSpeed)
        Q_PROPERTY(CarType m_carType READ getCarType)
        
        Player getPlayer() { return *this;}
        float getSpeed() {return m_speed;}
        CarType getCarType() { return m_carType;}
    
    
    public:
        CarType m_carType;
        float m_speed;
    
    }; Q_DECLARE_METATYPE(Player)
    

    Here is my main.cpp:

    #include <QCoreApplication>
    #include <iostream>
    #include <QMetaObject>
    #include "example.h"
    #include <QDebug>
    #include <QMetaProperty>
    
    int main(int argc, char *argv[])
    {
        QCoreApplication a(argc, argv);
        qRegisterMetaType<Player>();
        const QMetaObject* metaObject = &Player::staticMetaObject;
        qInfo()<< "Number of enums is: " << metaObject->enumeratorCount();
        return a.exec();
    }
    

    Currently, metaObject->enumeratorCount() returns 0. Is there a way to make that enum class accessible in main.cpp through Player's metaObject?

    R 1 Reply Last reply 14 Jul 2021, 17:47
    0
    • M Offline
      M Offline
      mrjj
      Lifetime Qt Champion
      wrote on 14 Jul 2021, 17:03 last edited by
      #2

      Hi
      It hink you might have to register the actual enum as they do here
      https://forum.qt.io/topic/41032/solved-enum-class-with-qvariant-how-to-register-strongly-typed-enum-with-qt-meta-system

      1 Reply Last reply
      0
      • R raphasauer
        14 Jul 2021, 16:57

        I'm tinkering a bit with Qt's meta-object system, and I've come across an issue with adding enum class to a meta-object. I have a struct that contain some variables, one of which is an enum class. My goal is to access this enum through metaObject.enumerator(). Here is my code:

        #ifndef EXAMPLE_H
        #define EXAMPLE_H
        #include <QObject>
        
        //Make this enum class accessible through the struct Player
        enum class CarType {
            NO_CAR = 0,
            SLOW_CAR,
            FAST_CAR,
            HYPER_CAR
        };
        
        
        struct Player {
            Q_GADGET
        
            Q_PROPERTY(Player player READ getPlayer)
            Q_PROPERTY(float m_speed READ getSpeed)
            Q_PROPERTY(CarType m_carType READ getCarType)
            
            Player getPlayer() { return *this;}
            float getSpeed() {return m_speed;}
            CarType getCarType() { return m_carType;}
        
        
        public:
            CarType m_carType;
            float m_speed;
        
        }; Q_DECLARE_METATYPE(Player)
        

        Here is my main.cpp:

        #include <QCoreApplication>
        #include <iostream>
        #include <QMetaObject>
        #include "example.h"
        #include <QDebug>
        #include <QMetaProperty>
        
        int main(int argc, char *argv[])
        {
            QCoreApplication a(argc, argv);
            qRegisterMetaType<Player>();
            const QMetaObject* metaObject = &Player::staticMetaObject;
            qInfo()<< "Number of enums is: " << metaObject->enumeratorCount();
            return a.exec();
        }
        

        Currently, metaObject->enumeratorCount() returns 0. Is there a way to make that enum class accessible in main.cpp through Player's metaObject?

        R Offline
        R Offline
        raven-worx
        Moderators
        wrote on 14 Jul 2021, 17:47 last edited by
        #3

        @raphasauer
        i dont think that you can register global enum through a Q_GADGET/Q_OBJECT.

        But you can declare a namespace in which the enum resides:

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

        --- 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
        1
        • R Offline
          R Offline
          raphasauer
          wrote on 14 Jul 2021, 19:03 last edited by
          #4

          @mrjj and @raven-worx , thanks for the replies! What I've gathered from this (and other posts) is: you can't use a Q_ENUM outside of QObject or a Q_GADGET macro. So in order to do what I wanted I need to change the code like this:

          #ifndef EXAMPLE_H
          #define EXAMPLE_H
          #include <QObject>
          
          struct Player {
              Q_GADGET
          
          public:
              enum class CarType {
                  NO_CAR = 0,
                  SLOW_CAR,
                  FAST_CAR,
                  HYPER_CAR
              }; Q_ENUM(CarType)
          
              Q_PROPERTY(Player player READ getPlayer)
              Q_PROPERTY(float m_speed READ getSpeed)
              Q_PROPERTY(CarType m_carType READ getCarType)
          
              Player getPlayer() { return *this;}
              float getSpeed() {return m_speed;}
              CarType getCarType() { return m_carType;}
          
          
          public:
              CarType m_carType;
              float m_speed;
          
          }; Q_DECLARE_METATYPE(Player)
          
          
          #endif
          
          #include <QCoreApplication>
          #include <iostream>
          #include <QMetaObject>
          #include "example.h"
          #include <QDebug>
          #include <QMetaProperty>
          
          int main(int argc, char *argv[])
          {
              QCoreApplication a(argc, argv);
              qRegisterMetaType<Player>();
              const QMetaObject* metaObject = &Player::staticMetaObject;
              qInfo() << "Number of enums is: " << metaObject->enumeratorCount();
              const QMetaEnum metaEnum = metaObject->enumerator(0);
              qInfo() << "Enum name: " << metaEnum.enumName();
              int i = 0;
              for(i = 0; i < metaEnum.keyCount(); i++)
                  qInfo() << "Enum key: " << metaEnum.key(i) << "; Value: " << metaEnum.value(i);
              return a.exec();
          }
          

          Thanks for your time!

          1 Reply Last reply
          1

          3/4

          14 Jul 2021, 17:47

          • Login

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