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