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")
});
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);

@ -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,

Loading…
Cancel
Save