Question

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.

Was it helpful?

Solution

You can't register enums as a type of qml. The qmlRegisterType<T>() function is only for classes that are derived from QObject. AFAIK registering enumus is not required. They are treated as normal ints.

To be able to pass all others types that do not derive from QObject within a QVariant to qml, use qRegisterMetaType<T>. You can't use them directly in qml, but you can pass to C++ slots, properties and invokable methods.

OTHER TIPS

You are probably not doing this correctly. You need to register the class contaning the enumeration, an then it will be available automatically. Here is an example of how it usually goes:

class MyClass : public QObject
{
    Q_OBJECT
    Q_ENUMS(MyEnum)
    Q_PROPERTY(MyEnum myEnum READ myEnum)

public:
    enum MyEnum {
        Value1 = 0,
        Value2
    };

    explicit MyObject(QObject *parent = 0);

    MyEnum myEnum() const;

    Q_INVOKABLE MyEnum getMyEnum() const;
};

qmlRegisterType<MyClass>("myclass", 1, 0, "MyClass");

To interpret this for your particular case, you would be writing something like this:

qmlRegisterType<Holiday>("holiday", 1, 0, "Holiday");

Then, you will access the enumeration values from QML like this:

holiday.Transportation
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top