summaryrefslogtreecommitdiffstats
path: root/doc
diff options
context:
space:
mode:
authorBea Lam <bea.lam@nokia.com>2010-07-01 06:32:30 (GMT)
committerBea Lam <bea.lam@nokia.com>2010-07-01 06:55:42 (GMT)
commit3084b54e7a4f6163dadcf3e52143391b10b5352e (patch)
tree7e5cdb9fde77a0164ac5c01a0a05bc392c01a0a4 /doc
parent0e7f70bb44eae2798c7f2198b713dc1c6ab6f652 (diff)
downloadQt-3084b54e7a4f6163dadcf3e52143391b10b5352e.zip
Qt-3084b54e7a4f6163dadcf3e52143391b10b5352e.tar.gz
Qt-3084b54e7a4f6163dadcf3e52143391b10b5352e.tar.bz2
Add abstractitemmodel example, and fix some model docs
Diffstat (limited to 'doc')
-rw-r--r--doc/src/declarative/examples.qdoc7
-rw-r--r--doc/src/declarative/qdeclarativemodels.qdoc264
-rw-r--r--doc/src/examples/qml-examples.qdoc15
-rw-r--r--doc/src/images/qml-abstractitemmodel-example.pngbin0 -> 4478 bytes
4 files changed, 165 insertions, 121 deletions
diff --git a/doc/src/declarative/examples.qdoc b/doc/src/declarative/examples.qdoc
index 7b02d33..587cdf2 100644
--- a/doc/src/declarative/examples.qdoc
+++ b/doc/src/declarative/examples.qdoc
@@ -168,11 +168,14 @@ The examples can be found in Qt's \c examples/declarative directory.
\list
\o \l{declarative/modelviews/gridview}{GridView}
\o \l{declarative/modelviews/listview}{ListView}
-\o \l{declarative/modelviews/objectlistmodel}{Object ListModel}
\o \l{declarative/modelviews/package}{Package}
\o \l{declarative/modelviews/parallax}{Parallax}
-\o \l{declarative/modelviews/stringlistmodel}{String ListModel}
\o \l{declarative/modelviews/visualitemmodel}{VisualItemModel}
+
+\o \l{declarative/modelviews/stringlistmodel}{String ListModel}
+\o \l{declarative/modelviews/objectlistmodel}{Object ListModel}
+\o \l{declarative/modelviews/abstractitemmodel}{AbstractItemModel}
+
\o \l{declarative/modelviews/webview}{WebView}
\endlist
diff --git a/doc/src/declarative/qdeclarativemodels.qdoc b/doc/src/declarative/qdeclarativemodels.qdoc
index 0d25da5..b44e6f2 100644
--- a/doc/src/declarative/qdeclarativemodels.qdoc
+++ b/doc/src/declarative/qdeclarativemodels.qdoc
@@ -30,7 +30,8 @@
\target qmlmodels
\title Data Models
-Some QML items use Data Models to provide the data to be displayed.
+QML items such as ListView, GridView and \l Repeater require Data Models
+that provide the data to be displayed.
These items typically require a \e delegate component that
creates an instance for each item in the model. Models may be static, or
have items modified, inserted, removed or moved dynamically.
@@ -66,55 +67,30 @@ Item {
\endqml
If there is a naming clash between the model's properties and the delegate's
-properties (e.g. if the \l Text delegate above had \e type or \e age properties)
-the roles can be accessed with the qualified \e model name instead.
-For example, the following model has a \e color role which clashes with the
-delegate \l Rectangle's \e color property. The clash is avoided by referencing
-the \e color property of the model by its full name: \e model.color.
-
-\code
-ListModel {
- id: myModel
- ListElement { color: "red" }
- ListElement { color: "green" }
-}
-
-Component {
- id: myDelegate
- Rectangle {
- width: 20; height: 20
- color: model.color
- }
-}
-\endcode
+properties, the roles can be accessed with the qualified \e model name instead.
+For example, if a \l Text element had \e type or \e age properties, the text in the
+above example would display those property values instead of the \e type and \e age values
+from the model item. In this case, the properties could have been referenced as
+\c model.type and \c model.age instead to ensure the delegate displays the
+property values from the model item.
A special \e index role containing the index of the item in the model
-is also available to the delegate.
-
-\e Note: the index role will be set to -1 if the item is removed from
+is also available to the delegate. Note this index is set to -1 if the item is removed from
the model. If you bind to the index role, be sure that the logic
accounts for the possibility of index being -1, i.e. that the item
-is no longer valid. Usually the item will shortly be destroyed, but
-it is possible to delay delegate destruction in some views via a delayRemove
-attached property.
+is no longer valid. (Usually the item will shortly be destroyed, but
+it is possible to delay delegate destruction in some views via a \c delayRemove
+attached property.)
-Models that do not have named roles will have the data provided via
-the \e modelData role. The \e modelData role is also provided for
-Models that have only one role. In this case the \e modelData role
+Models that do not have named roles (such as the QStringList model shown below)
+will have the data provided via the \e modelData role. The \e modelData role is also provided for
+models that have only one role. In this case the \e modelData role
contains the same data as the named role.
-There are a number of QML elements that operate using data models:
-
-\list
-\o ListView
-\o GridView
-\o PathView
-\o \l Repeater
-\endlist
+QML provides several types of data models among the built-in set of
+QML elements. In addition, models can be created with C++ and then
+made available to QML components.
-QML supports several types of data model, which may be provided by QML
-or C++ (via QDeclarativeContext::setContextProperty() or as plugin types,
-for example).
\section1 QML Data Models
@@ -126,6 +102,7 @@ available roles are specified by the \l ListElement properties.
\code
ListModel {
id: fruitModel
+
ListElement {
name: "Apple"
cost: 2.45
@@ -145,30 +122,26 @@ The above model has two roles, \e name and \e cost. These can be bound
to by a ListView delegate, for example:
\code
-Component {
- id: fruitDelegate
- Row {
+ListView {
+ width: 200; height: 250
+ model: fruitModel
+ delegate: Row {
Text { text: "Fruit: " + name }
Text { text: "Cost: $" + cost }
}
}
-ListView {
- model: fruitModel
- delegate: fruitDelegate
-}
\endcode
-It is also possible to manipulate the ListModel directly via JavaScript.
-In this case, the first item inserted will determine the roles available
-to any views using the model. For example, if an empty ListModel is
-created and populated via JavaScript the roles provided by the first
+ListModel provides methods to manipulate the ListModel directly via JavaScript.
+In this case, the first item inserted determines the roles available
+to any views that are using the model. For example, if an empty ListModel is
+created and populated via JavaScript, the roles provided by the first
insertion are the only roles that will be shown in the view:
\code
Item {
- ListModel {
- id: fruitModel
- }
+ ListModel { id: fruitModel }
+
MouseArea {
anchors.fill: parent
onClicked: fruitModel.append({"cost": 5.95, "name":"Pizza"})
@@ -176,7 +149,7 @@ Item {
}
\endcode
-When the MouseArea is clicked, \c fruitModel will have two roles, "cost" and "name".
+When the MouseArea is clicked, \c fruitModel will have two roles, \e cost and \e name.
Even if subsequent roles are added, only the first two will be handled by views
using the model. To reset the roles available in the model, call ListModel::clear().
@@ -198,14 +171,17 @@ XmlListModel {
}
\endcode
-The \l{declarative/demos/rssnews}{RSS News demo} shows how XmlListModel can
+The \l{demos/declarative/rssnews}{RSS News demo} shows how XmlListModel can
be used to display an RSS feed.
\section2 VisualItemModel
-VisualItemModel allows QML items to be provided as a model. This model contains
-both the data and delegate (its child items). This model does not provide any roles.
+VisualItemModel allows QML items to be provided as a model.
+
+This model contains both the data and delegate; the child items of a
+VisualItemModel provide the contents of the delegate. The model
+does not provide any roles.
\code
VisualItemModel {
@@ -228,51 +204,11 @@ will be positioned by the view.
\section1 C++ Data Models
-Models defined in C++ can be made available to QML either from a C++ application or from a
-\l{QDeclarativeExtensionPlugin}{QML C++ plugin}.
-
-\section2 QAbstractItemModel
-
-A model can be defined by subclassing QAbstractItemModel.
-
-QAbstractItemModel provides the roles set via the QAbstractItemModel::setRoleNames() method.
-The default role names set by Qt are:
-
-\table
-\header
-\o Qt Role
-\o QML Role Name
-\row
-\o Qt::DisplayRole
-\o display
-\row
-\o Qt::DecorationRole
-\o decoration
-\endtable
-
-The model could be made available to QML either directly:
-
-\code
-QDeclarativeContext *ctxt = view.rootContext();
-MyModel *model = new MyModel; // subclass of QAbstractItemModel
-ctxt->setContextProperty("myModel", model);
-\endcode
-
-or by registering the subclass as a new QML type in
-a \l{QDeclarativeExtensionPlugin}{QML C++ plugin}.
-
-QAbstractItemModel presents a hierarchy of tables, but views currently provided by QML
-can only display list data.
-In order to display child lists of a hierarchical model
-the VisualDataModel element provides several properties and functions for use
-with models of type QAbstractItemModel:
-\list
-\o \e hasModelChildren role property to determine whether a node has child nodes.
-\o \l VisualDataModel::rootIndex allows the root node to be specifed
-\o \l VisualDataModel::modelIndex() returns a QModelIndex which can be assigned to VisualDataModel::rootIndex
-\o \l VisualDataModel::parentModelIndex() returns a QModelIndex which can be assigned to VisualDataModel::rootIndex
-\endlist
+Models can be defined in C++ and then made available to QML. This is useful
+for exposing existing C++ data models or otherwise complex datasets to QML.
+A C++ model class can be defined as a QStringList, a QList<QObject*> or a
+QAbstractItemModel.
\section2 QStringList
@@ -309,10 +245,7 @@ QList<DataObject*> is exposed to QML:
\snippet examples/declarative/modelviews/objectlistmodel/dataobject.h 1
\codeline
\snippet examples/declarative/modelviews/objectlistmodel/main.cpp 0
-\dots 4
-\snippet examples/declarative/modelviews/objectlistmodel/main.cpp 1
-\dots 4
-\snippet examples/declarative/modelviews/objectlistmodel/main.cpp 2
+\dots
The QObject* is available as the \c modelData property. As a convenience,
the properties of the object are also made available directly in the
@@ -323,7 +256,7 @@ the ListView delegate:
Note the use of the fully qualified access to the \c color property.
The properties of the object are not replicated in the \c model
-object, since they are easily available via the modelData
+object, since they are easily available via the \c modelData
object.
The complete example is available in Qt's \l {declarative/modelviews/objectlistmodel}{examples/declarative/modelviews/objectlistmodel} directory.
@@ -333,13 +266,115 @@ have changed. If the QList changes, it will be necessary to reset
the model by calling QDeclarativeContext::setContextProperty() again.
+\section2 QAbstractItemModel
+
+A model can be defined by subclassing QAbstractItemModel.
+
+The roles of a QAbstractItemModel subclass can be exposed to QML by calling
+QAbstractItemModel::setRoleNames(). The default role names set by Qt are:
+
+\table
+\header
+\o Qt Role
+\o QML Role Name
+\row
+\o Qt::DisplayRole
+\o display
+\row
+\o Qt::DecorationRole
+\o decoration
+\endtable
+
+Here is an application with a QAbstractListModel subclass named \c AnimalModel
+that has \e type and \e size roles. It calls QAbstractItemModel::setRoleNames() to set the
+role names for accessing the properties via QML:
+
+\snippet examples/declarative/modelviews/abstractitemmodel/model.h 0
+\dots
+\snippet examples/declarative/modelviews/abstractitemmodel/model.h 1
+\dots
+\snippet examples/declarative/modelviews/abstractitemmodel/model.h 2
+\codeline
+\snippet examples/declarative/modelviews/abstractitemmodel/model.cpp 0
+\codeline
+\snippet examples/declarative/modelviews/abstractitemmodel/main.cpp 0
+\dots
+
+This model is displayed by a ListView delegate that accesses the \e type and \e size
+roles:
+
+\snippet examples/declarative/modelviews/abstractitemmodel/view.qml 0
+
+The complete example is available in Qt's \l {declarative/modelviews/abstractitemmodel}{examples/declarative/modelviews/abstractitemmodel} directory.
+
+QAbstractItemModel presents a hierarchy of tables, but the views currently provided by QML
+can only display list data.
+In order to display child lists of a hierarchical model
+the VisualDataModel element provides several properties and functions for use
+with models of type QAbstractItemModel:
+
+\list
+\o \e hasModelChildren role property to determine whether a node has child nodes.
+\o \l VisualDataModel::rootIndex allows the root node to be specifed
+\o \l VisualDataModel::modelIndex() returns a QModelIndex which can be assigned to VisualDataModel::rootIndex
+\o \l VisualDataModel::parentModelIndex() returns a QModelIndex which can be assigned to VisualDataModel::rootIndex
+\endlist
+
+
+\section2 Exposing C++ data models to QML
+
+The above examples use QDeclarativeContext::setContextProperty() to set
+model values directly in QML components. An alternative to this is to
+register the C++ model class as a QML type from a QML C++ plugin using
+QDeclarativeExtensionPlugin. This would allow the model classes to be
+created directly as elements within QML:
+
+\table
+\row
+
+\o
+\code
+class MyModelPlugin : public QDeclarativeExtensionPlugin
+{
+public:
+ void registerTypes(const char *uri)
+ {
+ qmlRegisterType<MyModel>(uri, 1, 0,
+ "MyModel");
+ }
+}
+
+Q_EXPORT_PLUGIN2(mymodelplugin, MyModelPlugin);
+\endcode
+
+\o
+\qml
+MyModel {
+ id: myModel
+ ListElement { someProperty: "some value" }
+}
+
+ListView {
+ width: 200; height: 250
+ model: myModel
+ delegate: Text { text: someProperty }
+}
+\endqml
+
+\endtable
+
+See \l {Tutorial: Writing QML extensions with C++} for details on writing QML C++
+plugins.
+
+
+
\section1 Other Data Models
\section2 An Integer
-An Integer specifies a model containing the integer number of elements.
-There are no data roles.
+An integer can be used to specify a model that contains a certain number
+of elements. In this case, the model does not have any data roles.
The following example creates a ListView with five elements:
\code
@@ -363,7 +398,7 @@ Item {
\section2 An Object Instance
-An Object instance specifies a model with a single Object element. The
+An object instance can be used to specify a model with a single object element. The
properties of the object are provided as roles.
The example below creates a list with one item, showing the color of the
@@ -382,10 +417,9 @@ Rectangle {
Component {
id: myDelegate
- Text {
- text: model.color
- }
+ Text { text: model.color }
}
+
ListView {
anchors.fill: parent
anchors.topMargin: 30
diff --git a/doc/src/examples/qml-examples.qdoc b/doc/src/examples/qml-examples.qdoc
index 211ee4b..c8a7403 100644
--- a/doc/src/examples/qml-examples.qdoc
+++ b/doc/src/examples/qml-examples.qdoc
@@ -252,6 +252,15 @@
*/
/*!
+ \title Models and Views: AbstractItemModel
+ \example declarative/modelviews/abstractitemmodel
+
+ This example shows how to use a QAbstractItemModel subclass as a model in QML.
+
+ \image qml-abstractitemmodel-example.png
+*/
+
+/*!
\title Models and Views: GridView
\example declarative/modelviews/gridview
@@ -306,8 +315,7 @@
\title Models and Views: Object ListModel
\example declarative/modelviews/objectlistmodel
- This example shows how to create a C++ extension that exposes a
- QList<QObject*> as a model in QML.
+ This example shows how to use a QList<QObject*> as a model in QML.
\image qml-objectlistmodel-example.png
*/
@@ -334,8 +342,7 @@
\title Models and Views: String ListModel
\example declarative/modelviews/stringlistmodel
- This example shows how to create a C++ extension that exposes a
- QStringList as a model in QML.
+ This example shows how to use a QStringList as a model in QML.
\image qml-stringlistmodel-example.png
*/
diff --git a/doc/src/images/qml-abstractitemmodel-example.png b/doc/src/images/qml-abstractitemmodel-example.png
new file mode 100644
index 0000000..1d7ff19
--- /dev/null
+++ b/doc/src/images/qml-abstractitemmodel-example.png
Binary files differ