Fixed abstractitemmodel example
authorOliver Wolff <oliver.wolff@digia.com>
Thu, 6 Dec 2012 13:54:32 +0000 (14:54 +0100)
committerThe Qt Project <gerrit-noreply@qt-project.org>
Tue, 11 Dec 2012 23:32:24 +0000 (00:32 +0100)
Change-Id: I1e85fd3f35f4f3d80e3fb599ae95b771db5f949b
Reviewed-by: hjk <qthjk@ovi.com>

examples/quick/models/abstractitemmodel/model.cpp
examples/quick/models/abstractitemmodel/model.h
examples/quick/models/models.pro
src/quick/doc/src/concepts/modelviewsdata/cppmodels.qdoc

index 9c24dec..524ed54 100644 (file)
@@ -54,16 +54,10 @@ 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)
 {
@@ -88,3 +82,12 @@ QVariant AnimalModel::data(const QModelIndex & index, int role) const {
     return QVariant();
 }
 
+//![0]
+QHash<int, QByteArray> AnimalModel::roleNames() const {
+    QHash<int, QByteArray> roles;
+    roles[TypeRole] = "type";
+    roles[SizeRole] = "size";
+    return roles;
+}
+//![0]
+
index 9436770..2db178b 100644 (file)
@@ -74,6 +74,8 @@ public:
 
     QVariant data(const QModelIndex & index, int role = Qt::DisplayRole) const;
 
+protected:
+    QHash<int, QByteArray> roleNames() const;
 private:
     QList<Animal> m_animals;
 //![2]
index 60407a6..95d2716 100644 (file)
@@ -1,5 +1,5 @@
 TEMPLATE = subdirs
 SUBDIRS = \
-#    abstractitemmodel \ #Doesn't build right now
+    abstractitemmodel \
     objectlistmodel \
     stringlistmodel
index e9add7b..d9bfee2 100644 (file)
@@ -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