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 2008
    Location
    Alameda, CA, USA
    Posts
    5,349
    Qt products
    Qt5
    Platforms
    Windows
    Thanks
    318
    Thanked 872 Times in 859 Posts

    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.

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

    sajis997 (7th March 2012)

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

    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

  4. #3
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,349
    Qt products
    Qt5
    Platforms
    Windows
    Thanks
    318
    Thanked 872 Times in 859 Posts

    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.