如何在QTableWidget中使用垂直网格线?

问题描述

现在我正在使用self.setShowGrid(False),但是它完全删除了网格线。因此,要么全部要么一无所有。但是我只想像照片中那样有垂直的网格线,而不是水平的。

这可能吗?该怎么做?

enter image description here

解决方法

一种可能的解决方案是使用委托来绘制这些线条:

from PyQt5 import QtCore,QtGui,QtWidgets


class VerticalLineDelegate(QtWidgets.QStyledItemDelegate):
    def paint(self,painter,option,index):
        super(VerticalLineDelegate,self).paint(painter,index)
        line = QtCore.QLine(option.rect.topRight(),option.rect.bottomRight())
        color = option.palette.color(QtGui.QPalette.Mid)
        painter.save()
        painter.setPen(QtGui.QPen(color))
        painter.drawLine(line)
        painter.restore()

if __name__ == "__main__":
    import sys

    app = QtWidgets.QApplication(sys.argv)
    w = QtWidgets.QTableWidget(10,4)
    delegate = VerticalLineDelegate(w)
    w.setItemDelegate(delegate)
    w.setShowGrid(False)
    w.resize(640,480)
    w.show()
    sys.exit(app.exec_())

Part