diff options
-rw-r--r-- | examples/quick/models/abstractitemmodel/model.cpp | 15 | ||||
-rw-r--r-- | examples/quick/models/abstractitemmodel/model.h | 2 | ||||
-rw-r--r-- | examples/quick/models/models.pro | 2 | ||||
-rw-r--r-- | src/quick/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 9c24decf2d..524ed5461b 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<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] + diff --git a/examples/quick/models/abstractitemmodel/model.h b/examples/quick/models/abstractitemmodel/model.h index 9436770b19..2db178bae1 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<int, QByteArray> roleNames() const; private: QList<Animal> m_animals; //![2] diff --git a/examples/quick/models/models.pro b/examples/quick/models/models.pro index 60407a679e..95d2716836 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 e9add7b221..d9bfee2638 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 |