2
votes

I've been googling around for this issue and reading the qt documentation, but all the answers I can find are written for Qt for C++

I'm trying to understand how to remove all the horizontal gridlines from a QTableWidget like the one here

QTableWidget

to make the table appear like a list with rows and columns

I have tried using stylesheets to chage the appearance of the widget but they seem to have no effect on the table's appearance

1

1 Answers

4
votes

I don't think there's a built-in method, however you can remove horizontal gridlines via stylesheets by disabling gridlines using setShowGrid(False) and then drawing the vertical line borders with the QTableView::item selector and the border-right style like this:

tableWidget.setShowGrid(False)
tableWidget.setStyleSheet('QTableView::item {border-right: 1px solid #d6d9dc;}')

enter image description here

Here's a quick example

from PyQt5.QtWidgets import QApplication, QWidget, QTableWidget, QTableWidgetItem, QVBoxLayout
import sys

class Table(QWidget):
    def __init__(self):
        super().__init__()
        self.setGeometry(0, 0, 300, 200)
        self.create_table()
        self.layout = QVBoxLayout()
        self.layout.addWidget(self.tableWidget) 
        self.setLayout(self.layout) 
        self.show()

    def create_table(self):
        self.tableWidget = QTableWidget()
        self.tableWidget.setRowCount(4)
        self.tableWidget.setColumnCount(2)
        self.tableWidget.setItem(0,0, QTableWidgetItem("Cell (1,1)"))
        self.tableWidget.setItem(0,1, QTableWidgetItem("Cell (1,2)"))
        self.tableWidget.setItem(1,0, QTableWidgetItem("Cell (2,1)"))
        self.tableWidget.setItem(1,1, QTableWidgetItem("Cell (2,2)"))
        self.tableWidget.setItem(2,0, QTableWidgetItem("Cell (3,1)"))
        self.tableWidget.setItem(2,1, QTableWidgetItem("Cell (3,2)"))
        self.tableWidget.setItem(3,0, QTableWidgetItem("Cell (4,1)"))
        self.tableWidget.setItem(3,1, QTableWidgetItem("Cell (4,2)"))
        self.tableWidget.move(0,0)

        # Remove horizontal gridlines
        self.tableWidget.setShowGrid(False)
        self.tableWidget.setStyleSheet('QTableView::item {border-right: 1px solid #d6d9dc;}')

if __name__ == '__main__':
    app = QApplication(sys.argv)
    table = Table()
    sys.exit(app.exec_())  

Note: If you wanted to remove vertical gridlines, change the style from border-right to border-bottom

enter image description here