gpt4 book ai didi

c++ - 使用 filterAcceptsRow 过滤 QTreeView 中的元素

转载 作者:太空宇宙 更新时间:2023-11-04 12:46:02 25 4
gpt4 key购买 nike

在 QTreeView 中过滤元素的正确方法是什么,以便根据标准只显示少数元素。

它有一个自定义 model使用代理进行订购,我使用 QSortFilterProxyModel::filterAcceptsRow(...)使用树元素结构,我无法过滤父元素,因此隐藏了较低级别的子元素。

我用的东西接近这个:

class INode {
public:
...
virtual bool isOutdated () const = 0;

virtual bool AllChildrenIsOudated () = 0;
INodeParent
...
}

class RootNode: public INode {
...
bool isOutdated() const override {
return false; // always show
}

bool AllChildrenIsOudated () = 0;
...
}

class GroupNode: public RootNode {
...
bool isOutdate() const override {
return AllChildrenIsOudated ();
}
...
}

class ElementNode: public GroupNode {
...
bool isOutdated() const override {
return outdate; // set by a setData (true, Qt :: UserRole)
}
...
}

class DetailNode: public ElementNode {
...
bool isOutdated () const override {
return parent->isOutdated(); // ask parrent
}
...
}

QSortFilterProxyModel :: filterAcceptsRow (int source_row, ...)
{
return sourceModel()->data(source_row).isOutdated ();
}

是否需要一些递归,或者过滤功能是否逐行工作?

最佳答案

正如 Simon 所评论的那样,他逐行导航。我正在处理的解决方案是旧版本的 Qt 5.6.2。在其中我可以观察到过滤器是逐行显示的,就我的树而言,如果过滤器传入父级并返回 false,则它不会显示任何嵌套行。

return sourceModel()->data(source_row, source_parent);

关于c++ - 使用 filterAcceptsRow 过滤 QTreeView 中的元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51529574/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com