summaryrefslogtreecommitdiffstats
path: root/doc/src/declarative
diff options
context:
space:
mode:
authorMartin Smith <martin.smith@nokia.com>2010-07-08 08:24:49 (GMT)
committerMartin Smith <martin.smith@nokia.com>2010-07-08 08:24:49 (GMT)
commit25e3227fc922f75cc772aadb9dd07cc8b965dcbd (patch)
treed9e96f5c1048849827a51c19ce2628d109baa78b /doc/src/declarative
parent4b48d16a6ef2f972e9cc2af1ea8f4f146be9849c (diff)
downloadQt-25e3227fc922f75cc772aadb9dd07cc8b965dcbd.zip
Qt-25e3227fc922f75cc772aadb9dd07cc8b965dcbd.tar.gz
Qt-25e3227fc922f75cc772aadb9dd07cc8b965dcbd.tar.bz2
doc: Added doc for accessing views and models from delegates.
Task-number: QTBUG-11648
Diffstat (limited to 'doc/src/declarative')
-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.
+
*/