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 <honglei.zhang@nokia.com>
This commit is contained in:
Mark Brand 2012-02-29 01:51:23 +01:00 committed by Qt by Nokia
parent f6ca63f896
commit 3d7cec6577
3 changed files with 14 additions and 8 deletions

2
dist/changes-5.0.0 vendored
View 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 *

View 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);

View 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