gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-28 04:33:39 27 4
gpt4 key购买 nike

我找到了 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/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com