Q_ENUMS "undefined" in QML?

Enumerations do not work for me.

  • I registered them with Q_ENUMS()
  • I did not forget the macro Q_OBJECT
  • type is registered using qmlRegisterType()
  • module is imported into QML

In short, everything is “book-like,” but for some reason, I keep getting undefinedfor every listing in QML. Did I miss something?

class UI : public QQuickItem {
    Q_OBJECT
    Q_ENUMS(ObjectType)
public:
enum ObjectType {
        _Root = 0,
        _Block
    };
...
};

...

qmlRegisterType<UI>("Nodes", 1, 0, "UI");

...

import Nodes 1.0
...
console.log(UI._Root) // undefined

EDIT: Also note that registered entries are indeed available for use in the metasystem, for some reason they do not work in QML.

UPDATE: I just found this error: https://bugreports.qt.io/browse/QTBUG-33248 But unlike this error, my root component is a bare UInon-user element with UIas its root.

, QML console.log(), .

class A : public QObject {
    Q_OBJECT
    Q_ENUMS(EA)
public:
    enum EA {
        EA_NULL = 0,
        EA_ONE
    };
};

class B : public A {
    Q_OBJECT
    Q_ENUMS(EB)
public:
    enum EB {
        EA_TWO = 2,
        EA_THREE
    };
};

#include "main.moc"

int main(int argc, char *argv[])
{
    QGuiApplication app(argc, argv);
    qmlRegisterType<A>("test", 1, 0, "A");
    qmlRegisterType<B>("test", 1, 0, "B");

    QtQuick2ApplicationViewer viewer;
    viewer.setMainQmlFile(QStringLiteral("qml/enums/main.qml"));
    viewer.showExpanded();

    return app.exec();
}

...

Component.onCompleted: {
        console.log(A.EA_NULL)
        console.log(A.EA_ONE)

        console.log(B.EA_NULL)
        console.log(B.EA_ONE)
        console.log(B.EA_TWO)
        console.log(B.EA_THREE)
    }

:

0
1
0
1
2
3

, , , " "... , , , , UI, QML, UI . ++ QML, - , .

+4
2

, , . , .

. . , ( undefined, ).

, Qt, int var, , , , , , ​​.

. :

main.cpp

#include <QQuickView>
#include <QQuickItem>

#include <QGuiApplication>

#include <QUrl>

class UI : public QQuickItem {
    Q_OBJECT
    Q_ENUMS(ObjectType)
public:
enum ObjectType {
        Root = 0,
        _Block
    };
};

#include "main.moc"

int main(int argc, char **argv)
{
    QGuiApplication guiApplication(argc, argv);
    qmlRegisterType<UI>("Nodes", 1, 0, "UI");
    QQuickView *view = new QQuickView;
    view->setSource(QUrl::fromLocalFile("main.qml"));
    view->show();
    return guiApplication.exec();
}

main.qml

import Nodes 1.0
import QtQuick 2.0

Rectangle {
    id: button
    width: 500; height: 500

    MouseArea {
        anchors.fill: parent
        onClicked: console.log(UI.Root)
    }
}

main.pro

TEMPLATE = app
TARGET = main
QT += quick
SOURCES += main.cpp

qmake && make && ./main

0
+10

, . , - , .

++ QML, , - main.cpp

qmlRegisterType<EnumClass>("Enums", 1, 0, "EnumClass");

.qml

import Enums 1.0

EnumClass {
    id: ec
}

ec.SomeEnum, undefined, QtCreator ec.SomeEnum .

, ,

EnumClass.SomeEnum

( , ).

0

All Articles