Reputation:
What is the correct way to filter elements in a QTreeView, so that only a few are displayed according to a criterion.
It has a custom model with a proxy for ordering, and I use QSortFilterProxyModel::filterAcceptsRow(...) With a tree-element structure I can not filter parent elements, so the lower-level children are hidden.
I use something close to this:
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
}
...
}
With
QSortFilterProxyModel :: filterAcceptsRow (int source_row, ...)
{
return sourceModel()->data(source_row).isOutdated ();
}
Is some recursion required, or does the filtering function work row by row?
Upvotes: 1
Views: 546
Reputation:
As commented by Simon, he navigates line by line. The solution I'm working on is an older version of Qt 5.6.2. In it I could observe that the filter is line by line, in the case of my tree, if the filter passes in the parent and returns false it does not display any of the nested lines.
return sourceModel()->data(source_row, source_parent);
Upvotes: 0