So model is 0 or invalid. Why are you going via the selection model anyway?
Something like this:
When the data model emits modelAboutToBeReset() you want to store the current value selected in the list view.
const QModelIndex currentIndex
= ui
->clientsOnline
->currentIndex
();
// may be an invalid index m_previouslySelected
= (currentIndex.
isValid())? currentIndex
->data
(Qt
::DisplayRole): QVariant();
const QModelIndex currentIndex = ui->clientsOnline->currentIndex(); // may be an invalid index
m_previouslySelected = (currentIndex.isValid())? currentIndex->data(Qt::DisplayRole): QVariant();
To copy to clipboard, switch view to plain text mode
When the data model later emits modelReset() find and restore the list view's current item if possible:
if (!m_previouslySelected.isNull()) {
const QModelIndexList indexes = model->match(
model->index(0,0),
Qt::DisplayRole,
m_previouslySelected,
1, // first hit only
Qt::MatchExactly
);
if (index.size() > 0) { // found a match
ui->clientsOnline->setCurrentIndex(indexes.at(0));
}
}
if (!m_previouslySelected.isNull()) {
const QAbstractItemModel *model = ui->clientsOnline->model();
const QModelIndexList indexes = model->match(
model->index(0,0),
Qt::DisplayRole,
m_previouslySelected,
1, // first hit only
Qt::MatchExactly
);
if (index.size() > 0) { // found a match
ui->clientsOnline->setCurrentIndex(indexes.at(0));
}
}
To copy to clipboard, switch view to plain text mode
Bookmarks