删除 QListView 中选定项目的列表

发布于 2024-09-11 09:39:26 字数 331 浏览 3 评论 0原文

如何删除 QT 4.6 中 QListView 中选定项目的列表。 像这样的东西不起作用,迭代器变得无效:

  QModelIndexList indexes = ui.listview_files->selectionModel()->selectedIndexes();
  foreach(QModelIndex index, indexes)
  {
    model->removeRow(index.row());
  }

removeRows 也不合适,它会删除给定项后面的 N 项。 我使用 QStandardItemModel 来存储项目。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

二智少女 2024-09-18 09:39:26
QModelIndexList indexes;
while((indexes = ui.listview_files->selectionModel()->selectedIndexes()).size()) { 
    model->removeRow(indexes.first().row()); 
}
QModelIndexList indexes;
while((indexes = ui.listview_files->selectionModel()->selectedIndexes()).size()) { 
    model->removeRow(indexes.first().row()); 
}
坐在坟头思考人生 2024-09-18 09:39:26

我不知道这是否是 Qt 4.8 新版本中的错误,但 sje397 解决方案对我不起作用(在 QTreeView 上)。

我分享我发现的最佳解决方案,即按降序对索引进行排序,并从尾到头删除行。

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

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());
}
岁月静好 2024-09-18 09:39:26

在这里,我在 2016 年挖掘了你的问题...

你原来的解决方案的问题是它使索引无效,即如果你想删除索引为 5、6 和 7 的元素,那么在删除第五个项目(现在的项目编号为 6)之后变为项目编号 5,依此类推。

为了使您的解决方案发挥作用,无需每次在循环中都评估 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());
}

希望这能帮助一些随机的谷歌用户。

Here I've excavated your question in 2016...

The problem with your original solution is that it invalidates indices, i.e. if you want to remove elements with indices 5, 6, and 7, after removing the fifth item, item number six now becomes item number five and so on.

To make your solution work, there's no need to evaluate selectionModel()->selectedIndexes() everytime in your loop. The trick is to start from the end and iterate back to the beginning. If you remove the item number 7 first, items with numbers 5 and 6 will keep their positions.

To give you folks some code:

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

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

Hope this will help some random googler.

好菇凉咱不稀罕他 2024-09-18 09:39:26

通过多行删除更加优化:

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

removing by multiple rows is more optimized:

QVector<QItemSelectionRange> ranges = ui.listView->selectionModel()->selection().toVector();
foreach (const QItemSelectionRange& range, ranges) {
    ui.listView->model()->removeRows(range.top(), range.height());
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文