[SOLVED] Model items in javascript.
-
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]
-
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/ -
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 -
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).
-
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.