I have a custom QSortFilterProxyModel that's being used with a custom model and a QTreeView. Through a QLineEdit and the corresponding signal/slot connection, it allows the user to search for something in the tree of the model and also to hide/show comments which are also nodes in the tree. This is the code:

Qt Code:
  1. ParsedTreeFilterProxyModel::ParsedTreeFilterProxyModel(QObject *parent)
  2. {
  3. m_commentsVisible = true;
  4. }
  5.  
  6.  
  7. bool ParsedTreeFilterProxyModel::commentsVisible()
  8. {
  9. return m_commentsVisible;
  10. }
  11.  
  12.  
  13. void ParsedTreeFilterProxyModel::setCommentsVisible(bool visible)
  14. {
  15. m_commentsVisible = visible;
  16. filterChanged();
  17. }
  18.  
  19.  
  20. bool ParsedTreeFilterProxyModel::filterAcceptsRow(int sourceRow, const QModelIndex &sourceParent) const
  21. {
  22. QString name = sourceModel()->data( sourceModel()->index(sourceRow, 0, sourceParent) ).toString();
  23. QString value = sourceModel()->data( sourceModel()->index(sourceRow, 1, sourceParent) ).toString();
  24. QString type = sourceModel()->data( sourceModel()->index(sourceRow, 0, sourceParent), Qt::UserRole ).toString();
  25. bool isComment = (type == "ConfigNode");
  26. return ( (!isComment || m_commentsVisible) && (name.contains(filterRegExp()) || value.contains(filterRegExp())) );
  27. }
To copy to clipboard, switch view to plain text mode 

Nothing special but as you may guess, when I search for something it only displays the nodes from the top level. When a node is inside some subtree, the parent tree nodes aren't displayed (as the documentation states). Now I want to make it work as it should: display a tree node if there's some child somewhere in its subtree that matches the filter.

The question is: when filterAcceptsRow() is processing one of those parent tree nodes, do I have to search for nodes that match the filter through the whole subtree (which is obviously very inefficient and time-consuming) or is there a better, more efficient, way to do it? I could think of moving through the tree and when a node matches the filter marking all the parent nodes as "visible" or something like that but AFAIK it's not possible with filterAcceptsRow(). Is it?

P.S.: I'm using Qt 4.2.