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.
QModelIndexList indexes;
while((indexes = ui.listview_files->selectionModel()->selectedIndexes()).size()) {
model->removeRow(indexes.first().row());
}
I don't know if it's a bug in new versions of Qt 4.8 but sje397 solution doesn't work for me (on a QTreeView).
I share the best solution i found which is to sort indexes in descending order and remove row from end to begin.
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());
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With