PDA

View Full Version : Do it yourself alternating row colors.



barnabyr
1st November 2008, 02:00
Hello !

I am using a subclassed QAbstractItemView and my model is a subclass of a QAbstractItemModel.

In my model's 'data' function I want to return a QBrush variant for the role, Qt.BackgroundRole. However I want to return an alternating row color depending on how the index is positioned in the view.

"So why don't you use QAbstractItemView.setAlternatingRowColors ?" You might ask.

Well .. the problem is two fold: Problem one is that there is only one selection color in a QAbstractItemView so you have D.I.Y. if you want to alternately color selected rows yourself. Problem two is that if you are doing some complex delegate drawing, you might want to know what brush the background is painted with.

So my question is this ... What is the best way to find out, from within the model, if you are painting an even or odd row ? Right now I'm caching this information by keeping a parallel data structure to the model, finding out what is not visible due to it being collapsed or filtered etc and then calculating odd and even that way. But this is a pain and it breaks when you start using view sorting.

Can Qt help me .... can I hijack what ever the internal mechanism is that makes setAlternatingRowColors work somehow ?

Thanks for any ideas.

barnaby.

caduel
1st November 2008, 09:24
The model can tell only based on its indices. However, as you often will wrap the model in a sorting or filtering proxy model, this approach will not really work.

Try using a style sheet. From the style sheet docs

Supports the box model. When alternating row colors is enabled, the alternating colors can be styled using the alternate-background-color property.

The color and background of the selected item is styled using selection-color and selection-background-color respectively.

HTH

wysota
1st November 2008, 09:49
Well .. the problem is two fold: Problem one is that there is only one selection color in a QAbstractItemView so you have D.I.Y. if you want to alternately color selected rows yourself. Problem two is that if you are doing some complex delegate drawing, you might want to know what brush the background is painted with.

Leave the model alone, trying to do it there is a wrong path. All you need is to modify the delegate - it knows if it's painting a "base" or "alternate base" colour through the use of QStyleOptionViewItemV2, so you can do all the background painting you want there.

barnabyr
1st November 2008, 18:30
Leave the model alone, trying to do it there is a wrong path. All you need is to modify the delegate - it knows if it's painting a "base" or "alternate base" colour through the use of QStyleOptionViewItemV2, so you can do all the background painting you want there.

Hi wysota,

Many thanks for the insightful reply. Yes that is what I should do. I only have 4.3 right now but an upgrade to 4.4 would be most useful as I could then use a QStyledItemDelegate and call initStyleOption which would initialize all those useful properties like the 'Alternate' feature.

barnaby.