Recursive filtering on QSortFilterProxy
-
Hi,
I have hierarchial data in my QStandardItemModel model which is applied to a QTreeView. As for the hierarchy , its depth level is 3 :
first_level_node second_level_node third_level_node
I want to apply QSortFilterProxyModel to it : If the filter does not match first_level_node's text but if it matches second_level_node or third_level_node , also that first_level_node should stay visible.
How can I achieve that ?
I made an attempt by calling setRecursiveFilteringEnabled , however that make entire rows invisible.
QObject::connect(m_filtering_line_edit, &QLineEdit::textChanged, m_proxy_model, [&](const QString& text) { if (text.isEmpty()) { m_treeview->setModel(m_model); m_treeview->collapseAll(); } else { QRegularExpression filter("^" + QRegularExpression::escape(text), QRegularExpression::CaseInsensitiveOption); m_proxy_model->setFilterRegularExpression(filter); m_proxy_model->setFilterKeyColumn(0); m_proxy_model->setRecursiveFilteringEnabled(true); m_treeview->setModel(m_proxy_model); } });
Many thanks
-
This property holds whether the filter to be applied recursively on children, and for any matching child, its parents will be visible as well.
and I have found that works as documented. If a child matches it is made visible and that means its parents are also visible. So what does your "make entire rows invisible." mean?
-
@JonB Sorry, I should have clarified. In my data model , there is only a single column , therefore I was referring to all model items as "rows". In other words, when I apply the filter with the regexp in the code, even when I enter a valid text , nothing is visible after filter processing . If the model data is :
aa bb
And when I type b ( QString& text in the lambda expression ) , I am expecting to see :
aa bb
Therefore I have asked the question that way assuming my expectation is correct.