12

I'm trying to change the background color of a QTableWidget row. There are some others posts about the same thing but none of the given solutions worked for me.

  • With that solution, we are setting the background on an already existing item in the table to a light grey on the item at row 0, column 1:

    self.table.item(1,0).setBackground(QtGui.QColor(125,125,125))

It's working, but set background for the row with iterating needs more time if you have more then one table. I need a function to change the row background by passing only the row index!

GCB613
  • 174
  • 13
stepBystep
  • 187
  • 1
  • 1
  • 8

1 Answers1

29

There is no function that performs this task, but we can create it, for example:

def setColortoRow(table, rowIndex, color):
    for j in range(table.columnCount()):
        table.item(rowIndex, j).setBackground(color)

enter image description here

eyllanesc
  • 235,170
  • 19
  • 170
  • 241
  • Is there a function that can set the background color of the tableWidget to a specific color? I mean for example all cells have gray background regardless of having or not having a value. – nooshinha Jun 09 '21 at 08:13
  • 1
    What is example of `color`? It requires `PySide2.QtWidgets.QTableWidgetItem.setBackground(PySide2.QtGui.QBrush)`? – Delrius Euphoria Jun 21 '21 at 09:26
  • use `item.setBackground(QBrush(color))` – eyllanesc Jun 21 '21 at 15:50