微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

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

如何解决如何在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

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。