Cerberus
15th October 2015, 15:47
I have a QListView which contents I said with a Model. When resetting the model I need to reselect the previous item but I'm not sure how to search for it and than select it.
This is how I set the model:
void IMClientGui::setActiveUsers(QSqlQueryModel *sqm) {
QString currentUser = this->getSelectedHost();
ui->clientsOnline->setModel(sqm);
//here the old selection should be reset if this entry is still in the list
}
QString IMClientGui::getSelectedHost() {
auto itemselectmodel = ui->clientsOnline->selectionModel();
if (itemselectmodel->selectedIndexes().size() > 0) {
QModelIndex index = itemselectmodel->selectedIndexes().at(0);
return index.data().toString();
} else
return "";
}
I cannot work with the indexes here (e.g. if item with index 3 was selected reset to index 3) since a previous item might have been removed and therefore the index could be less or more if an item was added
This is how I set the model:
void IMClientGui::setActiveUsers(QSqlQueryModel *sqm) {
QString currentUser = this->getSelectedHost();
ui->clientsOnline->setModel(sqm);
//here the old selection should be reset if this entry is still in the list
}
QString IMClientGui::getSelectedHost() {
auto itemselectmodel = ui->clientsOnline->selectionModel();
if (itemselectmodel->selectedIndexes().size() > 0) {
QModelIndex index = itemselectmodel->selectedIndexes().at(0);
return index.data().toString();
} else
return "";
}
I cannot work with the indexes here (e.g. if item with index 3 was selected reset to index 3) since a previous item might have been removed and therefore the index could be less or more if an item was added