如何在QTableWidget中设置垂直网格线?

2024-09-30 20:34:44 发布

您现在位置:Python中文网/ 问答频道 /正文

现在我正在使用self.setShowGrid(False),但它完全删除了网格线。所以不是全部就是什么都没有。但我只想要垂直的网格线,而不是像图中那样的水平网格线

这可能吗?如何做到这一点

enter image description here


Tags: selffalse水平网格线setshowgrid
1条回答
网友
1楼 · 发布于 2024-09-30 20:34:44

一种可能的解决方案是使用代理绘制这些线:

from PyQt5 import QtCore, QtGui, QtWidgets


class VerticalLineDelegate(QtWidgets.QStyledItemDelegate):
    def paint(self, painter, option, index):
        super(VerticalLineDelegate, self).paint(painter, option, 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_())

enter image description here

相关问题 更多 >