QSqlTableModel: disallow insert if changes are pending
authorMark Brand <mabrand@mabrand.nl>
Wed, 29 Feb 2012 00:51:23 +0000 (01:51 +0100)
committerQt by Nokia <qt-info@nokia.com>
Thu, 15 Mar 2012 14:35:22 +0000 (15:35 +0100)
For OnFieldChange and OnRowChange, inserting rows should not be
allowed if there are pending changes in cache.

Change-Id: Ia794332959a35a1de87e798ba1a74ace3dfae68f
Reviewed-by: Honglei Zhang <honglei.zhang@nokia.com>
dist/changes-5.0.0
src/sql/models/qsqltablemodel.cpp
tests/auto/sql/models/qsqltablemodel/tst_qsqltablemodel.cpp

index c85749f..e10f0df 100644 (file)
@@ -444,7 +444,7 @@ has any changes to submit. QTBUG-3108
 that fail upon resubmitting for edit strategies OnFieldChange and OnRowChange.
 Instead, pending (failed) changes cause new changes inappropriate to the
 edit strategy to be refused. The application should resolve or revert pending
-changes.
+changes. insertRows() and insertRecord() also respect the edit strategy.
 
 ****************************************************************************
 *                          Database Drivers                                *
index a001b26..0eb5447 100644 (file)
@@ -1088,17 +1088,18 @@ bool QSqlTableModel::removeRows(int row, int count, const QModelIndex &parent)
     parent must be invalid, since this model does not support
     parent-child relations.
 
-    Only one row at a time can be inserted when using the
-    OnFieldChange or OnRowChange update strategies.
+    For edit strategies OnFieldChange and OnRowChange, only one row
+    may be inserted at a time and the model may not contain other
+    cached changes.
 
     The primeInsert() signal will be emitted for each new row.
     Connect to it if you want to initialize the new row with default
     values.
 
-    Returns false if the parameters are out of bounds; otherwise
-    returns true.
+    Does not submit rows, regardless of edit strategy.
 
-    Does not submit rows, regardless of edit strategy, not even OnFieldChange.
+    Returns false if the parameters are out of bounds or the row cannot be
+    inserted; otherwise returns true.
 
     \sa primeInsert(), insertRecord()
 */
@@ -1108,8 +1109,9 @@ bool QSqlTableModel::insertRows(int row, int count, const QModelIndex &parent)
     if (row < 0 || count <= 0 || row > rowCount() || parent.isValid())
         return false;
 
-    if (d->strategy != OnManualSubmit && count != 1)
-        return false;
+    if (d->strategy != OnManualSubmit)
+        if (count != 1 || isDirty())
+            return false;
 
     d->busyInsertingRows = true;
     beginInsertRows(parent, row, row + count - 1);
index bb11316..0bdcb3b 100644 (file)
@@ -590,11 +590,15 @@ void tst_QSqlTableModel::insertRowFailure()
         QCOMPARE(model.data(model.index(1, 1)).toString(), QString("blah"));
         QFAIL_SQL(model, setRecord(1, values));
         QCOMPARE(model.data(model.index(1, 1)).toString(), QString("blah"));
+        QFAIL_SQL(model, insertRow(2));
+        QCOMPARE(model.rowCount(), 2);
     } else {
         QVERIFY_SQL(model, setData(model.index(1, 1), QString("eggs")));
         QCOMPARE(model.data(model.index(1, 1)).toString(), QString("eggs"));
         QVERIFY_SQL(model, setRecord(1, values));
         QCOMPARE(model.data(model.index(1, 1)).toString(), QString("spam"));
+        QVERIFY_SQL(model, insertRow(2));
+        QCOMPARE(model.rowCount(), 3);
     }
 
     // restore empty table