View Full Version : Hide parents in QSortFilterProxyModel if they don't match

21st October 2014, 14:35
Hi. i have a treeview. What I need is to hide parents if they don't match but their child do. As I guess it leads to tree reconstruction - changing parent of visible children. How is it better to do it?
Currently I have this, but of course it does show parents if children matches

bool TreeFilteredModel::filterAcceptsRow(int sourceRow, const QModelIndex &sourceParent) const
bool ret = false;
QModelIndex subIndex = sourceModel()->index(sourceRow, 0, sourceParent);
WorkflowTreeItem* item = (WorkflowTreeItem*)subIndex.internalPointer();
int childCound = sourceModel()->rowCount(subIndex);
if (subIndex.isValid())
for (int i = 0; i < childCound; ++i)
ret = ret || filterAcceptsRow(i, subIndex);
ret |= (item->command()->getName().contains(m_commandSearchParameter, Qt::CaseInsensitive) && item->command()->getName().contains(m_textSearchParameter, Qt::CaseInsensitive));
return ret;


21st October 2014, 15:44
As far as I remember filtering is done recursively, if you reject the parent, its children are not tested at all. So it seems your filtering boils down to checking if items match. If not, children will not be checked at all.