2

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
Alexander Foken
  • 219
  • 1
  • 2
  • 8
  • Possible duplicate of [Q\_ENUMS are "undefined" in QML?](http://stackoverflow.com/questions/21066822/q-enums-are-undefined-in-qml) – BaCaRoZzo Nov 24 '15 at 23:55
  • 1
    You should start the enums with an uppercase letter. That would suffice. Anyhow, look at the answer in the question listed above to check if you missed anything else. Also note that `Q_ENUMS` is somewhat deprecated now. Please consider the usage of [`Q_ENUM`](http://woboq.com/blog/q_enum.html). – BaCaRoZzo Nov 24 '15 at 23:58
  • Upper case letters did the trick. The remaining QML code always **assigned** enums with lower case first letter, so the basic problem was hidden. Can't use `Q_ENUM`, because my Qt5 is too old. – Alexander Foken Nov 25 '15 at 12:00
  • It's a pity you cannot use `Q_ENUM`, it's really awesome. Anyhow, it's good to know you've solved the issue. :) If you didn't do it, consider to upvote the answer above since it provides the information you were missing. – BaCaRoZzo Nov 25 '15 at 12:08

1 Answers1

-1

Encapsulate enum into QObject, just like I did - example from my code:

#ifndef UEMOBILEDATACONNECTION
#define UEMOBILEDATACONNECTION

#include <QObject>

class UeMobileDataConnection : public QObject
{
    Q_OBJECT

public:
    enum UeTypeMobileDataConnection
    {
        NOT_CONNECTED=false,
        CONNECTED=true
    };

    Q_ENUM(UeTypeMobileDataConnection)
};

#endif // UEMOBILEDATACONNECTION

then register type - example from my code:

qmlRegisterUncreatableType<UeMobileDataConnection>("si.testapp",
                                                   1,
                                                   0,
                                                   "UeTypeMobileDataConnection",
                                                   "Mobile Data Connection Status");

and use it on QML side.

KernelPanic
  • 2,328
  • 7
  • 47
  • 90
  • It's what he has done (at least by reading the question since he didn't show the code). it's good that you are using `Q_ENUM` by the way. :) – BaCaRoZzo Nov 24 '15 at 23:59