ラベルが非常に長い細い列がたくさんあります。ラベルを90度回転させたい。出来ますか?
2194 次
3 に答える
3
おそらく、 QTableWidgetItemをサブクラス化し、独自の垂直テキストペインティングを実装する必要があります。次にsetHorizontalHeaderItem()
、テーブルで使用して、新しいウィジェットのインスタンスをポイントします。
于 2010-09-06T18:16:56.107 に答える
2
この質問に対する答えを探しているとき、私は多くのヒントを見つけましたが、本当の答えはありませんでした。ヒントは、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
于 2014-02-11T04:01:25.327 に答える
2
以前の回答に基づいて正常に機能するカスタムスクリプトを作成しました。
次のコードをコピーして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))
それだけの価値があることを願っています!
于 2016-03-11T04:57:50.137 に答える