diff options
author | Olivier Goffart <ogoffart@trolltech.com> | 2009-03-24 13:43:21 (GMT) |
---|---|---|
committer | Olivier Goffart <ogoffart@trolltech.com> | 2009-03-24 13:47:16 (GMT) |
commit | dd7b1f11fa4b682d4283274d1fcba8a801fa10f1 (patch) | |
tree | 8f0ba802b1cbb308e57c8a6bc23f7dad06243c0b | |
parent | 680bed3dfd50ee888874b5d322899243e0143ff6 (diff) | |
download | Qt-dd7b1f11fa4b682d4283274d1fcba8a801fa10f1.zip Qt-dd7b1f11fa4b682d4283274d1fcba8a801fa10f1.tar.gz Qt-dd7b1f11fa4b682d4283274d1fcba8a801fa10f1.tar.bz2 |
Make sure the sorting is updated when dynamic sorting is enabled and the model is reset
Task-number: 248868 (part 2)
Reviewed-by: Thierry
-rw-r--r-- | src/gui/itemviews/qsortfilterproxymodel.cpp | 5 | ||||
-rw-r--r-- | tests/auto/qsortfilterproxymodel/tst_qsortfilterproxymodel.cpp | 52 |
2 files changed, 57 insertions, 0 deletions
diff --git a/src/gui/itemviews/qsortfilterproxymodel.cpp b/src/gui/itemviews/qsortfilterproxymodel.cpp index b6fb598..10bcb9a 100644 --- a/src/gui/itemviews/qsortfilterproxymodel.cpp +++ b/src/gui/itemviews/qsortfilterproxymodel.cpp @@ -1103,6 +1103,8 @@ void QSortFilterProxyModelPrivate::_q_sourceReset() // All internal structures are deleted in clear() q->reset(); update_source_sort_column(); + if (dynamic_sortfilter) + sort(); } void QSortFilterProxyModelPrivate::_q_sourceLayoutAboutToBeChanged() @@ -1495,6 +1497,7 @@ void QSortFilterProxyModel::setSourceModel(QAbstractItemModel *sourceModel) d->clear_mapping(); reset(); + d->update_source_sort_column(); } /*! @@ -2107,6 +2110,8 @@ void QSortFilterProxyModel::setDynamicSortFilter(bool enable) { Q_D(QSortFilterProxyModel); d->dynamic_sortfilter = enable; + if (enable) + d->sort(); } /*! diff --git a/tests/auto/qsortfilterproxymodel/tst_qsortfilterproxymodel.cpp b/tests/auto/qsortfilterproxymodel/tst_qsortfilterproxymodel.cpp index 2f44239..0a48066 100644 --- a/tests/auto/qsortfilterproxymodel/tst_qsortfilterproxymodel.cpp +++ b/tests/auto/qsortfilterproxymodel/tst_qsortfilterproxymodel.cpp @@ -130,6 +130,7 @@ private slots: void task236755_hiddenColumns(); void task247867_insertRowsSort(); void task248868_staticSorting(); + void task248868_dynamicSorting(); protected: void buildHierarchy(const QStringList &data, QAbstractItemModel *model); @@ -2539,5 +2540,56 @@ void tst_QSortFilterProxyModel::task248868_staticSorting() } +void tst_QSortFilterProxyModel::task248868_dynamicSorting() +{ + QStringListModel model1; + const QStringList initial = QString("bateau avion dragon hirondelle flamme camion elephant").split(" "); + model1.setStringList(initial); + QSortFilterProxyModel proxy1; + proxy1.sort(0); + proxy1.setSourceModel(&model1); + + QCOMPARE(proxy1.columnCount(QModelIndex()), 1); + //the model should not be sorted because sorting has not been set to dynamic yet. + QCOMPARE(proxy1.rowCount(QModelIndex()), initial.count()); + for (int row = 0; row < proxy1.rowCount(QModelIndex()); ++row) { + QModelIndex index = proxy1.index(row, 0, QModelIndex()); + QCOMPARE(proxy1.data(index, Qt::DisplayRole).toString(), initial.at(row)); + } + + proxy1.setDynamicSortFilter(true); + + //now the model should be sorted. + QStringList expected = initial; + expected.sort(); + for (int row = 0; row < proxy1.rowCount(QModelIndex()); ++row) { + QModelIndex index = proxy1.index(row, 0, QModelIndex()); + QCOMPARE(proxy1.data(index, Qt::DisplayRole).toString(), expected.at(row)); + } + + QStringList initial2 = initial; + initial2.replaceInStrings("bateau", "girafe"); + model1.setStringList(initial2); //this will cause a reset + + QStringList expected2 = initial2; + expected2.sort(); + + //now the model should still be sorted. + for (int row = 0; row < proxy1.rowCount(QModelIndex()); ++row) { + QModelIndex index = proxy1.index(row, 0, QModelIndex()); + QCOMPARE(proxy1.data(index, Qt::DisplayRole).toString(), expected2.at(row)); + } + + QStringListModel model2(initial); + proxy1.setSourceModel(&model2); + + //the model should again be sorted + for (int row = 0; row < proxy1.rowCount(QModelIndex()); ++row) { + QModelIndex index = proxy1.index(row, 0, QModelIndex()); + QCOMPARE(proxy1.data(index, Qt::DisplayRole).toString(), expected.at(row)); + } +} + + QTEST_MAIN(tst_QSortFilterProxyModel) #include "tst_qsortfilterproxymodel.moc" |