views:

55

answers:

1

Hi. 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.

+1  A: 
QModelIndexList indexes;
while((indexes = ui.listview_files->selectionModel()->selectedIndexes()).size()) { 
    model->removeRow(indexes.first().row()); 
}
sje397
Oh, how simple it all)But not always remove. Here's the solution:QModelIndexList indexes = ui.listview_files->selectionModel()->selectedIndexes(); while(indexes.size()) { model->removeRow(indexes.first().row()); indexes = ui.listview_files->selectionModel()->selectedIndexes();
Leviathan
Edited to take your comment into account ;)
sje397