I've been trying to export some QFlags for use in QML (mainly, to read them from properties of an object exposed in QML) but even following this SO question I keep on getting compiler errors:
#ifndef HOLIDAYACTIVITIES_H
#define HOLIDAYACTIVITIES_H
#include <QObject>
#include <QMetaType>
#include <QPair>
#include <QObject>
#include <QDateTime>
#include <QGeoLocation>
namespace HolidayPlanner
{
class Holiday: public QObject {
Q_OBJECT
Q_ENUMS(ActivityType)
Q_ENUMS(TransportType)
public:
explicit Holiday(QObject* parent = 0);
virtual ~Holiday();
enum ActivityType {
Transportation = 1,
Lodging = 2,
Sights = 4,
Note = 8,
Food = 16
};
enum TransportType {
Flight = 1,
Train = 2,
Subway = 4,
Bus = 8,
Foot = 16,
Other = 32,
None = 64
};
enum ItemRole {
TypeRole = Qt::UserRole + 1000,
StartDateRole = Qt::UserRole + 1001,
EndDateRole = Qt::UserRole + 1002,
StartPositionRole = Qt::UserRole + 1003,
EndPositionRole = Qt::UserRole + 1004,
TaskRole = Qt::UserRole + 1005,
};
Q_DECLARE_FLAGS(ActivityTypes, ActivityType)
Q_FLAGS(ActivityTypes)
Q_DECLARE_FLAGS(TransportTypes, TransportType)
Q_FLAGS(ActivityTypes)
};
Q_DECLARE_OPERATORS_FOR_FLAGS(Holiday::ActivityTypes)
Q_DECLARE_OPERATORS_FOR_FLAGS(Holiday::TransportTypes)
}; // namespace HolidayPlanner
Q_DECLARE_METATYPE(HolidayPlanner::Holiday::ActivityType)
Q_DECLARE_METATYPE(HolidayPlanner::Holiday::TransportType)
But when I issue
qmlRegisterType<HolidayPlanner::Holiday::ActivityType>();
anywhere in the code, I get
In file included from /usr/include/qt5/QtQml/QtQml:9:0,
from /home/lb/Coding/cpp/holiday-planner/src/holidayactivities.cpp:21:
/usr/include/qt5/QtQml/qqml.h: In instantiation of ‘int qmlRegisterType() [with T =
HolidayPlanner::Holiday::ActivityType]’:
/home/lb/Coding/cpp/holiday-planner/src/holidayactivities.cpp:28:60: required from here
/usr/include/qt5/QtQml/qqml.h:109:5: error: ‘staticMetaObject’ is not a member of
‘HolidayPlanner::Holiday::ActivityType’
QML_GETTYPENAMES
^
/usr/include/qt5/QtQml/qqml.h:119:21: error: ‘staticMetaObject’ is not a member of
‘HolidayPlanner::Holiday::ActivityType’
0, 0, 0, 0, &T::staticMetaObject,
^
What could be possibly wrong here?
For reference, other classes like QLocale can export their enums to QML (not done by default, but it works), but I couldn't find any clue to why it does not work in my case.