简体   繁体   English

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

[英]PyQT - Displaying vertical text in QTableWidget cells

I found C++ code for my question, but I can't make it work using Python. 我找到了解决问题的C ++ 代码 ,但无法使用Python使其正常工作。 I don't know C++, it's like recursion there... 我不了解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())

Running code: 运行代码:

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

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

If you look at the C++ example you refer to you'll see that the VerticalTextDelegate::paint implementation is basically fixing up the QPainter transform and then calling the base class implementation QStyledItemDelegate::paint . 如果您查看的是C++示例,则会看到VerticalTextDelegate::paint实现基本上是在修复QPainter转换,然后调用基类实现QStyledItemDelegate::paint You need to do likewise (untested)... 您也需要这样做(未经测试)...

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)

    # Call the base class implementation
    super(VerticalTextDelegate, self).paint(painter, optionCopy, index)

    painter.restore()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM