Qt Forum

    • Login
    • Search
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search
    • Unsolved

    Update: Forum Guidelines & Code of Conduct


    Qt World Summit: Early-Bird Tickets

    [SOLVED] Model items in javascript.

    QML and Qt Quick
    4
    5
    2170
    Loading More Posts
    • 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.
    • D
      digitalsurgeon last edited by

      I have a custom model implemented in c++ like so:

      @class MyModel : public QAbstractListModel {
      // .... some code
      }@

      its exposed to QML like so:

      @engine()->rootContext()->setContextProperty("myModel", m_myModel);
      @
      what I want to do in QML is access items of this model. like so

      @var name = myModel[2].name; // second item and "role" name
      @
      right now all I get is undefined in name.

      any ideas ?

      [Edit: Please, use @-tags for code snippets /Vass]

      1 Reply Last reply Reply Quote 0
      • M
        MichK last edited by

        Hi,
        Have you read this: "QML Data Models":http://developer.qt.nokia.com/doc/qt-4.7/qdeclarativemodels.html#id-3ebf8f48-0c48-46b0-9937-38b514d927c1 ?
        This thread can be useful: "model in ListView: C++ to QML":http://developer.qt.nokia.com/forums/viewthread/8929/

        1 Reply Last reply Reply Quote 0
        • M
          mbrasser last edited by

          Hi,

          The most common approach I've seen to deal with this issue is to add a Q_INVOKABLE getter function to the model, similar to ListModel:

          @var name = myModel.get(2).name@

          This doesn't allow for the syntax you've shown, but I don't think that syntax is possible with QML as it currently stands.

          Regards,
          Michael

          1 Reply Last reply Reply Quote 0
          • S
            srikanth_trulyit last edited by

            You can say YOUR_ROLE directly in the delegate in qml. According to the role your model returns some meaningful data. QML automatically passes the correct index to the model, according to the delegate (Note that inside delegate you will get a variable names "index" which does the thing).

            1 Reply Last reply Reply Quote 0
            • D
              digitalsurgeon last edited by

              Ok, so I solved this by implementing a slot.

              One thing QtQuick should think about is to provide a consistent API set for all the models.

              1 Reply Last reply Reply Quote 0
              • First post
                Last post