diff options
author | Mark Brand <mabrand@mabrand.nl> | 2012-04-23 01:06:17 +0200 |
---|---|---|
committer | Qt by Nokia <qt-info@nokia.com> | 2012-04-23 19:20:19 +0200 |
commit | 83c9ebbd6692cde99ee692e6549c591100f12545 (patch) | |
tree | bc367b1ea99f3a8b679ce338b59b5f95f006585b /tests/auto | |
parent | 508a90302b5bd2a1b228c62d1a1b24a3e66d24a9 (diff) |
QSqlQueryModel::setQuery() don't use deprecated reset()
Previously the method attempted to reset only as a last resort.
Now reset() is deprecated and resetting must happen between emitting
modelAboutToBeReset() and modelReset(). Since this suffices in all
cases to notify views that they must reinterrogate the model, it is no
longer necessary to signal explicitly row removals and insertions
within the scope of the reset.
Additionally, fetchMore() is now called within the scope of the reset
so insert signals do not have to be emitted here either.
This improved handling of resetting in QSqlQueryModel also allows the
cache in QSqlTableModel to be cleared directly at select().
This change may actually allow views to operate more efficiently since
they no longer have to react to separate row removal and insert
signals. Views can avoid pointless deallocation and reallocation
by considering row count only after the reset is finished. The cost is
that the columns and horizontal headers must be considered in the view
at each setQuery() call. In any case, it is not clear that trying to
be smart about this in the model justifies additional complexity.
Tests had to be adjusted where they expected explicit row removal
and insert signals.
Change-Id: I4f7eac1419824361d7d9bdcc6a87092b33e80d7a
Task-Id: QTBUG-25419
Reviewed-by: Andy Shaw <andy.shaw@digia.com>
Reviewed-by: Olivier Goffart <ogoffart@woboq.com>
Reviewed-by: Honglei Zhang <honglei.zhang@nokia.com>
Diffstat (limited to 'tests/auto')
-rw-r--r-- | tests/auto/sql/models/qsqlquerymodel/tst_qsqlquerymodel.cpp | 56 | ||||
-rw-r--r-- | tests/auto/sql/models/qsqltablemodel/tst_qsqltablemodel.cpp | 52 |
2 files changed, 34 insertions, 74 deletions
diff --git a/tests/auto/sql/models/qsqlquerymodel/tst_qsqlquerymodel.cpp b/tests/auto/sql/models/qsqlquerymodel/tst_qsqlquerymodel.cpp index 33793c013d..1c1319630e 100644 --- a/tests/auto/sql/models/qsqlquerymodel/tst_qsqlquerymodel.cpp +++ b/tests/auto/sql/models/qsqlquerymodel/tst_qsqlquerymodel.cpp @@ -478,22 +478,23 @@ void tst_QSqlQueryModel::fetchMore() CHECK_DATABASE(db); QSqlQueryModel model; - QSignalSpy spy(&model, SIGNAL(rowsInserted(QModelIndex, int, int))); + QSignalSpy modelAboutToBeResetSpy(&model, SIGNAL(modelAboutToBeReset())); + QSignalSpy modelResetSpy(&model, SIGNAL(modelReset())); model.setQuery(QSqlQuery("select * from " + qTableName("many", __FILE__), db)); int rowCount = model.rowCount(); - QCOMPARE(spy.value(0).value(1).toInt(), 0); - QCOMPARE(spy.value(0).value(2).toInt(), rowCount - 1); + QCOMPARE(modelAboutToBeResetSpy.count(), 1); + QCOMPARE(modelResetSpy.count(), 1); // If the driver doesn't return the query size fetchMore() causes the // model to grow and new signals are emitted + QSignalSpy rowsInsertedSpy(&model, SIGNAL(rowsInserted(QModelIndex, int, int))); if (!db.driver()->hasFeature(QSqlDriver::QuerySize)) { - spy.clear(); model.fetchMore(); int newRowCount = model.rowCount(); - QCOMPARE(spy.value(0).value(1).toInt(), rowCount); - QCOMPARE(spy.value(0).value(2).toInt(), newRowCount - 1); + QCOMPARE(rowsInsertedSpy.value(0).value(1).toInt(), rowCount); + QCOMPARE(rowsInsertedSpy.value(0).value(2).toInt(), newRowCount - 1); } } @@ -519,7 +520,8 @@ void tst_QSqlQueryModel::withSortFilterProxyModel() QTableView view; view.setModel(&proxy); - QSignalSpy modelRowsRemovedSpy(&model, SIGNAL(rowsRemoved(const QModelIndex &, int, int))); + QSignalSpy modelAboutToBeResetSpy(&model, SIGNAL(modelAboutToBeReset())); + QSignalSpy modelResetSpy(&model, SIGNAL(modelReset())); QSignalSpy modelRowsInsertedSpy(&model, SIGNAL(rowsInserted(const QModelIndex &, int, int))); model.setQuery(QSqlQuery("SELECT * FROM " + qTableName("test3", __FILE__), db)); view.scrollToBottom(); @@ -528,19 +530,14 @@ void tst_QSqlQueryModel::withSortFilterProxyModel() QCOMPARE(proxy.rowCount(), 511); - // The second call to setQuery() clears the model by removing it's rows. - // Only 256 rows because that is what was cached. - QCOMPARE(modelRowsRemovedSpy.count(), 1); - QCOMPARE(modelRowsRemovedSpy.value(0).value(1).toInt(), 0); - QCOMPARE(modelRowsRemovedSpy.value(0).value(2).toInt(), 255); - - // The call to scrollToBottom() forces the model to fetch all rows, - // which will be done in two steps. - QCOMPARE(modelRowsInsertedSpy.count(), 2); - QCOMPARE(modelRowsInsertedSpy.value(0).value(1).toInt(), 0); - QCOMPARE(modelRowsInsertedSpy.value(0).value(2).toInt(), 255); - QCOMPARE(modelRowsInsertedSpy.value(1).value(1).toInt(), 256); - QCOMPARE(modelRowsInsertedSpy.value(1).value(2).toInt(), 510); + // setQuery() resets the model accompanied by begin and end signals + QCOMPARE(modelAboutToBeResetSpy.count(), 1); + QCOMPARE(modelResetSpy.count(), 1); + + // The call to scrollToBottom() forces the model to fetch additional rows. + QCOMPARE(modelRowsInsertedSpy.count(), 1); + QCOMPARE(modelRowsInsertedSpy.value(0).value(1).toInt(), 256); + QCOMPARE(modelRowsInsertedSpy.value(0).value(2).toInt(), 510); } // For task 155402: When the model is already empty when setQuery() is called @@ -553,22 +550,19 @@ void tst_QSqlQueryModel::setQuerySignalEmission() CHECK_DATABASE(db); QSqlQueryModel model; - QSignalSpy modelRowsAboutToBeRemovedSpy(&model, SIGNAL(rowsAboutToBeRemoved(const QModelIndex &, int, int))); - QSignalSpy modelRowsRemovedSpy(&model, SIGNAL(rowsRemoved(const QModelIndex &, int, int))); + QSignalSpy modelAboutToBeResetSpy(&model, SIGNAL(modelAboutToBeReset())); + QSignalSpy modelResetSpy(&model, SIGNAL(modelReset())); - // First select, the model was empty and no rows had to be removed! + // First select, the model was empty and no rows had to be removed, but model resets anyway. model.setQuery(QSqlQuery("SELECT * FROM " + qTableName("test", __FILE__), db)); - QCOMPARE(modelRowsAboutToBeRemovedSpy.count(), 0); - QCOMPARE(modelRowsRemovedSpy.count(), 0); + QCOMPARE(modelAboutToBeResetSpy.count(), 1); + QCOMPARE(modelResetSpy.count(), 1); // Second select, the model wasn't empty and two rows had to be removed! + // setQuery() resets the model accompanied by begin and end signals model.setQuery(QSqlQuery("SELECT * FROM " + qTableName("test", __FILE__), db)); - QCOMPARE(modelRowsAboutToBeRemovedSpy.count(), 1); - QCOMPARE(modelRowsAboutToBeRemovedSpy.value(0).value(1).toInt(), 0); - QCOMPARE(modelRowsAboutToBeRemovedSpy.value(0).value(2).toInt(), 1); - QCOMPARE(modelRowsRemovedSpy.count(), 1); - QCOMPARE(modelRowsRemovedSpy.value(0).value(1).toInt(), 0); - QCOMPARE(modelRowsRemovedSpy.value(0).value(2).toInt(), 1); + QCOMPARE(modelAboutToBeResetSpy.count(), 2); + QCOMPARE(modelResetSpy.count(), 2); } // For task 170783: When the query's result set is empty no rows should be inserted, diff --git a/tests/auto/sql/models/qsqltablemodel/tst_qsqltablemodel.cpp b/tests/auto/sql/models/qsqltablemodel/tst_qsqltablemodel.cpp index 2cea8b3546..89085df6c9 100644 --- a/tests/auto/sql/models/qsqltablemodel/tst_qsqltablemodel.cpp +++ b/tests/auto/sql/models/qsqltablemodel/tst_qsqltablemodel.cpp @@ -1338,39 +1338,13 @@ void tst_QSqlTableModel::setFilter() QCOMPARE(model.rowCount(), 1); QCOMPARE(model.data(model.index(0, 0)).toInt(), 1); - QSignalSpy rowsRemovedSpy(&model, SIGNAL(rowsRemoved(QModelIndex,int,int))); - QSignalSpy rowsAboutToBeRemovedSpy(&model, - SIGNAL(rowsAboutToBeRemoved(QModelIndex,int,int))); - QSignalSpy rowsInsertedSpy(&model, SIGNAL(rowsInserted(QModelIndex,int,int))); - QSignalSpy rowsAboutToBeInsertedSpy(&model, - SIGNAL(rowsAboutToBeInserted(QModelIndex,int,int))); + QSignalSpy modelAboutToBeResetSpy(&model, SIGNAL(modelAboutToBeReset())); + QSignalSpy modelResetSpy(&model, SIGNAL(modelReset())); model.setFilter("id = 2"); // check the signals - QCOMPARE(rowsAboutToBeRemovedSpy.count(), 1); - QCOMPARE(rowsRemovedSpy.count(), 1); - QCOMPARE(rowsAboutToBeInsertedSpy.count(), 1); - QCOMPARE(rowsInsertedSpy.count(), 1); - QList<QVariant> args = rowsAboutToBeRemovedSpy.takeFirst(); - QCOMPARE(args.count(), 3); - QCOMPARE(qvariant_cast<QModelIndex>(args.at(0)), QModelIndex()); - QCOMPARE(args.at(1).toInt(), 0); - QCOMPARE(args.at(2).toInt(), 0); - args = rowsRemovedSpy.takeFirst(); - QCOMPARE(args.count(), 3); - QCOMPARE(qvariant_cast<QModelIndex>(args.at(0)), QModelIndex()); - QCOMPARE(args.at(1).toInt(), 0); - QCOMPARE(args.at(2).toInt(), 0); - args = rowsInsertedSpy.takeFirst(); - QCOMPARE(args.count(), 3); - QCOMPARE(qvariant_cast<QModelIndex>(args.at(0)), QModelIndex()); - QCOMPARE(args.at(1).toInt(), 0); - QCOMPARE(args.at(2).toInt(), 0); - args = rowsAboutToBeInsertedSpy.takeFirst(); - QCOMPARE(args.count(), 3); - QCOMPARE(qvariant_cast<QModelIndex>(args.at(0)), QModelIndex()); - QCOMPARE(args.at(1).toInt(), 0); - QCOMPARE(args.at(2).toInt(), 0); + QCOMPARE(modelAboutToBeResetSpy.count(), 1); + QCOMPARE(modelResetSpy.count(), 1); QCOMPARE(model.rowCount(), 1); QCOMPARE(model.data(model.index(0, 0)).toInt(), 2); @@ -1500,7 +1474,8 @@ void tst_QSqlTableModel::insertRecordsInLoop() record.setValue(1, "Testman"); record.setValue(2, 1); - QSignalSpy spyRowsRemoved(&model, SIGNAL(rowsRemoved(const QModelIndex &, int, int))); + QSignalSpy modelAboutToBeResetSpy(&model, SIGNAL(modelAboutToBeReset())); + QSignalSpy modelResetSpy(&model, SIGNAL(modelReset())); QSignalSpy spyRowsInserted(&model, SIGNAL(rowsInserted(const QModelIndex &, int, int))); for (int i = 0; i < 10; i++) { QVERIFY(model.insertRecord(model.rowCount(), record)); @@ -1509,18 +1484,9 @@ void tst_QSqlTableModel::insertRecordsInLoop() } model.submitAll(); // submitAll() calls select() which clears and repopulates the table - int firstRowIndex = 0, lastRowIndex = 12; - 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); - QCOMPARE(spyRowsInserted.count(), 11); + // model emits reset signals + QCOMPARE(modelAboutToBeResetSpy.count(), 1); + QCOMPARE(modelResetSpy.count(), 1); QCOMPARE(model.rowCount(), 13); QCOMPARE(model.columnCount(), 3); |