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. [SOLVED] Model items in javascript.
QtWS25 Last Chance

[SOLVED] Model items in javascript.

Scheduled Pinned Locked Moved QML and Qt Quick
5 Posts 4 Posters 2.4k Views
  • 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 Offline
    D Offline
    digitalsurgeon
    wrote on last edited by
    #1

    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
    0
    • M Offline
      M Offline
      MichK
      wrote on last edited by
      #2

      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
      0
      • M Offline
        M Offline
        mbrasser
        wrote on last edited by
        #3

        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
        0
        • S Offline
          S Offline
          srikanth_trulyit
          wrote on last edited by
          #4

          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
          0
          • D Offline
            D Offline
            digitalsurgeon
            wrote on last edited by
            #5

            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
            0

            • Login

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