Remove "All rights reserved" line from license headers.
[profile/ivi/qtdeclarative.git] / doc / src / qtquick1 / qdeclarativemodels.qdoc
1 /****************************************************************************
2 **
3 ** Copyright (C) 2012 Nokia Corporation and/or its subsidiary(-ies).
4 ** Contact: http://www.qt-project.org/
5 **
6 ** This file is part of the documentation of the Qt Toolkit.
7 **
8 ** $QT_BEGIN_LICENSE:FDL$
9 ** GNU Free Documentation License
10 ** Alternatively, this file may be used under the terms of the GNU Free
11 ** Documentation License version 1.3 as published by the Free Software
12 ** Foundation and appearing in the file included in the packaging of
13 ** this file.
14 **
15 ** Other Usage
16 ** Alternatively, this file may be used in accordance with the terms
17 ** and conditions contained in a signed written agreement between you
18 ** and Nokia.
19 **
20 **
21 **
22 **
23 **
24 ** $QT_END_LICENSE$
25 **
26 ****************************************************************************/
27
28 /*!
29 \page qdeclarativemodels.html
30 \inqmlmodule QtQuick 1
31 \ingroup qml-features
32 \contentspage QML Features
33 \previouspage {QML Animation and Transitions}{Animation and Transitions}
34 \nextpage {Presenting Data with Views}
35 \target qmlmodels
36 \title QML Data Models
37
38 QML items such as ListView, GridView and \l Repeater require Data Models
39 that provide the data to be displayed.
40 These items typically require a \e delegate component that
41 creates an instance for each item in the model.  Models may be static, or
42 have items modified, inserted, removed or moved dynamically.
43
44 Data is provided to the delegate via named data roles which the
45 delegate may bind to. Here is a ListModel with two roles, \e type and \e age,
46 and a ListView with a delegate that binds to these roles to display their
47 values:
48
49 \snippet doc/src/snippets/declarative/qml-data-models/listmodel-listview.qml document
50
51 If there is a naming clash between the model's properties and the delegate's
52 properties, the roles can be accessed with the qualified \e model name instead.
53 For example, if a \l Text element had \e type or \e age properties, the text in the
54 above example would display those property values instead of the \e type and \e age values
55 from the model item. In this case, the properties could have been referenced as
56 \c model.type and \c model.age instead to ensure the delegate displays the
57 property values from the model item.
58
59 A special \e index role containing the index of the item in the model
60 is also available to the delegate.  Note this index is set to -1 if the item is removed from
61 the model.  If you bind to the index role, be sure that the logic
62 accounts for the possibility of index being -1, i.e. that the item
63 is no longer valid. (Usually the item will shortly be destroyed, but
64 it is possible to delay delegate destruction in some views via a \c delayRemove
65 attached property.)
66
67 Models that do not have named roles (such as the QStringList model shown below)
68 will have the data provided via the \e modelData role.  The \e modelData role is also provided for
69 models that have only one role.  In this case the \e modelData role
70 contains the same data as the named role.
71
72 QML provides several types of data models among the built-in set of
73 QML elements. In addition, models can be created with C++ and then
74 made available to QML components.
75
76 The views used to access data models are described in the
77 \l{Presenting Data with Views} overview.
78 The use of positioner items to arrange items from a model is covered in
79 \l{Using QML Positioner and Repeater Items}.
80
81
82 \keyword qml-data-models
83 \section1 QML Data Models
84
85 \section2 ListModel
86
87 ListModel is a simple hierarchy of elements specified in QML.  The
88 available roles are specified by the \l ListElement properties.
89
90 \snippet doc/src/snippets/declarative/qml-data-models/listelements.qml model
91
92 The above model has two roles, \e name and \e cost.  These can be bound
93 to by a ListView delegate, for example:
94
95 \snippet doc/src/snippets/declarative/qml-data-models/listelements.qml view
96
97 ListModel provides methods to manipulate the ListModel directly via JavaScript.
98 In this case, the first item inserted determines the roles available
99 to any views that are using the model. For example, if an empty ListModel is
100 created and populated via JavaScript, the roles provided by the first
101 insertion are the only roles that will be shown in the view:
102
103 \snippet doc/src/snippets/declarative/qml-data-models/dynamic-listmodel.qml model
104 \dots
105 \snippet doc/src/snippets/declarative/qml-data-models/dynamic-listmodel.qml mouse area
106
107 When the MouseArea is clicked, \c fruitModel will have two roles, \e cost and \e name.
108 Even if subsequent roles are added, only the first two will be handled by views
109 using the model. To reset the roles available in the model, call ListModel::clear().
110
111
112 \section2 XmlListModel
113
114 XmlListModel allows construction of a model from an XML data source. The roles
115 are specified via the \l XmlRole element.
116
117 The following model has three roles, \e title, \e link and \e description:
118 \qml
119 XmlListModel {
120      id: feedModel
121      source: "http://rss.news.yahoo.com/rss/oceania"
122      query: "/rss/channel/item"
123      XmlRole { name: "title"; query: "title/string()" }
124      XmlRole { name: "link"; query: "link/string()" }
125      XmlRole { name: "description"; query: "description/string()" }
126 }
127 \endqml
128
129 The \l{declarative/rssnews}{RSS News demo} shows how XmlListModel can
130 be used to display an RSS feed.
131
132
133 \section2 VisualItemModel
134
135 VisualItemModel allows QML items to be provided as a model.
136
137 This model contains both the data and delegate; the child items of a
138 VisualItemModel provide the contents of the delegate. The model
139 does not provide any roles.
140
141 \snippet doc/src/snippets/declarative/models/visual-model-and-view.qml visual model and view
142
143 Note that in the above example there is no delegate required.
144 The items of the model itself provide the visual elements that
145 will be positioned by the view.
146
147 \keyword qml-c++-models
148 \section1 C++ Data Models
149
150 Models can be defined in C++ and then made available to QML. This is useful
151 for exposing existing C++ data models or otherwise complex datasets to QML.
152
153 A C++ model class can be defined as a QStringList, a QList<QObject*> or a
154 QAbstractItemModel. The first two are useful for exposing simpler datasets,
155 while QAbstractItemModel provides a more flexible solution for more complex
156 models.
157
158
159 \section2 QStringList-based model
160
161 A model may be a simple QStringList, which provides the contents of the list via the \e modelData role.
162
163 Here is a ListView with a delegate that references its model item's
164 value using the \c modelData role:
165
166 \snippet examples/declarative/modelviews/stringlistmodel/view.qml 0
167
168 A Qt application can load this QML document and set the value of \c myModel
169 to a QStringList:
170
171 \snippet examples/declarative/modelviews/stringlistmodel/main.cpp 0
172
173 The complete example is available in Qt's \l {declarative/modelviews/stringlistmodel}{examples/declarative/modelviews/stringlistmodel} directory.
174
175 \note There is no way for the view to know that the contents of a QStringList
176 have changed.  If the QStringList changes, it will be necessary to reset
177 the model by calling QDeclarativeContext::setContextProperty() again.
178
179
180 \section2 QObjectList-based model
181
182 A list of QObject* values can also be used as a model. A QList<QObject*> provides
183 the properties of the objects in the list as roles.
184
185 The following application creates a \c DataObject class that with
186 Q_PROPERTY values that will be accessible as named roles when a
187 QList<DataObject*> is exposed to QML:
188
189 \snippet examples/declarative/modelviews/objectlistmodel/dataobject.h 0
190 \dots 4
191 \snippet examples/declarative/modelviews/objectlistmodel/dataobject.h 1
192 \codeline
193 \snippet examples/declarative/modelviews/objectlistmodel/main.cpp 0
194 \dots
195
196 The QObject* is available as the \c modelData property.  As a convenience,
197 the properties of the object are also made available directly in the
198 delegate's context. Here, \c view.qml references the \c DataModel properties in
199 the ListView delegate:
200
201 \snippet examples/declarative/modelviews/objectlistmodel/view.qml 0
202
203 Note the use of the fully qualified access to the \c color property.
204 The properties of the object are not replicated in the \c model
205 object, since they are easily available via the \c modelData
206 object.
207
208 The complete example is available in Qt's \l {declarative/modelviews/objectlistmodel}{examples/declarative/modelviews/objectlistmodel} directory.
209
210 Note: There is no way for the view to know that the contents of a QList
211 have changed.  If the QList changes, it will be necessary to reset
212 the model by calling QDeclarativeContext::setContextProperty() again.
213
214
215 \section2 QAbstractItemModel
216
217 A model can be defined by subclassing QAbstractItemModel. This is the
218 best approach if you have a more complex model that cannot be supported
219 by the other approaches. A QAbstractItemModel can also automatically
220 notify a QML view when the model data has changed.
221
222 The roles of a QAbstractItemModel subclass can be exposed to QML by calling
223 QAbstractItemModel::setRoleNames(). The default role names set by Qt are:
224
225 \table
226 \header
227 \o Qt Role
228 \o QML Role Name
229 \row
230 \o Qt::DisplayRole
231 \o display
232 \row
233 \o Qt::DecorationRole
234 \o decoration
235 \endtable
236
237 Here is an application with a QAbstractListModel subclass named \c AnimalModel
238 that has \e type and \e size roles. It calls QAbstractItemModel::setRoleNames() to set the
239 role names for accessing the properties via QML:
240
241 \snippet examples/declarative/modelviews/abstractitemmodel/model.h 0
242 \dots
243 \snippet examples/declarative/modelviews/abstractitemmodel/model.h 1
244 \dots
245 \snippet examples/declarative/modelviews/abstractitemmodel/model.h 2
246 \codeline
247 \snippet examples/declarative/modelviews/abstractitemmodel/model.cpp 0
248 \codeline
249 \snippet examples/declarative/modelviews/abstractitemmodel/main.cpp 0
250 \dots
251
252 This model is displayed by a ListView delegate that accesses the \e type and \e size
253 roles:
254
255 \snippet examples/declarative/modelviews/abstractitemmodel/view.qml 0
256
257 QML views are automatically updated when the model changes. Remember the model
258 must follow the standard rules for model changes and notify the view when
259 the model has changed by using QAbstractItemModel::dataChanged(),
260 QAbstractItemModel::beginInsertRows(), etc. See the \l {Model subclassing reference} for
261 more information.
262
263 The complete example is available in Qt's \l {declarative/modelviews/abstractitemmodel}{examples/declarative/modelviews/abstractitemmodel} directory.
264
265 QAbstractItemModel presents a hierarchy of tables, but the views currently provided by QML
266 can only display list data.
267 In order to display child lists of a hierarchical model
268 the VisualDataModel element provides several properties and functions for use
269 with models of type QAbstractItemModel:
270
271 \list
272 \o \e hasModelChildren role property to determine whether a node has child nodes.
273 \o \l VisualDataModel::rootIndex allows the root node to be specifed
274 \o \l VisualDataModel::modelIndex() returns a QModelIndex which can be assigned to VisualDataModel::rootIndex
275 \o \l VisualDataModel::parentModelIndex() returns a QModelIndex which can be assigned to VisualDataModel::rootIndex
276 \endlist
277
278
279 \section2 Exposing C++ Data Models to QML
280
281 The above examples use QDeclarativeContext::setContextProperty() to set
282 model values directly in QML components. An alternative to this is to
283 register the C++ model class as a QML type from a QML C++ plugin using
284 QDeclarativeExtensionPlugin. This would allow the model classes to be
285 created directly as elements within QML:
286
287 \table
288 \row
289
290 \o
291 \code
292 class MyModelPlugin : public QDeclarativeExtensionPlugin
293 {
294 public:
295     void registerTypes(const char *uri)
296     {
297         qmlRegisterType<MyModel>(uri, 1, 0,
298                 "MyModel");
299     }
300 }
301
302 Q_EXPORT_PLUGIN2(mymodelplugin, MyModelPlugin);
303 \endcode
304
305 \o
306 \qml
307 MyModel {
308     id: myModel
309     ListElement { someProperty: "some value" }
310 }
311 \endqml
312
313 \qml
314 ListView {
315     width: 200; height: 250
316     model: myModel
317     delegate: Text { text: someProperty }
318 }
319 \endqml
320
321 \endtable
322
323 See \l {Tutorial: Writing QML extensions with C++} for details on writing QML C++
324 plugins.
325
326
327
328 \section1 Other Data Models
329
330
331 \section2 An Integer
332
333 An integer can be used to specify a model that contains a certain number
334 of elements. In this case, the model does not have any data roles.
335
336 The following example creates a ListView with five elements:
337 \qml
338 Item {
339     width: 200; height: 250
340
341     Component {
342         id: itemDelegate
343         Text { text: "I am item number: " + index }
344     }
345
346     ListView {
347         anchors.fill: parent
348         model: 5
349         delegate: itemDelegate
350     }
351
352 }
353 \endqml
354
355
356 \section2 An Object Instance
357
358 An object instance can be used to specify a model with a single object element.  The
359 properties of the object are provided as roles.
360
361 The example below creates a list with one item, showing the color of the
362 \e myText text.  Note the use of the fully qualified \e model.color property
363 to avoid clashing with \e color property of the Text element in the delegate.
364
365 \qml
366 Rectangle {
367     width: 200; height: 250
368
369     Text {
370         id: myText
371         text: "Hello"
372         color: "#dd44ee"
373     }
374
375     Component {
376         id: myDelegate
377         Text { text: model.color }
378     }
379
380     ListView {
381         anchors.fill: parent
382         anchors.topMargin: 30
383         model: myText
384         delegate: myDelegate
385     }
386 }
387 \endqml
388
389 \section1 Accessing Views and Models from Delegates
390
391 You can access the view for which a delegate is used, and its
392 properties, by using ListView.view in a delegate on a ListView, or
393 GridView.view in a delegate on a GridView, etc. In particular, you can
394 access the model and its properties by using ListView.view.model.
395
396 This is useful when you want to use the same delegate for a number of
397 views, for example, but you want decorations or other features to be
398 different for each view, and you would like these different settings to
399 be properties of each of the views. Similarly, it might be of interest
400 to access or show some properties of the model.
401
402 In the following example, the delegate shows the property \e{language}
403 of the model, and the color of one of the fields depends on the
404 property \e{fruit_color} of the view.
405
406 \snippet doc/src/snippets/declarative/models/views-models-delegates.qml rectangle
407
408 Another important case is when some action (e.g. mouse click) in the
409 delegate should update data in the model. In this case you can define
410 a function in the model, e.g.:
411
412 \code
413         setData(int row, const QString & field_name, QVariant new_value),
414 \endcode
415
416 ...and call it from the delegate using:
417
418 \js
419         ListView.view.model.setData(index, field, value)
420 \endjs
421
422 ...assuming that \e{field} holds the name of the field which should be
423 updated, and that \e{value} holds the new value.
424
425 */
426
427 /*!
428 \page qml-presenting-data.html
429 \inqmlmodule QtQuick 1
430 \title Presenting Data with QML
431
432 \section1 Introduction
433
434 Qt Quick contains a set of standard items that can be used to present data in a
435 number of different ways. For simple user interfaces,
436 \l{Using QML Positioner and Repeater Items#Repeaters}{Repeaters} can be used
437 in combination with
438 \l{Using QML Positioner and Repeater Items#Positioners}{Positioners}
439 to obtain pieces of data and arrange them in a user interface. However, when
440 large quantities of data are involved, it is often better to use models with
441 the standard views since these contain many built-in display and navigation
442 features.
443
444 \section1 Views
445
446 Views are scrolling containers for collections of items. They are feature-rich,
447 supporting many of the use cases found in typical applications, and can be
448 customized to meet requirements on style and behavior.
449
450 A set of standard views are provided in the basic set of Qt Quick
451 graphical elements:
452
453 \list
454 \o \l{#ListView}{ListView} arranges items in a horizontal or vertical list
455 \o \l{#GridView}{GridView} arranges items in a grid within the available space
456 \o \l{#PathView}{PathView} arranges items on a path
457 \endlist
458
459 Unlike these items, \l WebView is not a fully-featured view item, and needs
460 to be combined with a \l Flickable item to create a view that performs like
461 a Web browser.
462
463 \section2 ListView
464
465 \l ListView shows a classic list of items with horizontal or vertical placing
466 of items.
467
468 \beginfloatright
469 \inlineimage qml-listview-snippet.png
470 \endfloat
471
472 The following example shows a minimal ListView displaying a sequence of
473 numbers (using an \l{QML Data Models#An Integer}{integer as a model}).
474 A simple delegate is used to define an items for each piece of data in the
475 model.
476
477 \clearfloat
478 \snippet doc/src/snippets/declarative/listview/listview-snippet.qml document
479
480
481
482 \section2 GridView
483
484 \l GridView displays items in a grid like an file manager's icon view.
485
486 \section2 PathView
487
488 \l PathView displays items on a path, where the selection remains in
489 the same place and the items move around it.
490
491 \section1 Decorating Views
492
493 \section2 Headers and Footers
494
495 \section2 Sections
496
497 \section2 Navigation
498
499 In traditional user interfaces, views can be scrolled using standard
500 controls, such as scroll bars and arrow buttons. In some situations, it
501 is also possible to drag the view directly by pressing and holding a
502 mouse button while moving the cursor. In touch-based user interfaces,
503 this dragging action is often complemented with a flicking action, where
504 scrolling continues after the user has stopped touching the view.
505
506 \section1 Further Reading
507 */