Using Qt Quick 2.0, QT 5, a (generated) class full of enums to be used in the GUI.
Class is properly registered. Can't use enum items directly inside QML code, e.g.
in console.log
or switch
...case
, they always evaluate to undefined
.
Assigning the enum to a property int
DOES work, and gives the expected value.
Why? How can I use the enums directly, without copying all of them (hundreds)
to properties?
guiids.h:
#include <QObject>
class GuiIDs : public QObject
{
Q_OBJECT
// ...
Q_ENUMS(Element_ALARMSTATUS_t)
// ...
public:
// ...
enum Element_ALARMSTATUS_t {
status_ALARMSTATUS_HIDDEN,
status_ALARMSTATUS_NOALARM,
status_ALARMSTATUS_INFO,
status_ALARMSTATUS_ALARM,
status_ALARMSTATUS_ACKNOWLEDGED,
status_ALARMSTATUS_PAUSED,
element_ALARMSTATUS_COUNT
};
// ...
};
test.qml:
import QtQuick 2.0
import bla.bla.bla.guiids 1.0
Rectangle {
// ...
Rectangle {
x: 0
y: 0
width: 100
height: 100
color: "red"
MouseArea {
anchors.fill: parent
property int foo: GuiIDs.element_ALARMSTATUS_COUNT
onClicked: {
// ...
console.log("foo=",foo);
console.log("GuiIDs.element_ALARMSTATUS_COUNT=",GuiIDs.element_ALARMSTATUS_COUNT)
}
}
}
}
Log output after clicking the red rectangle:
qml: foo= 6
qml: GuiIDs.element_ALARMSTATUS_COUNT= undefined