PyQT-在QTableWidget单元格中显示垂直文本

时间:2018-09-04 08:35:58

标签: python c++ pyqt

我找到了C ++ code,但我无法使用Python使它正常工作。 我不懂C ++,就像那里的递归...

class VerticalTextDelegate(QtGui.QStyledItemDelegate):
    def __init__(self, parent):
        super(VerticalTextDelegate, self).__init__()

    def paint(self, painter, option, index):
        optionCopy = QtGui.QStyleOptionViewItem(option)
        rectCenter = QtCore.QPointF(QtCore.QRectF(option.rect).center())
        painter.save()
        painter.translate(rectCenter.x(), rectCenter.y())
        painter.rotate(-90.0)
        painter.translate(-rectCenter.x(), -rectCenter.y())
        optionCopy.rect = painter.worldTransform().mapRect(option.rect)

        # recursion here, I don't understand how it works in C++
        # self.paint(painter, optionCopy, index)

        self.painter.restore()

    def sizeHint(self, option, index):
        val = QtGui.QSize(self.sizeHint(option, index))
        return QtGui.QSize(val.height(), val.width())

运行代码:

    item = QtGui.QTableWidgetItem("test")
    self.table_widget.setItem(2, 0, item)

    self.table_widget.setItemDelegateForColumn(0,VerticalTextDelegate(self))

1 个答案:

答案 0 :(得分:1)

如果您参考的是C++示例,您会发现VerticalTextDelegate::paint实现基本上是在修复QPainter转换,然后调用基类实现{{1 }}。您也需要这样做(未经测试)...

QStyledItemDelegate::paint