summaryrefslogtreecommitdiffstats
path: root/doc/src/declarative/qdeclarativemodels.qdoc
diff options
context:
space:
mode:
Diffstat (limited to 'doc/src/declarative/qdeclarativemodels.qdoc')
-rw-r--r--doc/src/declarative/qdeclarativemodels.qdoc73
1 files changed, 73 insertions, 0 deletions
diff --git a/doc/src/declarative/qdeclarativemodels.qdoc b/doc/src/declarative/qdeclarativemodels.qdoc
index b44e6f2..a2f4d3a 100644
--- a/doc/src/declarative/qdeclarativemodels.qdoc
+++ b/doc/src/declarative/qdeclarativemodels.qdoc
@@ -429,4 +429,77 @@ Rectangle {
}
\endcode
+\section1 Accessing Views and Models from Delegates
+
+You can access the view for which a delegate is used, and its
+properties, by using ListView.view in a delegate on a ListView, or
+GridView.view in a delegate on a GridView, etc. In particular, you can
+access the model and its properties by using ListView.view.model.
+
+This is useful when you want to use the same delegate for a number of
+views, for example, but you want decorations or other features to be
+different for each view, and you would like these different settings to
+be properties of each of the views. Similarly, it might be of interest
+to access or show some properties of the model.
+
+In the following example, the delegate shows the property \e{language}
+of the model, and the color of one of the fields depends on the
+property \e{fruit_color} of the view.
+
+\code
+Rectangle {
+ width: 200; height: 200
+
+ ListModel {
+ id: fruitModel
+ property string language: "en"
+ ListElement {
+ name: "Apple"
+ cost: 2.45
+ }
+ ListElement {
+ name: "Orange"
+ cost: 3.25
+ }
+ ListElement {
+ name: "Banana"
+ cost: 1.95
+ }
+ }
+
+ Component {
+ id: fruitDelegate
+ Row {
+ Text { text: " Fruit: " + name; color: ListView.view.fruit_color }
+ Text { text: " Cost: $" + cost }
+ Text { text: " Language: " + ListView.view.model.language }
+ }
+ }
+
+ ListView {
+ property color fruit_color: "green"
+ model: fruitModel
+ delegate: fruitDelegate
+ anchors.fill: parent
+ }
+}
+\endcode
+
+Another important case is when some action (e.g. mouse click) in the
+delegate should update data in the model. In this case you can define
+a function in the model, e.g.:
+
+\code
+ setData(int row, const QString & field_name, QVariant new_value),
+\endcode
+
+...and call it from the delegate using:
+
+\code
+ ListView.view.model.setData(index, field, value)
+\endcode
+
+...assuming that \e{field} holds the name of the field which should be
+updated, and that \e{value} holds the new value.
+
*/