Qt のメタオブジェクト システムを少しいじっていますがenum class
、メタオブジェクトへの追加に関する問題に遭遇しました。いくつかの変数を含む構造体があり、そのうちの 1 つは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
Q_META_TYPE
メタオブジェクト システムでアクセスするために、この構造体を として宣言します。これにより、構造体のプロパティにアクセスできます。これが私のもの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();
}
私はそれが 1 になることを期待していenumeratorCount()
ますが、私は 0 を取得していますQ_ENUM
。
enum class CarType {
NO_CAR = 0,
SLOW_CAR,
FAST_CAR,
HYPER_CAR
}; Q_ENUM(CarType)
ただし、これはエラーになります。この列挙型をメタオブジェクト システムに登録する適切な方法は何ですか?
御時間ありがとうございます。
編集 関連するエラーは次のとおりです。
/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’?