Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What's the right way to remove multiple indices for a QStandardItemModel?

Tags:

model

qt

qt4

I was trying to remove all selected indices of a QTableView,

Now I use:

foreach (const QModelIndex & idx, model->selectionModel()->selectedIndexes())
{
    model->removeRow (idx.row()); // Obviously bug
}

There's a obvious problem that once you remove the row, the row id is invalidated, w

As there's no function that takes directly the index (or does the index act like a iterator that will get invalidated when data changed?), I don't know what to do.

like image 967
daisy Avatar asked Jan 16 '23 06:01

daisy


1 Answers

There is QPersistanceModelIndex class which keeps valid state of index. I tried and it seems to be working.

QList<QPersistentModelIndex> indexes;

foreach (const QModelIndex &i, ui->tableView->selectionModel()->selectedIndexes())
    indexes << i;

foreach (const QPersistentModelIndex &i, indexes)
    ui->tableView->model()->removeRow(i.row());

I hope it will help.

like image 165
fasked Avatar answered Apr 29 '23 14:04

fasked