[SOLVED] Model items in javascript.
-
wrote on 24 Sept 2011, 14:25 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]
-
wrote on 25 Sept 2011, 07:17 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/ -
wrote on 26 Sept 2011, 02:24 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 -
wrote on 26 Sept 2011, 07:36 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).
-
wrote on 29 Sept 2011, 09:48 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.
5/5