def paint(self, painter, option, index): ''' This pure abstract function must be reimplemented if you want to provide custom rendering. Use the painter and style option to render the item specified by the item index ''' painter.save() options = QStyleOptionViewItem(option) # get the cell text options.text = index.data() # get the cell rectangle rt = QRect(options.rect) # determines the cell is selected or not flag = False if option.state & QStyle.State_Selected: flag = True # sets back/fore color for selected/non-selected back_color = Qt.white if not flag else QColor('#0087D0') fore_color = Qt.black if not flag else Qt.white # paint the backgrouund painter.fillRect(rt, back_color) # select the pen for draw text painter.setPen(QColor(fore_color)) # make the margin rt.adjust(10, 0, 0, 10) # draw the text painter.drawText(rt, self.m_TextAlign, options.text) painter.restore()
def paint(self, painter_, option_, index_): """ Renders the delegate using the given painter and style option for the item specified by index. """ ref_to_non_const_option = QStyleOptionViewItem(option_) ref_to_non_const_option.showDecorationSelected = False super().paint(painter_, option_, index_)
def paint(self, painter, option, index): """ Renders the delegate using the given painter and style option for the item specified by index. """ # If the index is invalid we have nothing to draw if not index.isValid(): return options = QStyleOptionViewItem(option) self.initStyleOption(options, index) painter.save() # sometimes a paint event occurs after a element is removed selector static selection # but before qt list widget gets updated. This will cause isMissing and isFilteredOut to fail. # early out when this happens if options.text not in self._selector.staticSelection: return missing = self._selector.staticSelection.isMissing(options.text) filteredOut = self._selector.staticSelection.isFilteredOut( options.text) doc = QTextDocument() html = '''{0}'''.format(options.text) if filteredOut: html = '''<i>''' + html + '''</i>''' if missing: html = '''<s>''' + html + '''</s>''' doc.setHtml(html) options.text = "" #if options.widget is not None: QApplication.style().drawControl(QStyle.CE_ItemViewItem, options, painter) #Shift text right to make icon visible iconSize = options.icon.actualSize( options.rect.size()) if options.icon is not None else QSize(0, 0) painter.translate(options.rect.left() + iconSize.width(), options.rect.top()) clip = QRect(0, 0, options.rect.width() + iconSize.width(), options.rect.height()) painter.setClipRect(clip) ctx = QAbstractTextDocumentLayout.PaintContext() #set text color to red for selected item if missing or filteredOut: ctx.palette.setColor(QPalette.Text, QColor(85, 85, 85)) ctx.clip = clip doc.documentLayout().draw(painter, ctx) painter.restore()
def paint(self, painter: QPainter, option: QStyleOptionViewItem, index: QModelIndex): # let the standard delegate handle painting the non payload columns for now if index.column() not in (1, 2): super().paint(painter, option, index) return index = self.proxy.mapToSource(index) # setup option self.initStyleOption(option, index) # save the painter state and get the style object painter.save() style = option.widget.style() qfm = painter.fontMetrics() if index.column() == 1: # display sender ids in hex option.text = hex(int(option.text))[2:].upper() style.drawControl(QStyle.CE_ItemViewItem, option, painter) else: # print payload with highlights # get the frames highlighting info if self.proxy.highlightMode and index.row( ) in self.model.highlight_dict: highlight_bytes = self.model.highlight_dict[index.row()] else: highlight_bytes = set() # get the full payload from the option, then print it bytewise full_payload = option.text payload_length = len(full_payload) byte_counter = 0 painter.setBackground(QBrush(Qt.cyan)) for i in range(0, payload_length - 1, 2): if byte_counter in highlight_bytes: painter.setBackgroundMode(Qt.OpaqueMode) else: painter.setBackgroundMode(Qt.TransparentMode) option.text = full_payload[i:i + 2].upper() style.drawControl(QStyle.CE_ItemViewItem, option, painter) painter.translate(qfm.horizontalAdvance("DD ", 3), 0) byte_counter += 1 # restore the painter state painter.restore()
def paint(self, painter: QPainter, option: QStyleOptionViewItem, index: QModelIndex): # Draw the item regularly first, and then redraw it as if it was not # selected. That way, we get the selection highlight, but it does # not affect the color of the decoration. super().paint(painter, option, index) option.state = option.state & ~QStyle.State_Selected super().paint(painter, option, index)
def sizeHint(self, option, index): options = QStyleOptionViewItem(option) self.initStyleOption(options, index) doc = QTextDocument() doc.setHtml(options.text) doc.setTextWidth(options.rect.width()) return QSize(doc.idealWidth(), doc.size().height())
def paint(self, painter, option, index): if index.column() == 2: value = index.model().data(index, Qt.UserRole) if not self.is_supported_type(value): my_option = QStyleOptionViewItem(option) my_option.state &= ~QStyle.State_Enabled super(VariantDelegate, self).paint(painter, my_option, index) return super(VariantDelegate, self).paint(painter, option, index)
def testIntDelegate(self): """PYSIDE-1250: When creating a QVariant, use int instead of long long for anything that fits into a int. Verify by checking that a spin box is created as item view editor for int.""" item = QStandardItem() item.setData(123123, Qt.EditRole) # <-- QVariant conversion here model = QStandardItemModel() model.appendRow(item) style_option = QStyleOptionViewItem() delegate = QStyledItemDelegate() editor = delegate.createEditor(None, style_option, model.index(0, 0)) self.assertEqual(type(editor), QSpinBox)
def paint(self, painter: QPainter, option: QStyleOptionViewItem, index: QModelIndex) -> None: itemOption = QStyleOptionViewItem(option) # disable focus outline if itemOption.state & QStyle.State_HasFocus: itemOption.state ^= QStyle.State_HasFocus # hover whole row if index.row() == option.styleObject.hoverIndexRow: itemOption.state |= QStyle.State_MouseOver super().paint(painter, itemOption, index) # draw lines around numeric columns if index.column() in (5, 11): oldpen = painter.pen() painter.setPen(self.linepen) painter.drawLine( itemOption.rect.topRight() + QPoint(0, 0), itemOption.rect.bottomRight() + QPoint(0, 0) ) painter.setPen(oldpen)
def initStyleOption(self, option: QStyleOptionViewItem, index: QModelIndex): super().initStyleOption(option, index) if index.siblingAtRow(index.row() + 1).isValid(): # If we have a color, show it as decoration. Otherwise, show a cross. pixmap = QPixmap(option.decorationSize) color = index.data(Qt.DecorationRole) if color: pixmap.fill(color) else: pixmap.fill(Qt.transparent) painter = QPainter(pixmap) painter.drawLine(0, 0, pixmap.width() - 1, pixmap.height() - 1) painter.drawLine(0, pixmap.height() - 1, pixmap.width() - 1, 0) painter.end() option.icon = QIcon(pixmap) # Always show the decoration option.features |= QStyleOptionViewItem.HasDecoration else: option.features &= ~QStyleOptionViewItem.HasDecoration
def updateEditorGeometry(self, editor: QWidget, option: QStyleOptionViewItem, index: QModelIndex) -> None: itemOption = QStyleOptionViewItem(option) # set size of editor to size of cell geom: QRect = QApplication.style().subElementRect(QStyle.SE_ItemViewItemText, itemOption, editor) geom.setTop(geom.top()) editor.setGeometry(geom)
def paint(self, painter: QtGui.QPainter, option: QtWidgets.QStyleOptionViewItem, index): if option.state & QtWidgets.QStyle.State_HasFocus: option.state = option.state ^ QtWidgets.QStyle.State_HasFocus super().paint(painter, option, index)
def paint(self, painter, option, index): options = QStyleOptionViewItem(option) style = options.widget.style() style.drawControl(QStyle.CE_ItemViewItem, options, painter) painter.save() rectangle = option.rect if option.state & QStyle.State_Selected: pin = QImage(AssetsMapper.PINNED_SELECTED.value) painter.fillRect(rectangle, QBrush(QColor(Color.ITEM_SELECTED.value))) else: pin = QImage(AssetsMapper.PINNED.value) rectangle.setX(10) pin.setDevicePixelRatio(2.0) font = painter.font() font.setPixelSize(Style.FONT_SIZE.value) painter.setFont(font) pin_rect = copy.copy(rectangle) pin_rect.setHeight(32) pin_rect.setWidth(32) yoffset = 42 # Draw title title = index.data(ItemModel.TitleRole) pinned = index.data(ItemModel.PinRole) pin_point = rectangle.topLeft() pin_point.setY(pin_point.y() + 32) pin_rect.setTopLeft(pin_point) point = option.rect.topLeft() point.setY(point.y() - 40) rectangle.setTopLeft(point) pin_point.setY(pin_point.y() - 20) pin_rect.setTopLeft(pin_point) pin_rect.setHeight(16) pin_rect.setWidth(16) if pinned: painter.drawImage(pin_rect, pin) title = f' {title}' painter.setPen(QColor(Color.TITLE.value)) painter.drawText(rectangle, Qt.AlignVCenter, title) # Draw company company = index.data(ItemModel.CompanyRole) point = option.rect.topLeft() point.setY(point.y() + yoffset) rectangle.setTopLeft(point) font.setPixelSize(Style.FONT_SIZE.value) painter.setFont(font) painter.setPen(QColor(Color.COMPANY.value)) painter.drawText(rectangle, Qt.AlignVCenter, company) # Draw location location = index.data(ItemModel.LocationRole) point = option.rect.topLeft() point.setY(point.y() + yoffset) rectangle.setTopLeft(point) font.setPixelSize(Style.FONT_SIZE.value - 2) painter.setFont(font) painter.setPen(QColor(Color.LOCATION.value)) painter.drawText(rectangle, Qt.AlignVCenter, location) painter.restore()