diff options
-rw-r--r-- | doc/src/declarative/examples.qdoc | 7 | ||||
-rw-r--r-- | doc/src/declarative/qdeclarativemodels.qdoc | 264 | ||||
-rw-r--r-- | doc/src/examples/qml-examples.qdoc | 15 | ||||
-rw-r--r-- | doc/src/images/qml-abstractitemmodel-example.png | bin | 0 -> 4478 bytes | |||
-rw-r--r-- | examples/declarative/modelviews/abstractitemmodel/abstractitemmodel.pro | 8 | ||||
-rw-r--r-- | examples/declarative/modelviews/abstractitemmodel/abstractitemmodel.qrc | 6 | ||||
-rw-r--r-- | examples/declarative/modelviews/abstractitemmodel/main.cpp | 66 | ||||
-rw-r--r-- | examples/declarative/modelviews/abstractitemmodel/model.cpp | 88 | ||||
-rw-r--r-- | examples/declarative/modelviews/abstractitemmodel/model.h | 83 | ||||
-rw-r--r-- | examples/declarative/modelviews/abstractitemmodel/view.qml | 51 | ||||
-rw-r--r-- | examples/declarative/modelviews/objectlistmodel/main.cpp | 6 | ||||
-rw-r--r-- | examples/declarative/modelviews/stringlistmodel/main.cpp | 2 |
12 files changed, 469 insertions, 127 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 Binary files differnew file mode 100644 index 0000000..1d7ff19 --- /dev/null +++ b/doc/src/images/qml-abstractitemmodel-example.png diff --git a/examples/declarative/modelviews/abstractitemmodel/abstractitemmodel.pro b/examples/declarative/modelviews/abstractitemmodel/abstractitemmodel.pro new file mode 100644 index 0000000..55e67f3 --- /dev/null +++ b/examples/declarative/modelviews/abstractitemmodel/abstractitemmodel.pro @@ -0,0 +1,8 @@ +TEMPLATE = app + +QT += declarative + +RESOURCES += abstractitemmodel.qrc + +HEADERS = model.h +SOURCES = main.cpp model.cpp diff --git a/examples/declarative/modelviews/abstractitemmodel/abstractitemmodel.qrc b/examples/declarative/modelviews/abstractitemmodel/abstractitemmodel.qrc new file mode 100644 index 0000000..4ae861c --- /dev/null +++ b/examples/declarative/modelviews/abstractitemmodel/abstractitemmodel.qrc @@ -0,0 +1,6 @@ +<!DOCTYPE RCC><RCC version="1.0"> +<qresource> + <file>view.qml</file> +</qresource> +</RCC> + diff --git a/examples/declarative/modelviews/abstractitemmodel/main.cpp b/examples/declarative/modelviews/abstractitemmodel/main.cpp new file mode 100644 index 0000000..e869aba --- /dev/null +++ b/examples/declarative/modelviews/abstractitemmodel/main.cpp @@ -0,0 +1,66 @@ +/**************************************************************************** +** +** Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies). +** All rights reserved. +** Contact: Nokia Corporation (qt-info@nokia.com) +** +** This file is part of the QtDeclarative module of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:BSD$ +** You may use this file under the terms of the BSD license as follows: +** +** "Redistribution and use in source and binary forms, with or without +** modification, are permitted provided that the following conditions are +** met: +** * Redistributions of source code must retain the above copyright +** notice, this list of conditions and the following disclaimer. +** * Redistributions in binary form must reproduce the above copyright +** notice, this list of conditions and the following disclaimer in +** the documentation and/or other materials provided with the +** distribution. +** * Neither the name of Nokia Corporation and its Subsidiary(-ies) nor +** the names of its contributors may be used to endorse or promote +** products derived from this software without specific prior written +** permission. +** +** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR +** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT +** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT +** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, +** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY +** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE." +** $QT_END_LICENSE$ +** +****************************************************************************/ +#include "model.h" +#include <QDeclarativeContext> +#include <QDeclarativeView> + +#include <QApplication> + +//![0] +int main(int argc, char ** argv) +{ + QApplication app(argc, argv); + + AnimalModel model; + model.addAnimal(Animal("Wolf", "Medium")); + model.addAnimal(Animal("Polar bear", "Large")); + model.addAnimal(Animal("Quoll", "Small")); + + QDeclarativeView view; + QDeclarativeContext *ctxt = view.rootContext(); + ctxt->setContextProperty("myModel", &model); +//![0] + + view.setSource(QUrl("qrc:view.qml")); + view.show(); + + return app.exec(); +} + diff --git a/examples/declarative/modelviews/abstractitemmodel/model.cpp b/examples/declarative/modelviews/abstractitemmodel/model.cpp new file mode 100644 index 0000000..d0e0971 --- /dev/null +++ b/examples/declarative/modelviews/abstractitemmodel/model.cpp @@ -0,0 +1,88 @@ +/**************************************************************************** +** +** Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies). +** All rights reserved. +** Contact: Nokia Corporation (qt-info@nokia.com) +** +** This file is part of the QtDeclarative module of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:BSD$ +** You may use this file under the terms of the BSD license as follows: +** +** "Redistribution and use in source and binary forms, with or without +** modification, are permitted provided that the following conditions are +** met: +** * Redistributions of source code must retain the above copyright +** notice, this list of conditions and the following disclaimer. +** * Redistributions in binary form must reproduce the above copyright +** notice, this list of conditions and the following disclaimer in +** the documentation and/or other materials provided with the +** distribution. +** * Neither the name of Nokia Corporation and its Subsidiary(-ies) nor +** the names of its contributors may be used to endorse or promote +** products derived from this software without specific prior written +** permission. +** +** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR +** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT +** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT +** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, +** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY +** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE." +** $QT_END_LICENSE$ +** +****************************************************************************/ +#include "model.h" + +Animal::Animal(const QString &type, const QString &size) + : m_type(type), m_size(size) +{ +} + +QString Animal::type() const +{ + return m_type; +} + +QString Animal::size() const +{ + return m_size; +} + +//![0] +AnimalModel::AnimalModel(QObject *parent) + : QAbstractListModel(parent) +{ + QHash<int, QByteArray> roles; + roles[TypeRole] = "type"; + roles[SizeRole] = "size"; + setRoleNames(roles); +} +//![0] + +void AnimalModel::addAnimal(const Animal &animal) +{ + m_animals << animal; +} + +int AnimalModel::rowCount(const QModelIndex & parent) const { + return m_animals.count(); +} + +QVariant AnimalModel::data(const QModelIndex & index, int role) const { + if (index.row() < 0 || index.row() > m_animals.count()) + return QVariant(); + + const Animal &animal = m_animals[index.row()]; + if (role == TypeRole) + return animal.type(); + else if (role == SizeRole) + return animal.size(); + return QVariant(); +} + diff --git a/examples/declarative/modelviews/abstractitemmodel/model.h b/examples/declarative/modelviews/abstractitemmodel/model.h new file mode 100644 index 0000000..1119837 --- /dev/null +++ b/examples/declarative/modelviews/abstractitemmodel/model.h @@ -0,0 +1,83 @@ +/**************************************************************************** +** +** Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies). +** All rights reserved. +** Contact: Nokia Corporation (qt-info@nokia.com) +** +** This file is part of the QtDeclarative module of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:BSD$ +** You may use this file under the terms of the BSD license as follows: +** +** "Redistribution and use in source and binary forms, with or without +** modification, are permitted provided that the following conditions are +** met: +** * Redistributions of source code must retain the above copyright +** notice, this list of conditions and the following disclaimer. +** * Redistributions in binary form must reproduce the above copyright +** notice, this list of conditions and the following disclaimer in +** the documentation and/or other materials provided with the +** distribution. +** * Neither the name of Nokia Corporation and its Subsidiary(-ies) nor +** the names of its contributors may be used to endorse or promote +** products derived from this software without specific prior written +** permission. +** +** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR +** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT +** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT +** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, +** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY +** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE." +** $QT_END_LICENSE$ +** +****************************************************************************/ +#include <QAbstractListModel> +#include <QStringList> + +//![0] +class Animal +{ +public: + Animal(const QString &type, const QString &size); +//![0] + + QString type() const; + QString size() const; + +private: + QString m_type; + QString m_size; +//![1] +}; + +class AnimalModel : public QAbstractListModel +{ + Q_OBJECT +public: + enum AnimalRoles { + TypeRole = Qt::UserRole + 1, + SizeRole + }; + + AnimalModel(QObject *parent = 0); +//![1] + + void addAnimal(const Animal &animal); + + int rowCount(const QModelIndex & parent = QModelIndex()) const; + + QVariant data(const QModelIndex & index, int role = Qt::DisplayRole) const; + +private: + QList<Animal> m_animals; +//![2] +}; +//![2] + + diff --git a/examples/declarative/modelviews/abstractitemmodel/view.qml b/examples/declarative/modelviews/abstractitemmodel/view.qml new file mode 100644 index 0000000..591d89a --- /dev/null +++ b/examples/declarative/modelviews/abstractitemmodel/view.qml @@ -0,0 +1,51 @@ +/**************************************************************************** +** +** Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies). +** All rights reserved. +** Contact: Nokia Corporation (qt-info@nokia.com) +** +** This file is part of the QtDeclarative module of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:BSD$ +** You may use this file under the terms of the BSD license as follows: +** +** "Redistribution and use in source and binary forms, with or without +** modification, are permitted provided that the following conditions are +** met: +** * Redistributions of source code must retain the above copyright +** notice, this list of conditions and the following disclaimer. +** * Redistributions in binary form must reproduce the above copyright +** notice, this list of conditions and the following disclaimer in +** the documentation and/or other materials provided with the +** distribution. +** * Neither the name of Nokia Corporation and its Subsidiary(-ies) nor +** the names of its contributors may be used to endorse or promote +** products derived from this software without specific prior written +** permission. +** +** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR +** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT +** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT +** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, +** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY +** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE." +** $QT_END_LICENSE$ +** +****************************************************************************/ +import Qt 4.7 + +//![0] +ListView { + width: 200; height: 250 + anchors.fill: parent + + model: myModel + delegate: Text { text: "Animal: " + type + ", " + size } +} +//![0] + diff --git a/examples/declarative/modelviews/objectlistmodel/main.cpp b/examples/declarative/modelviews/objectlistmodel/main.cpp index 8c921b7..89c22a1 100644 --- a/examples/declarative/modelviews/objectlistmodel/main.cpp +++ b/examples/declarative/modelviews/objectlistmodel/main.cpp @@ -56,10 +56,8 @@ //![0] int main(int argc, char ** argv) { -//![0] QApplication app(argc, argv); -//![1] QList<QObject*> dataList; dataList.append(new DataObject("Item 1", "red")); dataList.append(new DataObject("Item 2", "green")); @@ -69,13 +67,11 @@ int main(int argc, char ** argv) QDeclarativeView view; QDeclarativeContext *ctxt = view.rootContext(); ctxt->setContextProperty("myModel", QVariant::fromValue(dataList)); +//![0] view.setSource(QUrl("qrc:view.qml")); view.show(); -//![1] return app.exec(); -//![2] } -//![2] diff --git a/examples/declarative/modelviews/stringlistmodel/main.cpp b/examples/declarative/modelviews/stringlistmodel/main.cpp index ea73859..bf46c63 100644 --- a/examples/declarative/modelviews/stringlistmodel/main.cpp +++ b/examples/declarative/modelviews/stringlistmodel/main.cpp @@ -66,10 +66,10 @@ int main(int argc, char ** argv) QDeclarativeView view; QDeclarativeContext *ctxt = view.rootContext(); ctxt->setContextProperty("myModel", QVariant::fromValue(dataList)); +//![0] view.setSource(QUrl("qrc:view.qml")); view.show(); -//![0] return app.exec(); } |