diff options
Diffstat (limited to 'doc/src/snippets')
-rw-r--r-- | doc/src/snippets/stringlistmodel/model.cpp | 32 |
1 files changed, 31 insertions, 1 deletions
diff --git a/doc/src/snippets/stringlistmodel/model.cpp b/doc/src/snippets/stringlistmodel/model.cpp index 76329dd..49e0fc7 100644 --- a/doc/src/snippets/stringlistmodel/model.cpp +++ b/doc/src/snippets/stringlistmodel/model.cpp @@ -59,6 +59,11 @@ int StringListModel::rowCount(const QModelIndex &parent) const } //! [0] + +#ifdef 0 +// This represents a read-only version of data(), an early stage in the +// development of the example leading to an editable StringListModel. + /*! Returns an appropriate value for the requested data. If the view requests an invalid index, an invalid variant is returned. @@ -66,7 +71,7 @@ int StringListModel::rowCount(const QModelIndex &parent) const string to be returned. */ -//! [1] +//! [1-data-read-only] QVariant StringListModel::data(const QModelIndex &index, int role) const { if (!index.isValid()) @@ -80,6 +85,31 @@ QVariant StringListModel::data(const QModelIndex &index, int role) const else return QVariant(); } +//! [1-data-read-only] +#endif + + +/*! + Returns an appropriate value for the requested data. + If the view requests an invalid index, an invalid variant is returned. + Any valid index that corresponds to a string in the list causes that + string to be returned. +*/ + +//! [1] +QVariant StringListModel::data(const QModelIndex &index, int role) const +{ + if (!index.isValid()) + return QVariant(); + + if (index.row() >= stringList.size()) + return QVariant(); + + if (role == Qt::DisplayRole || role == Qt::EditRole) + return stringList.at(index.row()); + else + return QVariant(); +} //! [1] /*! |