[Interest] Customize QTableView selection color

André Somers andre at familiesomers.nl
Fri Feb 5 17:35:35 CET 2016



Op 05/02/2016 om 16:34 schreef Murphy, Sean:
>
> I’m still struggling with how to customize the selection color for 
> items on a QTableView (previously posted as 
> http://lists.qt-project.org/pipermail/interest/2016-January/020760.html). 
> I currently color the rows with custom colors based on the data being 
> shown in each row in my model’s data(const QModelIndex &currIndex, int 
> role) when role == Qt::BackgroundRole.
>
> I’d like for that color to persist when the user selects a row; so 
> either I want the selection to only paint the dotted line around the 
> entire row without changing the background color to the OS’s default 
> selection color, or I’d also be fine if there was no change at allwhen 
> the row is selected. The only reason I allow rows to be selected at 
> all is apparently that is required to allow the user to start a drag 
> on a row when they are attempting to reorder the rows in the table. I 
> can’t seem to figure out where this needs to happen.
>
> Correct me if I’m wrong on any of the following:
>
> 1.I can’t use a stylesheet because the colors are different on a 
> row-by-row basis
>
> 2.I don’t think the selection background color is controlled anywhere 
> in the model?
>
> 3.I really don’t have any need for the user to be able to select 
> anything – the table is meant to be a read-only display of data - 
> EXCEPT it appears that being able to select items is a requirement for 
> the drag-n-drop system?
>
> Any pointers?
>
> Sean
>
>
If I understand you correctly, you basicaly want the color of the row to 
be whatever it was before it was selected, right?

Can't you just use a QProxyStyle and reset the selected flag from the 
style option before you pass on the render command to the underlying 
style?  Have the selection work, but not be visible. A 
QStyledItemDelegate doing a similar trick would work as well. Just make 
a copy of the style option, reset the selection flag in the state 
variable, and pass that on to the base class.

André

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.qt-project.org/pipermail/interest/attachments/20160205/32fde279/attachment.html>


More information about the Interest mailing list