From e37d314eb3e5b26b271aa5ec17dac1f2e8e16a45 Mon Sep 17 00:00:00 2001 From: Peter Yard Date: Fri, 24 Jul 2009 10:46:21 +1000 Subject: #250741 Doc for Making task editable --- doc/src/model-view-programming.qdoc | 9 +++++++-- doc/src/snippets/stringlistmodel/model.cpp | 32 +++++++++++++++++++++++++++++- 2 files changed, 38 insertions(+), 3 deletions(-) diff --git a/doc/src/model-view-programming.qdoc b/doc/src/model-view-programming.qdoc index e216591..7d7db19 100644 --- a/doc/src/model-view-programming.qdoc +++ b/doc/src/model-view-programming.qdoc @@ -1352,7 +1352,7 @@ The \l{QAbstractItemModel::data()}{data()} function is responsible for returning the item of data that corresponds to the index argument: - \snippet doc/src/snippets/stringlistmodel/model.cpp 1 + \snippet doc/src/snippets/stringlistmodel/model.cpp 1-data-read-only We only return a valid QVariant if the model index supplied is valid, the row number is within the range of items in the string list, and the @@ -1390,6 +1390,7 @@ The read-only model shows how simple choices could be presented to the user but, for many applications, an editable list model is much more useful. We can modify the read-only model to make the items editable + by changing the data() function we implemented for read-only, and by implementing two extra functions: \l{QAbstractItemModel::flags()}{flags()} and \l{QAbstractItemModel::setData()}{setData()}. @@ -1399,7 +1400,7 @@ \snippet doc/src/snippets/stringlistmodel/model.h 3 \section2 Making the Model Editable - + A delegate checks whether an item is editable before creating an editor. The model must let the delegate know that its items are editable. We do this by returning the correct flags for each item in @@ -1434,6 +1435,10 @@ one item of data has changed, the range of items specified in the signal is limited to just one model index. + Also the data() function needs to be changed to add the Qt::EditRole test: + + \snippet doc/src/snippets/stringlistmodel/model.cpp 1 + \section2 Inserting and Removing Rows It is possible to change the number of rows and columns in a model. In the 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] /*! -- cgit v0.12