def paintSection(self, painter, rect, logical_index): opt = QStyleOptionHeader() self.initStyleOption(opt) opt.rect = rect opt.section = logical_index opt.orientation = self.orientation() opt.textAlignment = Qt.AlignHCenter | Qt.AlignVCenter model = self.parent().model() opt.text = unicode(model.headerData(logical_index, opt.orientation, Qt.DisplayRole) or '') if self.isSortIndicatorShown() and self.sortIndicatorSection() == logical_index: opt.sortIndicator = QStyleOptionHeader.SortDown if self.sortIndicatorOrder() == Qt.AscendingOrder else QStyleOptionHeader.SortUp opt.text = opt.fontMetrics.elidedText(opt.text, Qt.ElideRight, rect.width() - 4) if self.isEnabled(): opt.state |= QStyle.State_Enabled if self.window().isActiveWindow(): opt.state |= QStyle.State_Active if self.hover == logical_index: opt.state |= QStyle.State_MouseOver sm = self.selectionModel() if opt.orientation == Qt.Vertical: try: opt.icon = model.headerData(logical_index, opt.orientation, Qt.DecorationRole) opt.iconAlignment = Qt.AlignVCenter except (IndexError, ValueError, TypeError): pass if sm.isRowSelected(logical_index, QModelIndex()): opt.state |= QStyle.State_Sunken painter.save() if ( (opt.orientation == Qt.Horizontal and sm.currentIndex().column() == logical_index) or (opt.orientation == Qt.Vertical and sm.currentIndex().row() == logical_index)): painter.setFont(self.current_font) self.style().drawControl(QStyle.CE_Header, opt, painter, self) painter.restore()
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 sectionSizeFromContents(self, logical_index): self.ensurePolished() opt = QStyleOptionHeader() self.initStyleOption(opt) opt.section = logical_index opt.orientation = self.orientation() opt.fontMetrics = self.fm model = self.parent().model() opt.text = unicode(model.headerData(logical_index, opt.orientation, Qt.DisplayRole) or '') if opt.orientation == Qt.Vertical: try: opt.icon = model.headerData(logical_index, opt.orientation, Qt.DecorationRole) opt.iconAlignment = Qt.AlignVCenter except (IndexError, ValueError, TypeError): pass if self.isSortIndicatorShown(): opt.sortIndicator = QStyleOptionHeader.SortDown return self.style().sizeFromContents(QStyle.CT_HeaderSection, opt, QSize(), self)
def sectionSizeFromContents(self, logical_index): self.ensurePolished() opt = QStyleOptionHeader() self.initStyleOption(opt) opt.section = logical_index opt.orientation = self.orientation() opt.fontMetrics = self.fm model = self.parent().model() opt.text = unicode(model.headerData(logical_index, opt.orientation, Qt.DisplayRole) or '') if opt.orientation == Qt.Vertical: try: val = model.headerData(logical_index, opt.orientation, Qt.DecorationRole) if val is not None: opt.icon = val opt.iconAlignment = Qt.AlignVCenter except (IndexError, ValueError, TypeError): pass if self.isSortIndicatorShown(): opt.sortIndicator = QStyleOptionHeader.SortDown return self.style().sizeFromContents(QStyle.CT_HeaderSection, opt, QSize(), self)