View Full Version : Custom Tree paint per row

23rd September 2008, 04:17
A quick rundown of what I'm trying to accomplish: I have a standardItemModel with two QTreeViews. The first is setup correctly and works fine. The second responds to indirect input. In the main working area, if the user clicks an object, the associations with that object should be visually differentiated. If I click a large blue object and my treeview has 'large' and 'blue' tags in it, I want those strings drawn in different color text.

My problem comes from the convoluted model/view relationship and the lack of helpful Qt docs. I've read everything there is to regarding itemDelegates over and again, and yet I am still completely in the dark about the basics. It seems that only one delegate is set for a given view so perhaps that isn't what I need. How can this be so hard to do cleanly? I have a model that I've queried, I know that items match, and I know my tree reflects this model. Why can't I say "hey tree! draw this 'item' (or your representation thereof) blue"!?

As an aside rant, why do standardItems, the direct members of an objectModel, have any sort of visualization functionality? For instance, when prototyping this thing I queried the tree's model for matches and got a list of standardItems. While I can easily change their foreground color, ALL views with this model reflect the changed color. That is not a separation of model and view at all! In my case, I'm ONLY worried about the view.

23rd September 2008, 06:13
Well, you are right till delegates. Further more, you will need to look into syntax highlighters :)

Set the highlighter for the delegates, and give the highlighter a list of words that is has to hiighlight ...
You can refer examples in qt. Also there was xml highlighter by JPN, but i dont remmeber the path. u can search for it on wiki or google.

24th September 2008, 01:19
Thank you for the reply.

After your recommendation, I took a short look at syntax highlighting and I am unsure if it will work for me. I know the trees will have strings (ie. "blue" and "large") repeating even though their underlying items are unique. I only want specific, unique items to highlight which makes me believe syntax highlighting may not work.

As far as delegates are concerned, I understand more than when I first posted last night but it seems they are still too directly tied to the model. If I understand correctly, when placing items in the model, I can set their data and dataRole. When the delegate draws each item, it determines how and what to draw based on the type of data and/or its role.

I've tried using an "unset" delegate to draw individual items but I don't know if my code is wrong or the method is incorrect no matter what. By "unset" I mean my view uses the default delegate and I am attempting to call delegate->paint(painter, styleOption, index) on the desired indices. Nothing happens when I do this but perhaps it is immediately overdrawn and I never see it.

Or more likely, I'm not instantiating my painter or styleOption correctly. All the code examples I've seen are overrides of paint() or drawRow() so the styleOption is already set and passed in as a parameter so I've no idea what about should be set, or when.

The bottom line is that there simply has to be a more direct, ad hoc way of drawing items in a view.