How to set background color to the GroupItem in QTreeview?

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP



How to set background color to the GroupItem in QTreeview?



I have created a tree view. Here i would like to set the background color of the group item.
My implementation looks like this:


if (role == Qt::BackgroundRole && isCategory(index))
QBrush lightGrayColor(Qt::lightGray);
return lightGrayColor;



But the background of the icon is not colored.
enter image description here



If i use QTreeview:: setAlternatingColors(true) it is coloring properly. But I want to achieve in the model. Should i use QitemDelegate::paint method or can be achieved in the model itself. Thank you





The model contains the data. The view is the representation.
– scopchanov
Aug 8 at 11:25






I meant the model class has a method data where i can set the color. Should i modify here to set the color or just use alternatingcolor call. The problem is here i want just to highlight the group items and view does not have information about it which is a groupitem
– user8183713
Aug 8 at 11:31





I am sorry, I can't follow. What is a method data?
– scopchanov
Aug 8 at 11:34





From QAbstractItemModel::data(const QModelIndex& index, int role /*= Qt::DisplayRole*/) . Here you can use the index parameter and paint the background.
– user8183713
Aug 8 at 11:44





@vinaycool I do not see that that is handled by the model, or if you want it to be handled you will have to create your own QTreeView.
– eyllanesc
Aug 8 at 13:30









By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Popular posts from this blog

Firebase Auth - with Email and Password - Check user already registered

Dynamically update html content plain JS

How to determine optimal route across keyboard