Search code examples
c++model-view-controllerqt4qlistview

Remove a list of selected items in the QListView


How can I remove a list of selected items in the QListView in QT 4.6. Something like this does not work, the iterator becomes invalid:

  QModelIndexList indexes = ui.listview_files->selectionModel()->selectedIndexes();
  foreach(QModelIndex index, indexes)
  {
    model->removeRow(index.row());
  }

removeRows also not suitable, it removes N-items that follows the one given. I use QStandardItemModel to store items.


Solution

  • QModelIndexList indexes;
    while((indexes = ui.listview_files->selectionModel()->selectedIndexes()).size()) { 
        model->removeRow(indexes.first().row()); 
    }