Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Set text color of QTableWidgetItem (Qt)

Tags:

qt

pyqt

pyqt5

QTableWidgetItem has a method for setting the backgroundColor of a cell in the table, but no method for setting the text color of that cell.

How do I change the text color of an arbitrary cell in a QTableWidget?

Changing the color of text in all cells is as simple as using this stylesheet.

QTableWidget::item {
    color: red;
}

But because the API is on the QTableWidget level (rather than QTableWidgetItem level), I find it impossible to target individual cells.

like image 435
Andrei Cioara Avatar asked Apr 08 '19 12:04

Andrei Cioara


2 Answers

The method is called setForeground() (not sure how I missed it). Not sure how to do it in CSS only though.

Code:

item = QTableWidgetItem('text')
item.setForeground(QBrush(QColor(0, 255, 0)))
like image 51
Andrei Cioara Avatar answered Nov 06 '22 01:11

Andrei Cioara


I know it's been a while, but you could try:

QTableWidget::item:selected { color:red; }
like image 22
steven Avatar answered Nov 06 '22 00:11

steven