Issue
I wish to make horizontal header cells in a QTableView display text from top to bottom (i.e., vertically), how can I do this?
Example PyQt5 app which displays a QTableView with a horizontal header showing text in the normal direction:
from PyQt5.QtGui import *
from PyQt5.QtWidgets import *
from PyQt5.QtCore import *
import sys
class TableModel(QAbstractTableModel):
def __init__(self, parent):
super(TableModel, self).__init__(parent)
def headerData(self, section, orientation, role):
if orientation != Qt.Horizontal:
return
if role != Qt.DisplayRole:
return
return 'Header Data'
def data(self, index, role):
if role != Qt.DisplayRole:
return
return 'Row Data'
def rowCount(self, parent):
return 1
def columnCount(self, parent):
return 1
class Window(QMainWindow):
def __init__(self):
super(Window, self).__init__()
main_widget = QWidget(self)
self.setCentralWidget(main_widget)
layout = QVBoxLayout(main_widget)
view = QTableView(main_widget)
view.horizontalHeader().setVisible(True)
view.verticalHeader().setVisible(False)
layout.addWidget(view)
model = TableModel(view)
view.setModel(model)
view.resizeColumnsToContents()
app = QApplication(sys.argv)
window = Window()
window.show()
app.exec_()
Solution
Simple use of delegate will not work here.
You need to subclass QHeaderView and override paintSection.
In implementation of it you need:
- rotate and translate painter
- calculate new rectange which wil take into acount above transformation
- call old implementation of
paintSection
with new values - restore painter state (reverse transformation).
Answered By - Marek R
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.