Highlight tree item
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:
- Matched tree item gets high-lighted.
- 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.
Guest last edited by
Hi sajis, Only that item has to highlight ? or that has to expand?
If the item is hidden inside the parent item, which happens in case of the parent item is not expanded, then i would like the parent items to be expanded so that the requested tree item is visible and highlighted.
Otherwise, it only needs to be highlighted as you understand.
Lets take an example . Please go to the following link:
You can see a tree view containing several tree items. Suppose that i have enterd something inside the QLineEdit which matches the item residing inside "X3DShaderNode". Since this item is already expanded , we only need to highlight the item that matches any of its children.
If it were not expanded then we would have need it to be both expanded and then highlight the matched child item. If the matched child item in turns contains children , i do not need to expand the child item
I hope i explained the issue . Let me know your idea to solve it.
I am still waiting for some suggestion over the issue i posted last time over the very same thread.
If i have not clarified enough please let me know.
winkle99 last edited by
Your explanation is a little confusing, but I'll give it a shot.
I'm assuming that what you're trying to do is, based on certain criteria, highlight an element in your QTreeView. What I've found helpful in applying special attributes to elements in a QTreeView (or any of the Qt Model Views) is to add a delegate that draws the cell in a special way. I usually use QModelIndex::data and QModelIndex::setData to set and test these criteria.
So, for example, in my delegate (see QItemDelegate and its examples) I may do something like
@if ( index.data( Qt::UserRole + 5).toBool() == true )
painter->fillRect( option.rect, Qt::green); // that will draw the background cell in the special highlight color.@
One nice thing about this approach is that the QItemDelegate will only draw if the node is visible. You get that part for free.