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

标签 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))

最佳答案

如果您查看您引用的 C++ 示例,您会发现 VerticalTextDelegate::paint 实现基本上修复了 QPainter 转换然后调用基类实现QStyledItemDelegate::paint。你需要做同样的事情(未经测试)......

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()

关于python - PyQT - 在 QTableWidget 单元格中显示垂直文本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52162125/

相关文章:

python - 具有 stdlib 或最小依赖性的 Python 中的持久多进程共享缓存

python - 了解如何在链表中找到中间节点的 while 循环条件

c++ - if else 与 >= (2 times) 一起编译得很好但不起作用

c++ - 从迭代器推导出模板函数返回类型的值类型

python - 在 QHeaderView 和 QListWidget 之间拖放列

python - Pandas 数据 reshape ,根据关联将具有相同索引但不同值的多行转换为多列

python 列表理解 : gathering duplicate columns

c++ - 使用可变参数模板进行委托(delegate)

python - 使用 QDataWidgetMapper 立即更改模型

python - 删除 QMdiSubWindow 的图标和样式