c++qtenumsqmetaobject

Proper way to add enum class to metaObject 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.

#ifndef EXAMPLE_H
#define EXAMPLE_H
#include <QObject>

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)



#endif // EXAMPLE_H

I declare this struct as a Q_META_TYPE, in order to access it with the meta-object system. This allows me to access the struct's properties. 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();
}

I expect that enumeratorCount() would be 1, however I get 0. I've searched about this issue, and most examples I could find have the struct inside a class, and the Q_ENUM declaration right after it, like this:

enum class CarType {
    NO_CAR = 0,
    SLOW_CAR,
    FAST_CAR,
    HYPER_CAR
}; Q_ENUM(CarType)

This, however, results in an error. What's the proper way to register this enum in the meta-object system?

Thanks for your time.

EDIT The errors that I get that are relevant are:

/home/raphael/SO_Example/example.h:10: error: ‘friend’ used outside of class

/home/raphael/SO_Example/example.h:10: error: ‘staticMetaObject’ was not declared in this scope; did you mean ‘qt_getQtMetaObject’?

Solution

  • If you don't want to add your enum to a class with Q_OBJECT or Q_GADGET, then the other way to do it is to put your enum inside a Q_NAMESPACE. You can't actually use Q_ENUM with Q_NAMESPACE, but you can use Q_ENUM_NS instead. I haven't tested this myself, but it should work:

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