Reset KSelectionProxyModel state when needed

When the model of the selectionModel is changed, it is necessary to
reset the proxy and clear any selection state.

This amends commit v5.9.0~5 (KSPM: Connect properly to the source model if it
is changed., 2015-03-12)
wilder
Stephen Kelly 10 years ago
parent a0e309b403
commit e92e367b8f
  1. 6
      autotests/kselectionproxymodeltest.cpp
  2. 5
      src/kselectionproxymodel.cpp

@ -134,7 +134,13 @@ void KSelectionProxyModelTest::selectionModelModelChange()
QStringLiteral("Tomorrow") QStringLiteral("Tomorrow")
}); });
QSignalSpy resetSpy(&proxy, &QAbstractItemModel::modelReset);
selectionModel.setModel(&strings2); selectionModel.setModel(&strings2);
QCOMPARE(resetSpy.size(), 1);
QCOMPARE(proxy.rowCount(), 0);
proxy.setSourceModel(&strings2); proxy.setSourceModel(&strings2);
selectionModel.select(strings2.index(0, 0), QItemSelectionModel::Select); selectionModel.select(strings2.index(0, 0), QItemSelectionModel::Select);

@ -2466,6 +2466,7 @@ void KSelectionProxyModel::setSelectionModel(QItemSelectionModel *itemSelectionM
SLOT(selectionChanged(QItemSelection,QItemSelection))); SLOT(selectionChanged(QItemSelection,QItemSelection)));
auto handleSelectionModelModel = [ &, d] { auto handleSelectionModelModel = [ &, d] {
beginResetModel();
if (d->selectionModelModelAboutToBeResetConnection) if (d->selectionModelModelAboutToBeResetConnection)
{ {
disconnect(d->selectionModelModelAboutToBeResetConnection); disconnect(d->selectionModelModelAboutToBeResetConnection);
@ -2482,7 +2483,11 @@ void KSelectionProxyModel::setSelectionModel(QItemSelectionModel *itemSelectionM
d->selectionModelModelResetConnection = connect( d->selectionModelModelResetConnection = connect(
d->m_selectionModel->model(), d->m_selectionModel->model(),
SIGNAL(modelReset()), this, SLOT(sourceModelReset())); 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) #if QT_VERSION >= QT_VERSION_CHECK(5, 5, 0)
connect(d->m_selectionModel.data(), &QItemSelectionModel::modelChanged, connect(d->m_selectionModel.data(), &QItemSelectionModel::modelChanged,

Loading…
Cancel
Save