Results 1 to 5 of 5

Thread: filter tree item by highlight

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Join Date
    Jan 2011
    Posts
    212
    Thanks
    24
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default filter tree item by highlight

    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

  2. #2
    Join Date
    Jan 2011
    Posts
    212
    Thanks
    24
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: filter tree item by highlight

    Hello forum,


    In the manual it says that , "In addition to sorting, QSortFilterProxyModel can be used to hide items that do not match a certain filter." I would rather like to highlight the item that matches and i do not want to hide any thing in my tree model.


    Please take a look at the attached image

    http://www.student.itn.liu.se/~sajis997/nodetree.png

    I have loaded a node database into the tree view. To increase the usability i also have a line edit so that the user finds it easier to scroll to a particular tree item once the item entered in the line edit matches any of the item inside the tree view.

    I would have the following functionalities:

    1. Matched tree item gets high-lighted.
    2. If the item resides inside the un-expanded parent item, it will be highlighted after it has been expanded.

    I got to look at the QSortFilterProxyModel and but the related examples shows how the the items filtered with the regular expression and the table views is also altered to reflect the filtered items. The altercation results is removing item from the views, which i do not want to have. I just want to get the tree item high-lighted.

    I hope to get some tips to implement this mechanism into my project.

    If i have not explained well enough please mention it. I shall elaborate again



    Regards
    Sajjad

  3. #3
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,328
    Thanks
    317
    Thanked 871 Times in 858 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: filter tree item by highlight

    So if you do not want to filter out any items from the tree, then using a QSortFilterProxyModel is probably overkill for this problem. However, if you do use it, then you need to implement the QSortFilterProxyModel::data() method to handle the case where the "role" parameter has the values Qt::ForegroundRole and Qt::BackgroundRole. If the modelIndex is one that should be highlighted, then you should return a QBrush that is your highlight color for the text and background, respectively. If it is not a highlighted item, then you simply return QVariant() and the model/view architecture will display it normally.

    If you only have one tree displaying the model (or if you want all views of the model to show the same highlight), then you don't actually need a proxy, you can simply handle this in the model itself.

    As you have seen from the examples, QSortFilterProxyModel is generally used for much more complex filtering of a model.

  4. The following user says thank you to d_stranz for this useful post:

    sajis997 (7th March 2012)

  5. #4
    Join Date
    Jan 2011
    Posts
    212
    Thanks
    24
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: filter tree item by highlight

    Thanks for the hint. Lets not over kill it. I already have the sub-classed Model from QAbstractItemModel. And i have over-ridden the data(...) as follows:

    Qt Code:
    1. QVariant H3DTreeModel::data(const QModelIndex &index, int role) const
    2. {
    3. if (!index.isValid())
    4. return QVariant();
    5.  
    6. H3DTreeItem *item = static_cast<H3DTreeItem*>(index.internalPointer());
    7.  
    8. if(role == Qt::DecorationRole)
    9. {
    10. return QIcon(":/H3D/images/Node.png");
    11. }
    12.  
    13. /*
    14.   display role is used to access string that
    15.   can be displayed as text in the view
    16.   */
    17. else if(role == Qt::DisplayRole)
    18. {
    19. return item->data(index.column());
    20. }
    21. else if(role == Qt::UserRole)
    22. {
    23. return QPixmap(":/H3D/images/Node.png");
    24. }
    25. else if(role == Qt::ToolTipRole)
    26. {
    27. //return item->data(index.column());
    28. if(m_nodeFactory)
    29. {
    30. return m_nodeFactory->getNodeData(item->data(index.column()).toString())->nodeData();
    31. }
    32. else
    33. return item->data(index.column());
    34. }
    35. // else if(role == Qt::BackgroundRole)
    36. // {
    37. // return item->data(index.column());
    38. // }
    39. // else if(role == Qt::BackgroundColorRole)
    40. // {
    41. // return item->data(index.column());
    42. // }
    43. // else if(role == Qt::ForegroundRole)
    44. // {
    45. // return item->data(index.column());
    46. // }
    47. else
    48. return QVariant();
    49. }
    To copy to clipboard, switch view to plain text mode 


    What other changes i have to do over this model subclasses so that the entered line edit string pattern will match the item in the tree-views and high-light it or expand and high-light it.



    Regards
    Sajjad

  6. #5
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,328
    Thanks
    317
    Thanked 871 Times in 858 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: filter tree item by highlight

    I would look at reimplementing the QAbstractItemModel::match() method so that it searches through your tree to return the list of indexes that match the search string. You would then call beginResetModel(), save this list to a member variable in your model class, and trigger a refresh of the view by calling endResetModel().

    In the data() method, if the role is Qt::BackgroundRole or Qt::ForegroundRole -and- the index is on the list you saved, you return appropriate brushes for background and foreground highlight (as QVariant), otherwise you return an empty QVariant to show the normal (non-highlighted) appearance.

    Note that Qt::BackgroundColorRole is deprecated; you should eliminate this else clause and simply look at the BackgroundRole.

    Edit: To expand the highlighted nodes, I think you need to implement a handler for the model's reset() signal in the widget that holds your QTreeView. When you receive it, retrieve the list of matched indexes from the model, then iterate over it, calling the QTreeView::expand() slot for each one.

    Further edit, and maybe a simpler way to do this: If the default appearance of selected items is OK for you, then you don't need to implement the BackgroundColor and ForegroundColor role handlers. Instead, pass the results of the match() method to a QItemSelectionModel subclass and set this on the view. The view will then display the selected items with the right colors. This way, you could have the same model displayed in different trees, each one with a different set of nodes highlighted. (Doing it all within the model itself means that every view would be the same).
    Last edited by d_stranz; 7th March 2012 at 03:58.

Similar Threads

  1. Filter tree view
    By Ginsengelf in forum Qt Programming
    Replies: 1
    Last Post: 19th October 2011, 14:10
  2. Replies: 0
    Last Post: 5th September 2011, 08:08
  3. Highlight Tree Item Viw
    By sajis997 in forum Qt Programming
    Replies: 4
    Last Post: 19th July 2011, 09:40
  4. Replies: 1
    Last Post: 18th November 2008, 06:57
  5. Optimizing filterAcceptsRow() to filter a tree
    By vfernandez in forum Qt Programming
    Replies: 1
    Last Post: 4th January 2007, 12:50

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Qt is a trademark of The Qt Company.