Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

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



  • 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?


  • Lifetime Qt Champion


  • Moderators

    @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)
    }
    


  • @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!