Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. QML and Qt Quick
  4. Q_ENUMS problem
Forum Updated to NodeBB v4.3 + New Features

Q_ENUMS problem

Scheduled Pinned Locked Moved QML and Qt Quick
4 Posts 2 Posters 7.3k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • B Offline
    B Offline
    borut123
    wrote on last edited by
    #1

    I have problems with exposing c++ enum to QML

    C++ code:
    @
    class MyClass : public QDeclarativeView
    {

    Q_OBJECT
    Q_ENUMS(Tasks)
    

    public:
    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]

    1 Reply Last reply
    0
    • D Offline
      D Offline
      DenisKormalev
      wrote on last edited by
      #2

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

      1 Reply Last reply
      0
      • B Offline
        B Offline
        borut123
        wrote on last edited by
        #3

        tnx, like this?

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

        1 Reply Last reply
        0
        • B Offline
          B Offline
          borut123
          wrote on last edited by
          #4

          i got it. thanks again

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

          in QML:
          import MyImport 1.0

          and then use:
          var task = MyClass.Task1;

          1 Reply Last reply
          0

          • Login

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • Users
          • Groups
          • Search
          • Get Qt Extensions
          • Unsolved