diff --git a/src/corelib/itemmodels/qsortfilterproxymodel.cpp b/src/corelib/itemmodels/qsortfilterproxymodel.cpp index f748a95160..fb09c22798 100644 --- a/src/corelib/itemmodels/qsortfilterproxymodel.cpp +++ b/src/corelib/itemmodels/qsortfilterproxymodel.cpp @@ -1479,6 +1479,7 @@ void QSortFilterProxyModelPrivate::_q_sourceReset() _q_clearMapping(); // All internal structures are deleted in clear() q->endResetModel(); + create_mapping(QModelIndex()); update_source_sort_column(); if (dynamic_sortfilter && update_source_sort_column()) sort(); @@ -2042,6 +2043,7 @@ void QSortFilterProxyModel::setSourceModel(QAbstractItemModel *sourceModel) connect(d->model, SIGNAL(modelReset()), this, SLOT(_q_sourceReset())); endResetModel(); + d->create_mapping(QModelIndex()); if (d->update_source_sort_column() && d->dynamic_sortfilter) d->sort(); } diff --git a/tests/auto/corelib/itemmodels/qsortfilterproxymodel_common/tst_qsortfilterproxymodel.cpp b/tests/auto/corelib/itemmodels/qsortfilterproxymodel_common/tst_qsortfilterproxymodel.cpp index 20fc5d492d..44da534246 100644 --- a/tests/auto/corelib/itemmodels/qsortfilterproxymodel_common/tst_qsortfilterproxymodel.cpp +++ b/tests/auto/corelib/itemmodels/qsortfilterproxymodel_common/tst_qsortfilterproxymodel.cpp @@ -2163,6 +2163,35 @@ void tst_QSortFilterProxyModel::changeSourceDataForwardsRoles_qtbug35440() QCOMPARE(spy.at(1).at(2).value >(), expectedChangedRoles); } +void tst_QSortFilterProxyModel::changeSourceDataProxySendDataChanged_qtbug87781() +{ + QStandardItemModel baseModel; + QSortFilterProxyModel proxyModelBefore; + QSortFilterProxyModel proxyModelAfter; + + QSignalSpy baseDataChangedSpy(&baseModel, &QStandardItemModel::dataChanged); + QSignalSpy beforeDataChangedSpy(&proxyModelBefore, &QSortFilterProxyModel::dataChanged); + QSignalSpy afterDataChangedSpy(&proxyModelAfter, &QSortFilterProxyModel::dataChanged); + + QVERIFY(baseDataChangedSpy.isValid()); + QVERIFY(beforeDataChangedSpy.isValid()); + QVERIFY(afterDataChangedSpy.isValid()); + + proxyModelBefore.setSourceModel(&baseModel); + baseModel.insertRows(0, 1); + baseModel.insertColumns(0, 1); + proxyModelAfter.setSourceModel(&baseModel); + + QCOMPARE(baseDataChangedSpy.size(), 0); + QCOMPARE(beforeDataChangedSpy.size(), 0); + QCOMPARE(afterDataChangedSpy.size(), 0); + + baseModel.setData(baseModel.index(0, 0), QStringLiteral("new data"), Qt::DisplayRole); + QCOMPARE(baseDataChangedSpy.size(), 1); + QCOMPARE(beforeDataChangedSpy.size(), 1); + QCOMPARE(afterDataChangedSpy.size(), 1); +} + void tst_QSortFilterProxyModel::sortFilterRole() { QStandardItemModel model; diff --git a/tests/auto/corelib/itemmodels/qsortfilterproxymodel_common/tst_qsortfilterproxymodel.h b/tests/auto/corelib/itemmodels/qsortfilterproxymodel_common/tst_qsortfilterproxymodel.h index 68c00318fe..b0f4137082 100644 --- a/tests/auto/corelib/itemmodels/qsortfilterproxymodel_common/tst_qsortfilterproxymodel.h +++ b/tests/auto/corelib/itemmodels/qsortfilterproxymodel_common/tst_qsortfilterproxymodel.h @@ -90,6 +90,7 @@ private slots: void changeSourceData(); void changeSourceDataKeepsStableSorting_qtbug1548(); void changeSourceDataForwardsRoles_qtbug35440(); + void changeSourceDataProxySendDataChanged_qtbug87781(); void resortingDoesNotBreakTreeModels(); void dynamicFilterWithoutSort(); void sortFilterRole();