Q_ENUMS problem

  • I have problems with exposing c++ enum to QML

    C++ code:
    class MyClass : public QDeclarativeView


    enum Tasks { Task1, Task2, Task3 };

    I also send the object to the qml:
    this->rootContext()->setContextProperty("myclass", this);
    QML code:
    Rectangle {
    anchors.fill: parent

                    MouseArea {
                        anchors.fill: parent
                        onClicked:  {
                            console.log("enum:" + myclass.Task1);

    I should get "enum:0" in log, but it displays "enum: undefined".

    Any idea what am I doing wrong?

    [edit: code highlighted / Denis Kormalev]

  • You should make qml know about your class with qmlRegisterType or qmlRegisterUncreatableType.

  • tnx, like this?

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

  • i got it. thanks again

    qmlRegisterType<MyClass>(“MyImport”, 1, 0, “MyClass”);

    in QML:
    import MyImport 1.0

    and then use:
    var task = MyClass.Task1;

Log in to reply

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.