[Interest] Customize QTableView selection color

Murphy, Sean smurphy at walbro.com
Fri Feb 5 17:39:46 CET 2016


> > I’m still struggling with how to customize the selection color for items on a
> > QTableView
> 
> I use a QStyledItemDelegate.
> In the paint(painter,option,index) method I create my own QPalette based
> on
> selection etc and then call the base class paint function.
> 
>      QStyleOptionViewItemV4 subop = option;
>      QPalette pal = qApp->palette();
>      if( selected ) {
>         pal.setColor(QPalette::Highlight,Qt::darkGray);
>         pal.setColor(QPalette::WindowText,Qt::white);
>         pal.setColor(QPalette::HighlightedText,Qt::white);
>         pal.setColor(QPalette::Text,Qt::white);
>         subop.state |= QStyle::State_Selected;
>         subop.palette = pal;
>     }
> 
>     BaseClass::paint(painter,subop,index);	// Note: subop.

Ok, so then in my case, I'd just inherit from QStyledItemDelegate and add a function that allows me to pass in my QMap<myItem*, QColor> so that I can adjust your calls of setColor() in paint() on the fly based on modelIndex.

Do you happen to have any pointers for customizing the drag and drop indicator? In my case, the user can only reorder rows, so I'd like the drop indicator to just be a thick line drawn in between rows, not a rectangle around an item/row. I was playing around with a custom QProxyStyle set on the view class, overloading drawPrimitive(QStyle::PrimitiveElement element, const QStyleOption *option, QPainter *painter, const QWidget *widget) when the element is a QStyle::PE_IndicatorItemViewItemDrop. I was able to have the indicator be just a line, but I was struggling a little to determine when the indicator needs to switch from being drawn above a row to below the row based on mouse pointer position. I didn't spend a lot of time on it though, so I may just need to go back and play with it some more.

Thanks!
Sean 


More information about the Interest mailing list