QTreeview Styling


I’ve been slowly teaching myself Qt styling in order to better create a visually cohesive set of tools. I’ve been doing this by taking an existing piece of software that takes CSS sheets and changing it to see what I can do, working my way through all the various widgets available in the online documentation. I’m pretty happy with my grasp so far, but I am stuck on how to style the items in QTreeview.

By default the software has a scheme like this:

When I use QTreeView::item:has-children { } I get this:

I can also get this by specifying has-siblings or adjoins-item:

I tried using nth-child(2) to grab the second level, but that just breaks a bunch of stuff. Is there any way via the CSS stylesheet to specifically style individual levels in the tree?




Not sure about individual levels (perhaps is top level widget/item).
What about alternate item background?


Hmm I can’t seem to find a stylesheet property to check for top level items.
Probably need to use a custom delegate with a paint override using the items data to determine how it should be represented?


Or actually, you could cheat it with a custom property on the item and a property check in the stylesheet? Seems dirty but should do the trick?


I’ve emailed the current maintainer of the program I’m editing to see if these have a tag, but it looks like you cannot differentiate between levels just using CSS unfortunately. I was hoping it would work like web CSS where you could use child levels. Oh well.

Thanks for the suggestions.