summaryrefslogtreecommitdiffstats
path: root/doc/src/declarative
diff options
context:
space:
mode:
authorMartin Smith <martin.smith@nokia.com>2010-07-08 08:24:49 (GMT)
committerJason McDonald <jason.mcdonald@nokia.com>2010-07-16 07:20:42 (GMT)
commitb2ab0a5912f68730ed6b23f31351d747ca8815cf (patch)
treec35486f3bbcdb405acebfa7658df76dc04d89d77 /doc/src/declarative
parentec1ef2cdd7fc7aad08db6014056f1a59f9dc0520 (diff)
downloadQt-b2ab0a5912f68730ed6b23f31351d747ca8815cf.zip
Qt-b2ab0a5912f68730ed6b23f31351d747ca8815cf.tar.gz
Qt-b2ab0a5912f68730ed6b23f31351d747ca8815cf.tar.bz2
doc: Added doc for accessing views and models from delegates.
Task-number: QTBUG-11648 (cherry picked from commit 25e3227fc922f75cc772aadb9dd07cc8b965dcbd)
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.
+
*/