sergejacub
sergejacub

Reputation: 11

How to style a QTableWidget in Pyqt5

I'm currently styling my GUI application but i have some trouble with styling my QTableWidget, because as you can see in the picture, there are a little white area in the top left corner of my QTableWidget and i want it to be lightblue as the rest of the table.

Picture of the GUI

This is the code of how i have styled so far:

def start_ui():
    app = QtWidgets.QApplication(sys.argv)
    style = """
        QFrame{
            color: blue;
            background: lightgreen;
            font-weight: bold;
        }
        QLabel{
            color: blue;
            font-weight: bold;
        }
        QLineEdit{
            color: blue;
            border-style: solid;
            border: 2px solid black;
            background: lightblue;
            font-weight: bold;
        }
        QPushButton{
            border-style: solid;
            border: 2px solid black;
            color: darkblue;
            background: lightblue;
            font-weight: bold;
        }
        QTableWidget{
            background: lightblue;
        }
    """
    app.setStyleSheet(style)
    win = ModbusLoggerWindow()
    win.show()
    sys.exit(app.exec_())

And then i have these three lines in the class, where i create my QTableWidget and these lines styles my verticalheaders and horizontalheaders:

    stylesheetHeader = "::section{background-color: lightblue}"
    self.tableWidget.horizontalHeader().setStyleSheet(stylesheetHeader)
    self.tableWidget.verticalHeader().setStyleSheet(stylesheetHeader)

Upvotes: 0

Views: 2962

Answers (2)

Michael
Michael

Reputation: 156

Anyone looking for this in 2023+, using PyQt6:

QTableWidget QTableCornerButton::section {
    background-color: lightblue;
}

Upvotes: 1

eyllanesc
eyllanesc

Reputation: 243897

This element in the top-left part is not part of the QHeaderView but a QAbstractButton so a possible solution is to apply the stylesheet directly to the button:

button = self.tableWidget.findChild(QAbstractButton)
button.setStyleSheet("background-color: lightblue")

To avoid the use of findChild it can be set via QTableWidget:

stylesheet = """
QHeaderView::section{background-color: lightblue}
QAbstractButton{background-color: lightblue}
"""
self.tableWidget.setStyleSheet(stylesheet)

Upvotes: 2

Related Questions