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

发布于 2024-09-18 14:38:44 字数 40 浏览 5 评论 0原文

我有许多带有很长标签的狭窄列。我想将标签旋转 90 度。是否可以?

I have many narrow columns with very long labels. I want to rotate the labels by 90 degrees. Is it possible?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

人事已非 2024-09-25 14:38:45

您可能必须继承 QTableWidgetItem 并实现您自己的垂直文本绘制。然后在表格上使用 setHorizo​​ntalHeaderItem() 来指向新小部件的实例。

You will probably have to subclass QTableWidgetItem and implement your own vertical text painting. Then use setHorizontalHeaderItem() on your table to point to an instance of your new widget.

谁的年少不轻狂 2024-09-25 14:38:45

在寻找这个问题的答案时,我发现了很多提示,但没有真正的答案。提示告诉我们子类化 QHeaderView 并重新实现 PaintSection。当我尝试在 PyQt4 中执行此操作并尝试按照 QHeaderView 的源代码从头开始实现 PaintSection 时,我没有成功。然而,简单地旋转画家实例并调整所有尺寸提示就成功了。
该代码仅适用于水平标题,并且非常紧凑:

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

When searching for an answer to this questions I found many hints, but no real answer. The hints tell one to subclass QHeaderView and re-implement paintSection. When I tried to do so in PyQt4 and tried to implement paintSection from scratch, following the source of QHeaderView, I was not successful. However, simply rotating the painter instance and adjusting all the size hints was successful.
The code works for horizontal headers only and is nicely compact:

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
给我一枪 2024-09-25 14:38:45

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

将下一个代码复制并粘贴到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))

希望值得!

I've made a custom script that works fine based on previous answer..

copy and paste the next code in a rotated.py file

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

then import from your main.py file this class using this line:

from rotated import RotatedHeaderView

and complete the actions with this line:

self.YourTableName.setHorizontalHeader(RotatedHeaderView(self.YourTableName))

hope worth it!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文