我有许多很窄的列,带有非常长的标签。我想将标签旋转90度。这是可能的吗?
#!/usr/bin/env python
from PyQt4.QtCore import *
from PyQt4.QtGui import *
class RotatedHeaderView(QHeaderView):
def __init__(self, parent=None):
super(RotatedHeaderView, self).__init__(Qt.Horizontal, parent)
self.setMinimumSectionSize(20)
def paintSection(self, painter, rect, logicalIndex ):
painter.save()
# translate the painter such that rotate will rotate around the correct point
painter.translate(rect.x()+rect.width(), rect.y())
painter.rotate(90)
# and have parent code paint at this location
newrect = QRect(0,0,rect.height(),rect.width())
super(RotatedHeaderView, self).paintSection(painter, newrect, logicalIndex)
painter.restore()
def minimumSizeHint(self):
size = super(RotatedHeaderView, self).minimumSizeHint()
size.transpose()
return size
def sectionSizeFromContents(self, logicalIndex):
size = super(RotatedHeaderView, self).sectionSizeFromContents(logicalIndex)
size.transpose()
return size
然后从您的main.py文件中使用以下代码行导入这个类:
from rotated import RotatedHeaderView
并使用以下行完成操作:
self.YourTableName.setHorizontalHeader(RotatedHeaderView(self.YourTableName))
希望这是值得的!
from PyQt4 import QtGui, QtCore
class RotatedHeaderView( QtGui.QHeaderView ):
def __init__(self, orientation, parent=None ):
super(RotatedHeaderView, self).__init__(orientation, parent)
self.setMinimumSectionSize(20)
def paintSection(self, painter, rect, logicalIndex ):
painter.save()
# translate the painter such that rotate will rotate around the correct point
painter.translate(rect.x()+rect.width(), rect.y())
painter.rotate(90)
# and have parent code paint at this location
newrect = QtCore.QRect(0,0,rect.height(),rect.width())
super(RotatedHeaderView, self).paintSection(painter, newrect, logicalIndex)
painter.restore()
def minimumSizeHint(self):
size = super(RotatedHeaderView, self).minimumSizeHint()
size.transpose()
return size
def sectionSizeFromContents(self, logicalIndex):
size = super(RotatedHeaderView, self).sectionSizeFromContents(logicalIndex)
size.transpose()
return size