Hello forum,

I have loaded a database into a tree view as follows:

nodetree.png


I have a textfield above the tree view where the user types the text and the matched tree item name with the entered text gets highlighted. There will be no changes(addition / deletion) within the tree view except the matched item will be highlighted.

If the matched item is within the uncollapsed tree item , it will be collapsed and shown highlighted.

I tried to get it done by subclassing the sor filter proxy model , but i could not make it functional. I need more help on this issue.


Qt Code:
  1. class H3DHighlighterProxyModel : public QSortFilterProxyModel
  2. {
  3. Q_OBJECT
  4. public:
  5.  
  6. H3DHighlighterProxyModel(QObject *parent = 0);
  7. ~H3DHighlighterProxyModel();
  8.  
  9. protected:
  10.  
  11. virtual QVariant data(const QModelIndex &index, int role) const;
  12.  
  13. private:
  14.  
  15. QVariant filterCheck(const QModelIndex&) const;
  16. };
  17.  
  18. QVariant H3DHighlighterProxyModel::data(const QModelIndex &index, int role) const
  19. {
  20. //get the handle to the underlyiing data - map the model index to the source model index
  21. // QModelIndex sourceIndex = mapToSource(index);
  22.  
  23. // //make sure that the model index is valid
  24. // if(!sourceIndex.isValid())
  25. // {
  26. // return QVariant();
  27. // }
  28.  
  29. // if(role == Qt::BackgroundRole)
  30. // {
  31. // return filterCheck(sourceIndex);
  32. // }
  33. // else
  34. // {
  35. // return sourceIndex.data(role);
  36. // }
  37.  
  38. if(role == Qt::BackgroundRole)
  39. {
  40. QString text = QSortFilterProxyModel::data(index).toString();
  41.  
  42. if(!filterRegExp().isEmpty())
  43. {
  44. if(text.contains(filterRegExp()))
  45. {
  46. return QColor(Qt::lightGray);
  47. }
  48. }
  49. }
  50.  
  51. return QSortFilterProxyModel::data(index,role);
  52. }
To copy to clipboard, switch view to plain text mode 


Any hint to sort it out?


Regards
Sajjad