Delete list of selected items in QListView

How to remove the list of selected items in 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 does not fit, it removes N-elements that follow the data. I am using QStandardItemModel to store items.

+5
source share
4 answers
QModelIndexList indexes;
while((indexes = ui.listview_files->selectionModel()->selectedIndexes()).size()) { 
    model->removeRow(indexes.first().row()); 
}
+6
source

I do not know if the error is in new versions of Qt 4.8, but the sje397 solution does not work for me (in QTreeView).

I am using the best solution I have found to sort indexes in descending order and delete a row from the end to start.

QModelIndexList indexes = pTreeview->selectionModel()->selectedIndexes();
qSort(indexes.begin(), indexes.end(), qGreater<QModelIndex>());

for(iter = indexes.constBegin(); iter != indexes.constEnd(); ++iter){
   pModels->removeRow((*iter).row(), (*iter).parent());
}
+2
source

2016 ...

, , .. 5, 6 7, 6 ..

, selectionModel()->selectedIndexes() . , . 7, 5 6 .

- :

QModelIndexList selectedIndexes(listView->selectionModel()->selectedIndexes());

for (QModelIndexList::const_iterator it = selectedIndexes.constEnd() - 1;
        it >= selectedIndexes.constBegin(); --it) {
    model->removeRow(it->row());
}

, .

+2

:

QVector<QItemSelectionRange> ranges = ui.listView->selectionModel()->selection().toVector();
foreach (const QItemSelectionRange& range, ranges)
{
    ui.listView->model()->removeRows(range.top(), range.height());
}
+1

All Articles