diff --git a/autotests/kselectionproxymodeltest.cpp b/autotests/kselectionproxymodeltest.cpp index b9f1277..a312a6c 100644 --- a/autotests/kselectionproxymodeltest.cpp +++ b/autotests/kselectionproxymodeltest.cpp @@ -134,7 +134,13 @@ void KSelectionProxyModelTest::selectionModelModelChange() QStringLiteral("Tomorrow") }); + QSignalSpy resetSpy(&proxy, &QAbstractItemModel::modelReset); + selectionModel.setModel(&strings2); + + QCOMPARE(resetSpy.size(), 1); + QCOMPARE(proxy.rowCount(), 0); + proxy.setSourceModel(&strings2); selectionModel.select(strings2.index(0, 0), QItemSelectionModel::Select); diff --git a/src/kselectionproxymodel.cpp b/src/kselectionproxymodel.cpp index 428b849..e68fcf7 100644 --- a/src/kselectionproxymodel.cpp +++ b/src/kselectionproxymodel.cpp @@ -2466,6 +2466,7 @@ void KSelectionProxyModel::setSelectionModel(QItemSelectionModel *itemSelectionM SLOT(selectionChanged(QItemSelection,QItemSelection))); auto handleSelectionModelModel = [ &, d] { + beginResetModel(); if (d->selectionModelModelAboutToBeResetConnection) { disconnect(d->selectionModelModelAboutToBeResetConnection); @@ -2482,7 +2483,11 @@ void KSelectionProxyModel::setSelectionModel(QItemSelectionModel *itemSelectionM d->selectionModelModelResetConnection = connect( d->m_selectionModel->model(), SIGNAL(modelReset()), this, SLOT(sourceModelReset())); + d->m_rootIndexList.clear(); + delete d->m_indexMapper; + d->m_indexMapper = new KModelIndexProxyMapper(sourceModel(), d->m_selectionModel->model(), this); } + endResetModel(); }; #if QT_VERSION >= QT_VERSION_CHECK(5, 5, 0) connect(d->m_selectionModel.data(), &QItemSelectionModel::modelChanged,