Keeping the same selection color in a TableView whether it's active or not

1.5k Views Asked by At

I'm using QTableView class in my GUI and I would like the selected row to have the same color whether the TableView is active or inactive. I tried to set this CSS stylesheet to achieve this:

QTableView:!active {
    selection-background-color: palette(Highlight);
    selection-color: palette(HighlightedText)
}

On Linux, it works just fine, but on Windows 7, when the TableView loses its focus, the text turns black instead of staying white (the background stays blue, so that part is OK). Am I missing something here ?

2

There are 2 best solutions below

2
On

You also have to style text color, for example just add:

QTableView:!active {
...
selection-color: white;
}
1
On

This works well in python

pal = tbl_list.palette()
pal.setColor(QPalette.Inactive, QPalette.Highlight, pal.color(QPalette.Active, QPalette.Highlight))
tbl_list.setPalette(pal)