def sizeHint(self, option, index): # type: (QStyleOptionViewItem, QModelIndex) -> QSize opt = QStyleOptionViewItem(option) self.initStyleOption(option, index) opt.features |= QStyleOptionViewItem.HasDecoration widget = option.widget style = widget.style() if widget is not None else QApplication.style() sh = style.sizeFromContents( QStyle.CT_ItemViewItem, opt, QSize(), widget) return sh
def paint(self, painter, option, index): # type: (QPainter, QStyleOptionViewItem, QModelIndex) -> None opt = QStyleOptionViewItem(option) self.initStyleOption(opt, index) widget = option.widget if widget is not None: style = widget.style() else: style = QApplication.style() text = opt.text opt.text = "" trect = style.subElementRect(QStyle.SE_ItemViewItemText, opt, widget) style.drawControl(QStyle.CE_ItemViewItem, opt, painter, widget) # text margin (as in QCommonStylePrivate::viewItemDrawText) margin = style.pixelMetric(QStyle.PM_FocusFrameHMargin, None, widget) + 1 trect = trect.adjusted(margin, 0, -margin, 0) opt.text = text if opt.textElideMode != Qt.ElideNone: st = self.__static_text_elided_cache(opt, trect.width()) else: st = self.__static_text_cache(text) tsize = st.size() textalign = opt.displayAlignment text_pos_x = text_pos_y = 0.0 if textalign & Qt.AlignLeft: text_pos_x = trect.left() elif textalign & Qt.AlignRight: text_pos_x = trect.x() + trect.width() - tsize.width() elif textalign & Qt.AlignHCenter: text_pos_x = trect.center().x() - tsize.width() / 2 if textalign & Qt.AlignTop: text_pos_y = trect.top() elif textalign & Qt.AlignBottom: text_pos_y = trect.top() + trect.height() - tsize.height() elif textalign & Qt.AlignVCenter: text_pos_y = trect.center().y() - tsize.height() / 2 painter.setFont(opt.font) painter.drawStaticText(text_pos_x, text_pos_y, st)
def __init__(self, widget=None): self.widget = widget if widget is None: self.style = QApplication.instance().style() else: self.style = widget.style()