3

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 ?

Mohammad Kanan
  • 4,452
  • 10
  • 23
  • 47
Trap
  • 218
  • 3
  • 8
  • 2
    The [colors](https://doc.qt.io/qt-5/stylesheet-reference.html#paletterole) should be `palette(highlight)` and `palette(highlighted-text)`. – ekhumoro Apr 12 '18 at 21:15

2 Answers2

0

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

QTableView:!active {
...
selection-color: white;
}
Mohammad Kanan
  • 4,452
  • 10
  • 23
  • 47
  • That's what I did as a workaround, but ekhumoro solution works too and I rather not hard code color in the stylesheet. – Trap Apr 13 '18 at 11:34
  • @Trap, great, I just use this simple approach to change to other color when the view is not in focus, that's a matter of whats the requirement :) – Mohammad Kanan Apr 13 '18 at 11:57
0

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)
QuentinJS
  • 162
  • 1
  • 9
  • I can't edit because some mysterious edit queue is full: To also make the text use the appropriate color, duplicate the `setColor` line and use `HighlightedText` instead of `Highlight`. For reference: https://doc.qt.io/qt-5/qpalette.html#ColorRole-enum – bugmenot123 Feb 16 '22 at 13:00