Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ruby-on-rails-3/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Qt-QTable可以将列标签旋转90度吗?_Qt_Qtablewidget - Fatal编程技术网

Qt-QTable可以将列标签旋转90度吗?

Qt-QTable可以将列标签旋转90度吗?,qt,qtablewidget,Qt,Qtablewidget,我有许多带有很长标签的窄列。我想把标签旋转90度。有可能吗?您可能需要子类化并实现自己的垂直文本绘制。然后在您的表格上使用setHorizontalHeaderItem()指向新小部件的一个实例。在搜索此问题的答案时,我发现了许多提示,但没有找到真正的答案。提示告诉我们将QHeaderView子类化并重新实现paintSection。当我尝试在PyQt4中这样做,并尝试按照QHeaderView的源代码从头实现paintSection时,我没有成功。但是,简单地旋转painter实例并调整所有大

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

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

在搜索此问题的答案时,我发现了许多提示,但没有找到真正的答案。提示告诉我们将QHeaderView子类化并重新实现paintSection。当我尝试在PyQt4中这样做,并尝试按照QHeaderView的源代码从头实现paintSection时,我没有成功。但是,简单地旋转painter实例并调整所有大小提示是成功的。 该代码仅适用于水平标题,并且非常紧凑:

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

根据前面的答案,我制作了一个自定义脚本,效果很好

在rotated.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))
并使用以下行完成操作:

from rotated import RotatedHeaderView
self.YourTableName.setHorizontalHeader(RotatedHeaderView(self.YourTableName))
希望值得