QSqlTableModel::removeRows(): require valid full range of rows
authorMark Brand <mabrand@mabrand.nl>
Mon, 6 Feb 2012 16:19:19 +0000 (17:19 +0100)
committerQt by Nokia <qt-info@nokia.com>
Tue, 7 Feb 2012 21:05:51 +0000 (22:05 +0100)
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>
dist/changes-5.0.0
src/sql/models/qsqltablemodel.cpp

index 6bacccc..5317137 100644 (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                                *
 ****************************************************************************
index 4094052..df48115 100644 (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;
 }