From 555aa4439b3a4a2c5fd12b6ac155b87fe69e749f Mon Sep 17 00:00:00 2001 From: Mark Brand Date: Fri, 10 Feb 2012 00:47:33 +0100 Subject: [PATCH] QSqlTableModel::commitAll(): replace row removal hack The purpose of the hack was to fool QSqlQueryModel into signaling the removal of extra rows via rowsRemoved(). The extra rows are the inserted rows generated by QSqlTableModel. While it is important to signal the removal of all the rows before requerying after committing changes, there is a cleaner way. The table model should remove its rows before the query model removes its rows. Iterating backwards avoids having to decrement row numbers above ones being removed. Expected test results have been adjusted for these changes. Change-Id: I0e8aa81f5e7b8fea5922f5ffd1cfb4a932313a10 Reviewed-by: Yunqiao Yin --- src/sql/models/qsqltablemodel.cpp | 14 +++++++++++++- .../auto/sql/models/qsqltablemodel/tst_qsqltablemodel.cpp | 10 +++++++--- 2 files changed, 20 insertions(+), 4 deletions(-) diff --git a/src/sql/models/qsqltablemodel.cpp b/src/sql/models/qsqltablemodel.cpp index 51c7170..cbb0496 100644 --- a/src/sql/models/qsqltablemodel.cpp +++ b/src/sql/models/qsqltablemodel.cpp @@ -669,7 +669,6 @@ bool QSqlTableModel::submitAll() case QSqlTableModelPrivate::Insert: if (!insertRowIntoTable(it.value().rec())) return false; - d->bottom = d->bottom.sibling(d->bottom.row() + 1, d->bottom.column()); break; case QSqlTableModelPrivate::Update: if (!updateRowInTable(it.key(), it.value().rec())) @@ -684,6 +683,19 @@ bool QSqlTableModel::submitAll() break; } } + + // all changes have been committed + + // clean up inserted rows + QSqlTableModelPrivate::CacheMap::Iterator it = d->cache.end(); + while (it != d->cache.constBegin()) { + --it; + if (it.value().op() == QSqlTableModelPrivate::Insert) { + beginRemoveRows(QModelIndex(), it.key(), it.key()); + it = d->cache.erase(it); + endRemoveRows(); + } + } d->clearCache(); return select(); } diff --git a/tests/auto/sql/models/qsqltablemodel/tst_qsqltablemodel.cpp b/tests/auto/sql/models/qsqltablemodel/tst_qsqltablemodel.cpp index c0bdfae..270de82 100644 --- a/tests/auto/sql/models/qsqltablemodel/tst_qsqltablemodel.cpp +++ b/tests/auto/sql/models/qsqltablemodel/tst_qsqltablemodel.cpp @@ -1199,9 +1199,13 @@ void tst_QSqlTableModel::insertRecordsInLoop() model.submitAll(); // submitAll() calls select() which clears and repopulates the table int firstRowIndex = 0, lastRowIndex = 12; - QCOMPARE(spyRowsRemoved.count(), 1); - QCOMPARE(spyRowsRemoved.at(0).at(1).toInt(), firstRowIndex); - QCOMPARE(spyRowsRemoved.at(0).at(2).toInt(), lastRowIndex); + QCOMPARE(spyRowsRemoved.count(), 11); + // QSqlTableModel emits 10 signals for its 10 inserted rows + QCOMPARE(spyRowsRemoved.at(0).at(1).toInt(), lastRowIndex); + QCOMPARE(spyRowsRemoved.at(9).at(1).toInt(), firstRowIndex + 3); + // QSqlQueryModel emits 1 signal for its 3 rows + QCOMPARE(spyRowsRemoved.at(10).at(1).toInt(), firstRowIndex); + QCOMPARE(spyRowsRemoved.at(10).at(2).toInt(), firstRowIndex + 2); QCOMPARE(spyRowsInserted.at(10).at(1).toInt(), firstRowIndex); QCOMPARE(spyRowsInserted.at(10).at(2).toInt(), lastRowIndex); -- 2.7.4