qt - Changing text direction in QTableView header -
i have table view in qt, header text go in vertical direction:
either in green square or in red?
you can achieve providing own implementation of qheaderview
. here example implementation, overrides paintsection
method paint text vertically.
class myheaderview(qtwidgets.qheaderview): def __init__(self, parent=none): super().__init__(qt.horizontal, parent) self._font = qtgui.qfont("helvetica", 15) self._metrics = qtgui.qfontmetrics(self._font) self._descent = self._metrics.descent() self._margin = 10 def paintsection(self, painter, rect, index): data = self._get_data(index) painter.rotate(-90) painter.setfont(self._font) painter.drawtext(- rect.height() + self._margin, rect.left() + (rect.width() + self._descent) / 2, data) def sizehint(self): return qtcore.qsize(0, self._get_text_width() + 2 * self._margin) def _get_text_width(self): return max([self._metrics.width(self._get_data(i)) in range(0, self.model().columncount())]) def _get_data(self, index): return self.model().headerdata(index, self.orientation())
you can use class in view follows:
headerview = myheaderview() tableview.sethorizontalheader(headerview)
and result in following view:
as side note want add regular items, rather provide own item delegate. however, noted in qt documentation:
each header renders data each section itself, , not rely on delegate. result, calling header's setitemdelegate() function have no effect.
Comments
Post a Comment