diff options
Diffstat (limited to 'doc/src/declarative/qdeclarativemodels.qdoc')
-rw-r--r-- | doc/src/declarative/qdeclarativemodels.qdoc | 33 |
1 files changed, 31 insertions, 2 deletions
diff --git a/doc/src/declarative/qdeclarativemodels.qdoc b/doc/src/declarative/qdeclarativemodels.qdoc index 91acb3c..788d417 100644 --- a/doc/src/declarative/qdeclarativemodels.qdoc +++ b/doc/src/declarative/qdeclarativemodels.qdoc @@ -143,6 +143,28 @@ ListView { } \endcode +It is also possible to manipulate the ListModel directly via JavaScript. +In this case, the first item inserted will determine the roles available +to any views using the model. For example, if an empty ListModel is +created and populated via JavaScript the roles provided by the first +insertion are the only roles that will be shown in the view: + +\code +Item { + ListModel { + id: fruitModel + } + MouseArea { + anchors.fill: parent + onClicked: fruitModel.append({"cost": 5.95, "name":"Pizza"}) + } +} +\endcode + +When the MouseArea is clicked fruitModel will have two roles, "cost" and "name". +Even if subsequent roles are added, only the first two will be handled by views +using the model. + \section2 XmlListModel @@ -283,7 +305,9 @@ QDeclarativeContext *ctxt = view.rootContext(); ctxt->setContextProperty("myModel", QVariant::fromValue(dataList)); \endcode -The properties of the object may then be accessed in the delegate: +The QObject* is available as the \c modelData property. As a convenience, +the properties of the object are also made available directly in the +delegate's context: \code ListView { @@ -295,13 +319,18 @@ ListView { Rectangle { height: 25 width: 100 - color: model.color + color: model.modelData.color Text { text: name } } } } \endcode +Note the use of the fully qualified access to the \c color property. +The properties of the object are not replicated in the \c model +object, since they are easily available via the modelData +object. + Note: There is no way for the view to know that the contents of a QList have changed. If the QList is changed, it will be necessary to reset the model by calling QDeclarativeContext::setContextProperty() again. |