From 87d0c02fab03b7d3bf2094af22465f029bcdd096 Mon Sep 17 00:00:00 2001 From: Oliver Wolff Date: Thu, 6 Dec 2012 14:54:32 +0100 Subject: [PATCH] Fixed abstractitemmodel example Change-Id: I1e85fd3f35f4f3d80e3fb599ae95b771db5f949b Reviewed-by: hjk --- examples/quick/models/abstractitemmodel/model.cpp | 15 +++++++++------ examples/quick/models/abstractitemmodel/model.h | 2 ++ examples/quick/models/models.pro | 2 +- .../doc/src/concepts/modelviewsdata/cppmodels.qdoc | 2 +- 4 files changed, 13 insertions(+), 8 deletions(-) diff --git a/examples/quick/models/abstractitemmodel/model.cpp b/examples/quick/models/abstractitemmodel/model.cpp index 9c24dec..524ed54 100644 --- a/examples/quick/models/abstractitemmodel/model.cpp +++ b/examples/quick/models/abstractitemmodel/model.cpp @@ -54,16 +54,10 @@ QString Animal::size() const return m_size; } -//![0] AnimalModel::AnimalModel(QObject *parent) : QAbstractListModel(parent) { - QHash roles; - roles[TypeRole] = "type"; - roles[SizeRole] = "size"; - setRoleNames(roles); } -//![0] void AnimalModel::addAnimal(const Animal &animal) { @@ -88,3 +82,12 @@ QVariant AnimalModel::data(const QModelIndex & index, int role) const { return QVariant(); } +//![0] +QHash AnimalModel::roleNames() const { + QHash roles; + roles[TypeRole] = "type"; + roles[SizeRole] = "size"; + return roles; +} +//![0] + diff --git a/examples/quick/models/abstractitemmodel/model.h b/examples/quick/models/abstractitemmodel/model.h index 9436770..2db178b 100644 --- a/examples/quick/models/abstractitemmodel/model.h +++ b/examples/quick/models/abstractitemmodel/model.h @@ -74,6 +74,8 @@ public: QVariant data(const QModelIndex & index, int role = Qt::DisplayRole) const; +protected: + QHash roleNames() const; private: QList m_animals; //![2] diff --git a/examples/quick/models/models.pro b/examples/quick/models/models.pro index 60407a6..95d2716 100644 --- a/examples/quick/models/models.pro +++ b/examples/quick/models/models.pro @@ -1,5 +1,5 @@ TEMPLATE = subdirs SUBDIRS = \ -# abstractitemmodel \ #Doesn't build right now + abstractitemmodel \ objectlistmodel \ stringlistmodel diff --git a/src/quick/doc/src/concepts/modelviewsdata/cppmodels.qdoc b/src/quick/doc/src/concepts/modelviewsdata/cppmodels.qdoc index e9add7b..d9bfee2 100644 --- a/src/quick/doc/src/concepts/modelviewsdata/cppmodels.qdoc +++ b/src/quick/doc/src/concepts/modelviewsdata/cppmodels.qdoc @@ -121,7 +121,7 @@ QAbstractItemModel::setRoleNames(). The default role names set by Qt are: \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 +that has \e type and \e size roles. It reimplements QAbstractItemModel::roleNames() to set the role names for accessing the properties via QML: \snippet examples/quick/modelviews/abstractitemmodel/model.h 0 -- 1.7.2.5