Search code examples
pythonpyqtpyqt4qtableview

PyQt4 / QTableView : How to change the style of selection rectangle?


I am trying to change the style of selection rectangle in QTableView.

I have tried with stylesheet; but it allowed me to set only the selection background color as shown in Figure 1. I wanted the selection rectangle as shown in Figure 2. (No Fill Color + Thick black border)

Any idea?

def TableUi(self, Dialog):
    Dialog.setObjectName("Dialog")
    Dialog.resize(343, 179)

    self.tableWidget = QtGui.QTableWidget(Dialog)
    self.tableWidget.setGeometry(QtCore.QRect(10, 10, 321, 121))
    self.tableWidget.setStyleSheet("selection-color: rgb(255, 0, 127);\n"
    "selection-background-color: rgb(85, 255, 127);")
    self.tableWidget.setRowCount(3)
    self.tableWidget.setColumnCount(3)

enter image description here

Full Code Sample: https://justpaste.it/5fe3r


Solution

  • To style the selected cell you can target QTableView::item::selected in your stylesheet.

    self.tableWidget.setStyleSheet('''
        QTableView::item::selected {
          border: 5px solid black;
        }
    ''')
    

    qtableview_item_selected