Hi, I use a number of enums in my project, which I keep in a single "Enums" class, each of them defined by the c+11 way of "enum class" and the new Q_ENUM macro (instead of Q_ENUMS, as proposed here).
I don't get them to work in QML, though - and all I can find about this topic (there are actually plenty of solutions!) is using the older concepts.
Below you find a minimized example project. On click, qml shall send Enums.DayOfWeek.MONDAY to a slot.
The result is:
qrc:/main.qml:17: TypeError: Cannot read property 'MONDAY' of undefined
where I'd expext qDebug output from the slot (environment: Qt 5.5, QtCreator 3.6, Win7x64).
Any ideas about how I could do it better / make it work?
Several Enums in enums.h
#ifndef ENUMS_H
#define ENUMS_H
#include <QObject>
class Enums
{
Q_GADGET
public:
enum class DayOfWeek {
SUNDAY=0,
MONDAY=1,
TUESDAY=2,
WEDNESDAY=3,
THURSDAY=4,
FRIDAY=5,
SATURDAY=6,
UNDEFINED=-1
};
Q_ENUM(DayOfWeek)
enum class NameOrder {
FIRST_FAMILY=0,
FAMILY_FIRST=1
};
Q_ENUM(NameOrder)
};
#endif // ENUMS_H
#ifndef ENUMS_H
#define ENUMS_H
#include <QObject>
class Enums
{
Q_GADGET
public:
enum class DayOfWeek {
SUNDAY=0,
MONDAY=1,
TUESDAY=2,
WEDNESDAY=3,
THURSDAY=4,
FRIDAY=5,
SATURDAY=6,
UNDEFINED=-1
};
Q_ENUM(DayOfWeek)
enum class NameOrder {
FIRST_FAMILY=0,
FAMILY_FIRST=1
};
Q_ENUM(NameOrder)
};
#endif // ENUMS_H
To copy to clipboard, switch view to plain text mode
An object with a slot that takes such an enum: someobject.h
#ifndef SOMEOBJECT_H
#define SOMEOBJECT_H
#include <QObject>
#include <QDebug>
#include "enums.h"
{
Q_OBJECT
public:
public slots:
void outputDay(Enums::DayOfWeek day) { qDebug()<<day; }
};
#endif // SOMEOBJECT_H
#ifndef SOMEOBJECT_H
#define SOMEOBJECT_H
#include <QObject>
#include <QDebug>
#include "enums.h"
class SomeObject : public QObject
{
Q_OBJECT
public:
explicit SomeObject(QObject *parent = 0): QObject(parent) {}
public slots:
void outputDay(Enums::DayOfWeek day) { qDebug()<<day; }
};
#endif // SOMEOBJECT_H
To copy to clipboard, switch view to plain text mode
This is being registered in main.cpp
#include <QGuiApplication>
#include <QQmlApplicationEngine>
#include <QQmlEngine>
#include <QtQml>
#include "enums.h"
#include "someobject.h"
int main(int argc, char *argv[])
{
QGuiApplication app(argc, argv);
QQmlApplicationEngine engine;
qmlRegisterUncreatableType<Enums>("url.Enums", 1, 0, "Enums", "You cannot create an instance of the Enums.");
qmlRegisterType<SomeObject>("url.SomeObject",1,0,"SomeObject");
engine.
load(QUrl(QStringLiteral
("qrc:/main.qml")));
return app.exec();
}
#include <QGuiApplication>
#include <QQmlApplicationEngine>
#include <QQmlEngine>
#include <QtQml>
#include "enums.h"
#include "someobject.h"
int main(int argc, char *argv[])
{
QGuiApplication app(argc, argv);
QQmlApplicationEngine engine;
qmlRegisterUncreatableType<Enums>("url.Enums", 1, 0, "Enums", "You cannot create an instance of the Enums.");
qmlRegisterType<SomeObject>("url.SomeObject",1,0,"SomeObject");
engine.load(QUrl(QStringLiteral("qrc:/main.qml")));
return app.exec();
}
To copy to clipboard, switch view to plain text mode
And I try to use it in main.qml
import QtQuick 2.5
import QtQuick.Window 2.2
import url.SomeObject 1.0
import url.Enums 1.0
Window {
visible: true
SomeObject {
id: myObject
}
MouseArea {
anchors.fill: parent
onClicked: {
myObject.outputDay(Enums.DayOfWeek.MONDAY)
}
}
}
import QtQuick 2.5
import QtQuick.Window 2.2
import url.SomeObject 1.0
import url.Enums 1.0
Window {
visible: true
SomeObject {
id: myObject
}
MouseArea {
anchors.fill: parent
onClicked: {
myObject.outputDay(Enums.DayOfWeek.MONDAY)
}
}
}
To copy to clipboard, switch view to plain text mode
Bookmarks