From 3d7cec6577fa32fb6036b346db57c53e38ea2ffd Mon Sep 17 00:00:00 2001 From: Mark Brand Date: Wed, 29 Feb 2012 01:51:23 +0100 Subject: [PATCH] QSqlTableModel: disallow insert if changes are pending For OnFieldChange and OnRowChange, inserting rows should not be allowed if there are pending changes in cache. Change-Id: Ia794332959a35a1de87e798ba1a74ace3dfae68f Reviewed-by: Honglei Zhang --- dist/changes-5.0.0 | 2 +- src/sql/models/qsqltablemodel.cpp | 16 +++++++++------- .../models/qsqltablemodel/tst_qsqltablemodel.cpp | 4 ++++ 3 files changed, 14 insertions(+), 8 deletions(-) diff --git a/dist/changes-5.0.0 b/dist/changes-5.0.0 index c85749f2eb..e10f0df195 100644 --- a/dist/changes-5.0.0 +++ b/dist/changes-5.0.0 @@ -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 * diff --git a/src/sql/models/qsqltablemodel.cpp b/src/sql/models/qsqltablemodel.cpp index a001b262de..0eb5447600 100644 --- a/src/sql/models/qsqltablemodel.cpp +++ b/src/sql/models/qsqltablemodel.cpp @@ -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); diff --git a/tests/auto/sql/models/qsqltablemodel/tst_qsqltablemodel.cpp b/tests/auto/sql/models/qsqltablemodel/tst_qsqltablemodel.cpp index bb1131655d..0bdcb3bce6 100644 --- a/tests/auto/sql/models/qsqltablemodel/tst_qsqltablemodel.cpp +++ b/tests/auto/sql/models/qsqltablemodel/tst_qsqltablemodel.cpp @@ -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