def paint(self, painter, option, index): option = QStyleOptionViewItem(option) # copy option self.initStyleOption(option, index) style = option.widget.style() if option.widget else QApplication.style() doc = QTextDocument() doc.setHtml(option.text) # painting item without text option.text = "" style.drawControl(QStyle.CE_ItemViewItem, option, painter) ctx = QAbstractTextDocumentLayout.PaintContext() # Hilight text if item is selected if option.state & QStyle.State_Selected: ctx.palette.setColor(QPalette.Text, option.palette.color(QPalette.Active, QPalette.HighlightedText)) textRect = style.subElementRect(QStyle.SE_ItemViewItemText, option) painter.save() painter.translate(textRect.topLeft()) painter.setClipRect(textRect.translated(-textRect.topLeft())) doc.documentLayout().draw(painter, ctx) painter.restore()
def paint(self, painter, option, index): options = QStyleOptionViewItem(option) self.initStyleOption(options, index) self.doc.setHtml(options.text) self.doc.setTextWidth(option.rect.width()) options.text = "" style = QApplication.style() if options.widget is None else options.widget.style() style.drawControl(QStyle.CE_ItemViewItem, options, painter) ctx = QAbstractTextDocumentLayout.PaintContext() if option.state & QStyle.State_Selected: ctx.palette.setColor(QPalette.Text, option.palette.color( QPalette.Active, QPalette.HighlightedText)) else: ctx.palette.setColor(QPalette.Text, option.palette.color( QPalette.Active, QPalette.Text)) text_rect = style.subElementRect(QStyle.SE_ItemViewItemText, options, None) painter.save() painter.translate(text_rect.topLeft()) painter.setClipRect(text_rect.translated(-text_rect.topLeft())) self.doc.documentLayout().draw(painter, ctx) painter.restore()
def paint(self, painter, option, index): painter.save() options = QStyleOptionViewItem(option) self.initStyleOption(options, index) self.doc.setHtml(options.text) options.text = "" # 原字符 style = QApplication.style( ) if options.widget is None else options.widget.style() style.drawControl(QStyle.CE_ItemViewItem, options, painter) ctx = QAbstractTextDocumentLayout.PaintContext() if option.state & QStyle.State_Selected: ctx.palette.setColor( QPalette.Text, option.palette.color(QPalette.Active, QPalette.HighlightedText)) else: ctx.palette.setColor( QPalette.Text, option.palette.color(QPalette.Active, QPalette.Text)) text_rect = style.subElementRect(QStyle.SE_ItemViewItemText, options) the_fuck_your_shit_up_constant = 3 #  ̄へ ̄ # margin = (option.rect.height() - options.fontMetrics.height()) // 2 margin = margin - the_fuck_your_shit_up_constant text_rect.setTop(text_rect.top() + margin) painter.translate(text_rect.topLeft()) painter.setClipRect(text_rect.translated(-text_rect.topLeft())) self.doc.documentLayout().draw(painter, ctx) painter.restore()
def paint(self, painter, option, index): options = QStyleOptionViewItem(option) item = index.data(Qt.UserRole) if isinstance(item, (CommentItem, ChangeItem)): options.decorationAlignment = Qt.AlignHCenter self.initStyleOption(options,index) if options.widget is None: style = QApplication.style() else: style = options.widget.style() doc = QTextDocument() doc.setHtml(options.text) options.text = "" style.drawControl(QStyle.CE_ItemViewItem, options, painter) ctx = QAbstractTextDocumentLayout.PaintContext() # Highlighting text if item is selected #if (optionV4.state & QStyle::State_Selected) #ctx.palette.setColor(QPalette::Text, optionV4.palette.color(QPalette::Active, QPalette::HighlightedText)) textRect = style.subElementRect(QStyle.SE_ItemViewItemText, options) painter.save() painter.translate(textRect.topLeft()) painter.setClipRect(textRect.translated(-textRect.topLeft())) doc.documentLayout().draw(painter, ctx) painter.restore()
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 paint(self, painter, option, index): painter.save() options = QStyleOptionViewItem(option) self.initStyleOption(options, index) self.doc.setPlainText(options.text) column = index.column() if column in self.disasm_columns: options.font.setWeight(QFont.Bold) self.highlight(self.doc, self.disasm_rules) elif column in self.value_columns: options.font.setWeight(QFont.Bold) self.highlight(self.doc, self.value_rules) self.doc.setDefaultFont(options.font) options.text = "" style = (QApplication.style() if options.widget is None else options.widget.style()) style.drawControl(QStyle.CE_ItemViewItem, options, painter) ctx = QAbstractTextDocumentLayout.PaintContext() if option.state & QStyle.State_Selected: ctx.palette.setColor( QPalette.Text, option.palette.color(QPalette.Active, QPalette.HighlightedText), ) else: ctx.palette.setColor( QPalette.Text, option.palette.color(QPalette.Active, QPalette.Text), ) textRect = style.subElementRect(QStyle.SE_ItemViewItemText, options) if index.column() != 0: textRect.adjust(5, 0, 0, 0) the_constant = 4 margin = (option.rect.height() - options.fontMetrics.height()) // 2 margin = margin - the_constant textRect.setTop(textRect.top() + margin) painter.translate(textRect.topLeft()) painter.setClipRect(textRect.translated(-textRect.topLeft())) self.doc.documentLayout().draw(painter, ctx) painter.restore()
def paint(self, painter, opt: QtWidgets.QStyleOptionViewItem, index): self.drawBackground(painter, opt, index) self.drawFocus(painter, opt, opt.rect) try: opt.text = '{0:%d %b %H:%M}'.format( datetime.strptime(index.model().data(index), '%Y-%m-%d %H:%M:%S.%f')) except TypeError: pass opt.font.setItalic(True) opt.backgroundBrush = QtCore.Qt.yellow QtWidgets.QApplication.style().drawControl( QtWidgets.QStyle.CE_ItemViewItem, opt, painter)
def paint(self, painter: QPainter, option, index): # reference following link # https://stackoverflow.com/questions/53353450/how-to-highlight-a-words-in-qtablewidget-from-a-searchlist # https://stackoverflow.com/questions/34623036/implementing-a-delegate-for-wordwrap-in-a-qtreeview-qt-pyside-pyqt painter.save() doc = QTextDocument(self) # option.palette.setColor(QPalette.HighlightedText, QColor.fromRgb(30, 136, 200)) # remove dotted border in table if option.state & QStyle.State_HasFocus: option.state = option.state ^ QStyle.State_HasFocus options = QStyleOptionViewItem(option) self.initStyleOption(options, index) doc.setPlainText(options.text) # keyword = index.data(Qt.UserRole) # if self.keywords: self.keywordHighlight(doc) options.text = "" # print(dir(options.palette)) # print(options.palette.Highlight) style = QApplication.style() if options.widget is None \ else options.widget.style() style.drawControl(QStyle.CE_ItemViewItem, options, painter, options.widget) # for selection highlight ctx = QAbstractTextDocumentLayout.PaintContext() if index.data(Qt.UserRole) == 'reserved': ctx.palette.setColor(QPalette.Text, QColor.fromRgb(100, 100, 100)) doc.setDefaultFont( QFont(option.font.family(), option.font.pointSize() * 2 // 3)) else: ctx.palette.setColor(QPalette.Text, QColor.fromRgb(217, 217, 217)) doc.setDefaultFont(option.font) textRect = option.rect # margin = 4 # textRect.setTop(textRect.top() + margin) textRect.setTop(textRect.top()) painter.translate(textRect.topLeft()) painter.setClipRect(textRect.translated(-textRect.topLeft())) doc.setTextWidth(option.rect.width()) doc.documentLayout().draw(painter, ctx) painter.restore()
def paint(self, painter, option, index): """QStyledItemDelegate.paint implementation """ option.state &= ~QStyle.State_HasFocus # never draw focus rect options = QStyleOptionViewItem(option) self.initStyleOption(options, index) style = QApplication.style( ) if options.widget is None else options.widget.style() doc = QTextDocument() doc.setDocumentMargin(1) doc.setHtml(options.text) if options.widget is not None: doc.setDefaultFont(options.widget.font()) # bad long (multiline) strings processing doc.setTextWidth(options.rect.width()) options.text = "" style.drawControl(QStyle.CE_ItemViewItem, options, painter) ctx = QAbstractTextDocumentLayout.PaintContext() # Highlighting text if item is selected if option.state & QStyle.State_Selected: ctx.palette.setColor( QPalette.Text, option.palette.color(QPalette.Active, QPalette.HighlightedText)) textRect = style.subElementRect(QStyle.SE_ItemViewItemText, options) painter.save() painter.translate(textRect.topLeft()) """Original example contained line painter.setClipRect(textRect.translated(-textRect.topLeft())) but text is drawn clipped with it on kubuntu 12.04 """ doc.documentLayout().draw(painter, ctx) painter.restore()
def paint(self, painter, option, index): """QStyledItemDelegate.paint implementation """ option.state &= ~QStyle.State_HasFocus # never draw focus rect options = QStyleOptionViewItem(option) self.initStyleOption(options,index) style = QApplication.style() if options.widget is None else options.widget.style() doc = QTextDocument() doc.setDocumentMargin(1) doc.setHtml(options.text) if options.widget is not None: doc.setDefaultFont(options.widget.font()) # bad long (multiline) strings processing doc.setTextWidth(options.rect.width()) options.text = "" style.drawControl(QStyle.CE_ItemViewItem, options, painter); ctx = QAbstractTextDocumentLayout.PaintContext() # Highlighting text if item is selected if option.state & QStyle.State_Selected: ctx.palette.setColor(QPalette.Text, option.palette.color(QPalette.Active, QPalette.HighlightedText)) textRect = style.subElementRect(QStyle.SE_ItemViewItemText, options) painter.save() painter.translate(textRect.topLeft()) """Original example contained line painter.setClipRect(textRect.translated(-textRect.topLeft())) but text is drawn clipped with it on kubuntu 12.04 """ doc.documentLayout().draw(painter, ctx) painter.restore()
def paint(self, painter, option, index): """QStyledItemDelegate.paint implementation """ option.state &= ~QStyle.State_HasFocus # never draw focus rect option.state |= QStyle.State_Active # draw fuzzy-open completion as focused, even if focus is on the line edit options = QStyleOptionViewItem(option) self.initStyleOption(options, index) style = QApplication.style() if options.widget is None else options.widget.style() doc = QTextDocument() if self._font is not None: doc.setDefaultFont(self._font) doc.setDocumentMargin(1) doc.setHtml(options.text) # bad long (multiline) strings processing doc.setTextWidth(options.rect.width()) options.text = "" style.drawControl(QStyle.CE_ItemViewItem, options, painter) ctx = QAbstractTextDocumentLayout.PaintContext() # Highlighting text if item is selected if option.state & QStyle.State_Selected: ctx.palette.setColor(QPalette.Text, option.palette.color(QPalette.Active, QPalette.Text)) textRect = style.subElementRect(QStyle.SE_ItemViewItemText, options) painter.save() painter.translate(textRect.topLeft()) painter.setClipRect(textRect.translated(-textRect.topLeft())) doc.documentLayout().draw(painter, ctx) painter.restore()
def paint(self, painter: QPainter, opt: QStyleOptionViewItem, index: QModelIndex): rect = opt.rect self.initStyleOption(opt, index) file_name = str(index.model().data(index.model().index( index.row(), self.file_name_index))) base_file_name = Path(file_name).name font_metrics = QFontMetrics(painter.font()) # Draw correct background opt.text = "" style = opt.widget.style() if opt.widget else QApplication.style() style.drawControl(QStyle.CE_ItemViewItem, opt, painter, opt.widget) cg = QPalette.Normal if opt.state & QStyle.State_Enabled else QPalette.Disabled if cg == QPalette.Normal and not (opt.state & QStyle.State_Active): cg = QPalette.Inactive # # Set pen color # if opt.state & QStyle.State_Selected: # painter.setPen(opt.palette.color(cg, QPalette.HighlightedText)) # else: # painter.setPen(opt.palette.color(cg, QPalette.Text)) if file_name in self.image_cache: img = self.image_cache[file_name] if img and not img.isNull(): painter.drawImage( rect.topLeft(), # QRect(rect.left(), rect.top(), rect.width(), rect.height() - self.title_height), img) else: self.image_cache[file_name] = None worker = ThumbnailWorker(file_name, self.width, self.height) worker.signals.about_image.connect( lambda file_name, image: self._on_about_image( file_name, image, index)) QThreadPool.globalInstance().start(worker) rect_title = QRect(rect.left(), rect.top(), rect.width(), rect.height()) rect_title.setLeft(rect_title.left() + self.title_margin) rect_title.setTop(rect_title.top() + rect_title.height() - self.title_height) rect_title.setRight(rect_title.right() - self.title_margin) painter.setPen(opt.palette.color(cg, QPalette.Text)) elided_text = font_metrics.elidedText( base_file_name, Qt.ElideRight, rect.width() - self.title_margin * 2) painter.drawText(rect_title, Qt.AlignVCenter | Qt.AlignLeft, elided_text) if opt.state & QStyle.State_Selected: painter.fillRect(rect, get_half_alpha(opt.palette.highlight())) elif opt.state & QStyle.State_MouseOver: painter.fillRect(rect, get_half_alpha(opt.palette.midlight())) painter.drawRect(rect)