什么是删除QStandardItemModel的多个索引的正确方法? [英] What's the right way to remove multiple indices for a QStandardItemModel?

查看:464
本文介绍了什么是删除QStandardItemModel的多个索引的正确方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图删除QTableView的所有选定索引,

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

现在我使用:

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

一个明显的问题是,一旦您删除了该行,行ID就会失效,

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.

推荐答案

QPersistanceModelIndex类保留索引的有效状态.我尝试过,而且似乎可以正常工作.

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());

我希望这会有所帮助.

这篇关于什么是删除QStandardItemModel的多个索引的正确方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆