diff options
Diffstat (limited to 'src/quick/doc/src/concepts/modelviewsdata/cppmodels.qdoc')
-rw-r--r-- | src/quick/doc/src/concepts/modelviewsdata/cppmodels.qdoc | 76 |
1 files changed, 49 insertions, 27 deletions
diff --git a/src/quick/doc/src/concepts/modelviewsdata/cppmodels.qdoc b/src/quick/doc/src/concepts/modelviewsdata/cppmodels.qdoc index e8265b92d2..a849a30b4e 100644 --- a/src/quick/doc/src/concepts/modelviewsdata/cppmodels.qdoc +++ b/src/quick/doc/src/concepts/modelviewsdata/cppmodels.qdoc @@ -233,34 +233,10 @@ contact details from an SQLite database. \section3 Editable Data Model -Besides the \c roleNames() and \c data(), the editable models must reimplement -the \l{QSqlTableModel::}{setData} method to save changes to existing SQL data. -The following version of the method checks if the given model index is valid -and the \c role is equal to \l Qt::EditRole, before calling the parent class -version: - -\code -bool SqlEditableModel::setData(const QModelIndex &item, const QVariant &value, int role) -{ - if (item.isValid() && role == Qt::EditRole) { - QSqlTableModel::setData(item, value,role); - emit dataChanged(item, item); - return true; - } - return false; +QSqlTableModel implements setData() as described \l{#changing-model-data}{below}. -} -\endcode - -\note It is important to emit the \l{QAbstractItemModel::}{dataChanged}() -signal after saving the changes. - -Unlike the C++ item views such as QListView or QTableView, the \c setData() -method must be explicitly invoked from QML whenever appropriate. For example, -on the \l[QML]{TextField::}{editingFinished}() or \l[QML]{TextField::}{accepted}() -signal of \l[QtQuickControls]{TextField}. Depending on the -\l{QSqlTableModel::}{EditStrategy} used by the model, the changes are either -queued for submission later or submitted immediately. +Depending on the \l{QSqlTableModel::}{EditStrategy} used by the model, the +changes are either queued for submission later or submitted immediately. You can also insert new data into the model by calling \l {QSqlTableModel::insertRecord}(). In the following example snippet, @@ -327,5 +303,51 @@ ListView { See \l {Writing QML Extensions with C++} for details on writing QML C++ plugins. +\section2 Changing Model Data + +Besides the \c roleNames() and \c data(), editable models must reimplement +the \l{QAbstractItemModel::}{setData} method to save changes to existing model data. +The following version of the method checks if the given model index is valid +and the \c role is equal to \l Qt::EditRole: + +\code +bool EditableModel::setData(const QModelIndex &index, const QVariant &value, int role) +{ + if (index.isValid() && role == Qt::EditRole) { + // Set data in model here. It can also be a good idea to check whether + // the new value actually differs from the current value + if (m_entries[index.row()] != value.toString()) { + m_entries[index.row()] = value.toString(); + emit dataChanged(index, index, { Qt::EditRole, Qt::DisplayRole }); + return true; + } + } + return false; +} +\endcode + +\note It is important to emit the \l{QAbstractItemModel::}{dataChanged}() +signal after saving the changes. + +Unlike the C++ item views such as QListView or QTableView, the \c setData() +method must be explicitly invoked from QML delegates whenever appropriate. This is done +by simply assigning a new value to the corresponding model property. For example, +on the \l[QML]{TextField::}{editingFinished}() or \l[QML]{TextField::}{accepted}() +signal of \l[QtQuickControls]{TextField}. + +\qml +ListView { + anchors.fill: parent + model: EditableModel {} + delegate: TextField { + width: ListView.view.width + text: model.edit + onAccepted: model.edit = text + } +} +\endqml + +\note The \c edit role is equal to \l Qt::EditRole. See \l{QAbstractItemModel::}{roleNames}() +for the built-in role names. However, real life models would usually register custom roles. */ |