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:

Qt Code:
  1. void IMClientGui::setActiveUsers(QSqlQueryModel *sqm) {
  2. QString currentUser = this->getSelectedHost();
  3. ui->clientsOnline->setModel(sqm);
  4. //here the old selection should be reset if this entry is still in the list
  5. }
  6.  
  7. QString IMClientGui::getSelectedHost() {
  8. auto itemselectmodel = ui->clientsOnline->selectionModel();
  9.  
  10. if (itemselectmodel->selectedIndexes().size() > 0) {
  11. QModelIndex index = itemselectmodel->selectedIndexes().at(0);
  12. return index.data().toString();
  13. } else
  14. return "";
  15. }
To copy to clipboard, switch view to plain text mode 

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