【问题标题】:How to, inside a QTreeView, delete a selected row?如何在 QTreeView 中删除选定的行?
【发布时间】:2021-02-08 12:52:02
【问题描述】:

我尝试使用事件删除 QTreeView 的选定行。 这是函数:

bool TargetEventFilter::deleteselect(bool delete)
{
    auto select= examplemodel->selectionModel()->currentIndex();
    auto parent= select.parent();
    int row= select.row();
    bool remove = examplemodel->model()->removeRow(row, parent);
    return remove ;
}

所以我的问题是: 该事件正在运行(使用调试器尝试)。Parent 和 indext 总是正确的。但问题是所选行不会被删除。在“return remove”中,我得到一个“false”。

【问题讨论】:

  • 你怎么知道examplemodel->selectionModel()->currentIndex()是有效的?你没有检查它是否有效,所以你不知道:) 这个想法是,任何时候你使用一个可能返回具有 invalid value 含义的值的函数,你必须检查它。在 API 中返回无效值是否是一个理想的设计决策当然值得商榷——在现代代码中,人们会使用 std::optional 或类似的构造,但是 Qt API 的这一部分已有十多年的历史,他们决定反对它。跨度>

标签: c++ qt qtreeview


【解决方案1】:

你为什么不使用CurrentIndex

 bool remove = examplemodel->model()->removeRow(row, select);

【讨论】:

  • 你是什么意思?我已经在上面使用了它,然后将选择分为行和父项?
  • 你的 QTreeView 找不到选定的索引。你可以使用“QItemSelectionModel::currentIndex()”而不是“QModelIndex::parent()”来查找“QModelIndex”
猜你喜欢
  • 1970-01-01
  • 2012-09-08
  • 2019-09-22
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-05-23
相关资源
最近更新 更多