Change the selection color of a QTableWidget
Asked Answered
F

2

12

The default is for the selected row to be colored gray if the QTableWidget does not have focus, and orange if it does have focus. I would like to, instead, make the selected row be red whether or not the widget has focus. I tried adding this to the style sheet:

QTableWidget{ selection-background-color: red}

I also tried

QTableWidget:edit-focus{ selection-background-color: red} 

and

QTableWidget:focus{ selection-background-color: red} 

but none of them seem to turn anything red, it still seems to remain orange if focused and gray if not. What properties do I have to set to make the selected row always the same color, whether or not it has focus?

Thanks,

David

Foldaway answered 20/10, 2011 at 18:19 Comment(0)
B
25

You almost had it. Technically speaking, you're adjusting the selection color of the items within your table widget, so:

QTableWidget::item{ selection-background-color: red}

should do the trick.

Alternatively:

QTableWidget::item{ background-color: blue }
QTableWidget::item:selected{ background-color: red }
Busby answered 20/10, 2011 at 18:41 Comment(1)
It does the trick very well, but it doesn't work when we set the "background-color" of the item as well. I have to comment the background-color out to make the selection-colors work.Corrinecorrinne
M
0

background-color will set background color of the items in row and selection-color will set the text color because when the row is selected then if text are present in the row then it might become unreadable due to color so setting proper text color is important.

#d9fffb : light blue

#000000 : black

self.table.setStyleSheet(
            "QTableView::item:selected"
            "{"
            "background-color : #d9fffb;"
            "selection-color : #000000;"
            "}"
        )
Matherly answered 11/5, 2022 at 4:31 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.