QSqlTableModel::removeRows(): require valid full range of rows

If an invalid range of rows is specified, it's likely to be a
programming or user error. The old behavior of ignoring out of range
rows seems dangerous and complicates the code.

Also implement the documented behavior of returning false if
changes are unsuccessful for OnFieldChange and OnRowChange.
Previously the return value of submit() was ignored.

Updated and improved documentation.

Change-Id: Iaaf51c6d9a0c8c06fd5d186b4b88358fbeab9936
Reviewed-by: Yunqiao Yin <charles.yin@nokia.com>
This commit is contained in:
Mark Brand 2012-02-06 17:19:19 +01:00 committed by Qt by Nokia
parent f5e1da12f0
commit 269ef14215
2 changed files with 25 additions and 13 deletions

5
dist/changes-5.0.0 vendored
View File

@ -331,6 +331,11 @@ QTestLib
* removeRows() no longer emits extra beforeDelete signal for out of range row.
* removeRows() now requires the whole range of targetted rows to be valid
before doing anything. Previously, it would remove what it could and
ignore the rest of the range.
****************************************************************************
* Database Drivers *
****************************************************************************

View File

@ -974,13 +974,20 @@ bool QSqlTableModel::removeColumns(int column, int count, const QModelIndex &par
does not support hierarchical structures, \a parent must be
an invalid model index.
Emits the beforeDelete() signal before a row is deleted. When
the edit strategy is OnManualSubmit signal emission is delayed
until submitAll() is called.
When the edit strategy is OnManualSubmit, deletion of rows from
the database is delayed until submitAll() is called; otherwise,
deletions are immediate.
Returns true if all rows could be removed; otherwise returns
false. Detailed error information can be retrieved using
lastError().
Inserted but not yet submitted rows in the range to be removed
are immediately removed from the model.
Before a row is deleted from the database, the beforeDelete()
signal is emitted.
If row < 0 or row + count > rowCount(), no action is taken and
false is returned. Returns true if all rows could be removed;
otherwise returns false. Detailed database error information
can be retrieved using lastError().
\sa removeColumns(), insertRows()
*/
@ -989,9 +996,12 @@ bool QSqlTableModel::removeRows(int row, int count, const QModelIndex &parent)
Q_D(QSqlTableModel);
if (parent.isValid() || row < 0 || count <= 0)
return false;
else if (row + count > rowCount())
return false;
else if (!count)
return true;
int i;
for (i = 0; i < count && row + i < rowCount(); ++i) {
for (int i = 0; i < count; ++i) {
int idx = row + i;
if (d->cache.value(idx).op() == QSqlTableModelPrivate::Insert) {
revertRow(idx);
@ -1007,11 +1017,8 @@ bool QSqlTableModel::removeRows(int row, int count, const QModelIndex &parent)
}
}
if (d->strategy != OnManualSubmit && i > 0)
submit();
if (i < count)
return false;
if (d->strategy != OnManualSubmit)
return submit();
return true;
}