def paint(self, painter, option, index): painter.save() painter.setClipRect(QRectF(option.rect)) if hasattr(QStyle, 'CE_ItemViewItem'): QApplication.style().drawControl(QStyle.ControlElement.CE_ItemViewItem, option, painter) elif option.state & QStyle.StateFlag.State_Selected: painter.fillRect(option.rect, option.palette.highlight()) painter.translate(option.rect.topLeft()) self.to_doc(index).drawContents(painter) painter.restore()
def paint(self, painter, option, index): opts = QStyleOptionProgressBar() opts.rect = option.rect opts.minimum = 1 opts.maximum = 100 opts.textVisible = True try: percent = int(index.model().data(index, Qt.ItemDataRole.DisplayRole)) except (TypeError, ValueError): percent = 0 opts.progress = percent opts.text = (_('Unavailable') if percent == 0 else '%d%%'%percent) QApplication.style().drawControl(QStyle.ControlElement.CE_ProgressBar, opts, painter)
def paint(self, painter, option, index): self.initStyleOption(option, index) style = QApplication.style() if option.widget is None \ else option.widget.style() self.document.setHtml(option.text) style.drawPrimitive(QStyle.PrimitiveElement.PE_PanelItemViewItem, option, painter, widget=option.widget) rect = style.subElementRect( QStyle.SubElement.SE_ItemViewItemDecoration, option, self.parent()) ic = option.icon if rect.isValid() and not ic.isNull(): sz = ic.actualSize(option.decorationSize) painter.drawPixmap(rect.topLeft(), ic.pixmap(sz)) ctx = QAbstractTextDocumentLayout.PaintContext() ctx.palette = option.palette if option.state & QStyle.StateFlag.State_Selected: ctx.palette.setColor( QPalette.ColorRole.Text, ctx.palette.color(QPalette.ColorRole.HighlightedText)) textRect = style.subElementRect(QStyle.SubElement.SE_ItemViewItemText, option, self.parent()) painter.save() painter.translate(textRect.topLeft()) painter.setClipRect(textRect.translated(-textRect.topLeft())) self.document.documentLayout().draw(painter, ctx) painter.restore()
def paint(self, painter, option, index): QStyledItemDelegate.paint(self, painter, option, index) style = QApplication.style() # Ensure the cover is rendered over any selection rect style.drawItemPixmap( painter, option.rect, Qt.AlignmentFlag.AlignTop | Qt.AlignmentFlag.AlignHCenter, QPixmap(index.data(Qt.ItemDataRole.DecorationRole)))
def paint(self, painter, option, index): QStyledItemDelegate.paint(self, painter, option, index) style = QApplication.style() waiting = self.animator.is_running() and bool(index.data(Qt.ItemDataRole.UserRole)) if waiting: rect = QRect(0, 0, self.spinner_width, self.spinner_width) rect.moveCenter(option.rect.center()) self.animator.draw(painter, rect, self.color) else: # Ensure the cover is rendered over any selection rect style.drawItemPixmap(painter, option.rect, Qt.AlignmentFlag.AlignTop|Qt.AlignmentFlag.AlignHCenter, QPixmap(index.data(Qt.ItemDataRole.DecorationRole)))
def updateEditorGeometry(self, editor, option, index): if editor is None: return fm = editor.fontMetrics() # get the original size of the edit widget opt = QStyleOptionViewItem(option) self.initStyleOption(opt, index) opt.showDecorationSelected = True opt.decorationSize = QSize( 0, 0) # We want the editor to cover the decoration style = QApplication.style() initial_geometry = style.subElementRect( QStyle.SubElement.SE_ItemViewItemText, opt, None) orig_width = initial_geometry.width() # Compute the required width: the width that can show all of the current value if hasattr(self, 'get_required_width'): new_width = self.get_required_width(editor, style, fm) else: # The line edit box seems to extend by the space consumed by an 'M'. # So add that to the text text = self.displayText(index.data(Qt.ItemDataRole.DisplayRole), QLocale()) + 'M' srect = style.itemTextRect(fm, editor.geometry(), Qt.AlignmentFlag.AlignLeft, False, text) new_width = srect.width() # Now get the size of the combo/spinner arrows and add them to the needed width if isinstance(editor, (QComboBox, QDateTimeEdit)): r = style.subControlRect(QStyle.ComplexControl.CC_ComboBox, QStyleOptionComboBox(), QStyle.SubControl.SC_ComboBoxArrow, editor) new_width += r.width() elif isinstance(editor, (QSpinBox, QDoubleSpinBox)): r = style.subControlRect(QStyle.ComplexControl.CC_SpinBox, QStyleOptionSpinBox(), QStyle.SubControl.SC_SpinBoxUp, editor) new_width += r.width() # Compute the maximum we can show if we consume the entire viewport pin_view = self.table_widget.pin_view is_pin_view, p = False, editor.parent() while p is not None: if p is pin_view: is_pin_view = True break p = p.parent() if is_pin_view: max_width = pin_view.horizontalScrollBar().geometry().width() else: view = self.table_widget max_width = view.horizontalScrollBar().geometry().width( ) - view.verticalHeader().width() # What we have to display might not fit. If so, adjust down new_width = new_width if new_width < max_width else max_width # See if we need to change the editor's geometry if new_width <= orig_width: delta_x = 0 delta_width = 0 else: # Compute the space available from the left edge of the widget to # the right edge of the displayed table (the viewport) and the left # edge of the widget to the left edge of the viewport. These are # used to position the edit box space_left = initial_geometry.x() space_right = max_width - space_left if editor.layoutDirection() == Qt.LayoutDirection.RightToLeft: # If language is RtL, align to the cell's right edge if possible cw = initial_geometry.width() consume_on_left = min(space_left, new_width - cw) consume_on_right = max(0, new_width - (consume_on_left + cw)) delta_x = -consume_on_left delta_width = consume_on_right else: # If language is LtR, align to the left if possible consume_on_right = min(space_right, new_width) consume_on_left = max(0, new_width - consume_on_right) delta_x = -consume_on_left delta_width = consume_on_right - initial_geometry.width() initial_geometry.adjust(delta_x, 0, delta_width, 0) editor.setGeometry(initial_geometry)