February 14, 2012

sajis997 sajis997
Lab Rat
24 posts

highlight tree item

 

Hello forum,

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.

Thanks
Sajjad

4 replies

February 14, 2012

Deleted Member # 1d173 Deleted Member # 1d173
Lab Rat
37 posts

Hi sajis, Only that item has to highlight ? or that has to expand?

February 14, 2012

sajis997 sajis997
Lab Rat
24 posts

Hi

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:

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

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.

Regards
Sajjad

March 6, 2012

sajis997 sajis997
Lab Rat
24 posts

I am still waiting for some suggestion over the issue i posted last time over the very same thread.

Anyone ?

If i have not clarified enough please let me know.

Regards
Sajjad

March 7, 2012

winkle99 winkle99
Lab Rat
8 posts

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

  1. if ( index.data( Qt::UserRole + 5).toBool() == true )
  2.   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.

 
  ‹‹ Strange behavior when set data back from delegate to model      QSqlQueryModel headerData Qt::Vertical Display keep increasing the row numbers ››

You must log in to post a reply. Not a member yet? Register here!