我为我的问题找到了 C++代码,但我无法使用 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))
相关分类