代码之家  ›  专栏  ›  技术社区  ›  danatel

qt-qtable能否将列标签旋转90度?

  •  2
  • danatel  · 技术社区  · 14 年前

    我有许多窄列,标签很长。我想把标签旋转90度。有可能吗?

    3 回复  |  直到 9 年前
        1
  •  3
  •   Arnold Spence    14 年前

    你可能需要子类 QTableWidgetItem 并实现自己的垂直文本绘制。然后使用 setHorizontalHeaderItem() 在您的表上指向新小部件的一个实例。

        2
  •  2
  •   Peter    11 年前

    在寻找这个问题的答案时,我发现了很多提示,但没有真正的答案。提示告诉我们将qheaderview子类化并重新实现paintsection。当我在Pyqt4中尝试这样做,并尝试从头开始实现paintsection时,遵循qheaderview的来源,我没有成功。但是,只需旋转paint实例并调整所有大小提示就可以成功了。 该代码仅适用于水平标题,并且非常紧凑:

    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
    
        3
  •  1
  •   Marcelo    9 年前

    我已经制作了一个自定义脚本,根据前面的答案,它可以正常工作。

    复制并粘贴旋转.py文件中的下一个代码

    #!/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))
    

    希望值得!