0

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’?
Raphael Sauer
  • 630
  • 1
  • 6
  • 27
  • *"This, however, results in an error."* -- What is the error? – JarMan Jul 14 '21 at 12:52
  • @JarMan sorry for my late reply. Several erros are accused on the IDE, the first of which is: SO_Example_autogen/EWIEGA46WW/../../../SO_Example/example.h:10:4: error: ‘friend’ used outside of class 10 | }; Q_ENUM(CarType) | ^~~~~~ I also edited the post. Thanks for your reply. – Raphael Sauer Jul 14 '21 at 15:19
  • You can't use Q_ENUM outside of a Q_OBJECT/Q_GADGET/Q_NAMESPACE. – JarMan Jul 14 '21 at 15:32
  • @JarMan thanks! Is there any way to access the enum's info from main.cpp using a 'Player' type meta-object? Or I would *have* to declare the enum inside of the struct? – Raphael Sauer Jul 14 '21 at 15:58

1 Answers1

1

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)
}
JarMan
  • 7,589
  • 1
  • 10
  • 25
  • That's a way to do it, but I can't access it in main through player. I've also posted this question on Qt's forum and the conclusion is that there's no way to do (use Q_ENUM) outside of a QObject or a Q_GADGET. Link here: https://forum.qt.io/topic/128519/proper-way-to-add-enum-class-to-meta-object-qt-5-15/4 – Raphael Sauer Jul 14 '21 at 20:05