Use stylesheet in a qtablewidget to send properties of a cell


I'm trying to change the color of the text and background of an item in a qtablewidget with this code:

tabla.item(0,0).setStyleSheet("QTableWidget::item {border: 1px solid red;}")

Where table refers to a QTableWidget.

However, it does not do anything. What am I doing wrong?

asked by Revsky01 13.09.2018 в 07:34

1 answer


First of all the command that you point out should throw you an error since the QTableWidgetItem does not have the setStyleSheet () method.

On the other hand the Qt Style Sheets can not be applied to specific items pointing coordinates, you can apply it to certain items that you have certain states such as pressed , selected , etc.

Because of the above, we discard the use of qss, QTableWidgetItem has methods to indicate the colors of the text and background using the methods setForeground() and setBackground() , respectively.


from PyQt5 import QtCore, QtGui, QtWidgets

if __name__ == "__main__":
    import sys
    app = QtWidgets.QApplication(sys.argv)
    table = QtWidgets.QTableWidget(10, 10)

    it = QtWidgets.QTableWidgetItem("hola mundo")
    table.setItem(0, 0, it)

answered by 13.09.2018 / 10:39