def paintVerticalCell(self, painter: QPainter, hv: QHeaderView, cellIndex: QModelIndex, leafIndex: QModelIndex, logicalLeafIndex: int, styleOptions: QStyleOptionHeader, sectionRect: QRect, left: int): uniopt = QStyleOptionHeader(styleOptions) self.setForegroundBrush(uniopt, cellIndex) self.setBackgroundBrush(uniopt, cellIndex) width = self.cellSize(cellIndex, hv, uniopt).width() if cellIndex == leafIndex: width = sectionRect.width() - left top = self.currentCellLeft(cellIndex, leafIndex, logicalLeafIndex, sectionRect.top(), hv) height = self.currentCellWidth(cellIndex, leafIndex, logicalLeafIndex, hv) r = QRect(left, top, width, height) uniopt.text = cellIndex.data(Qt.DisplayRole) painter.save() uniopt.rect = r if cellIndex.data(Qt.UserRole): hv.style().drawControl(QStyle.CE_HeaderSection, uniopt, painter, hv) m = QMatrix() m.rotate(-90) painter.setWorldMatrix(m, True) new_r = QRect(0, 0, r.height(), r.width()) new_r.moveCenter(QPoint(-r.center().y(), r.center().x())) uniopt.rect = new_r hv.style().drawControl(QStyle.CE_HeaderLabel, uniopt, painter, hv) else: hv.style().drawControl(QStyle.CE_Header, uniopt, painter, hv) painter.restore() return left + width
def cellSize(self, leafIndex: QModelIndex, hv: QHeaderView, styleOptions: QStyleOptionHeader) -> QSize: res = QSize() variant = leafIndex.data(Qt.SizeHintRole) if variant: res = variant fnt = QFont(hv.font()) var = leafIndex.data(Qt.FontRole) if var: fnt = var fnt.setBold(True) fm = QFontMetrics(fnt) size = QSize( fm.size(0, leafIndex.data(Qt.DisplayRole)) + QSize(4, 0)) # WA: add more horizontal size (4px) if leafIndex.data(Qt.UserRole): size.transpose() decorationsSize = QSize(hv.style().sizeFromContents( QStyle.CT_HeaderSection, styleOptions, QSize(), hv)) emptyTextSize = QSize(fm.size(0, "")) return res.expandedTo(size + decorationsSize - emptyTextSize)