def paintEvent(self, event): """Override Qt method""" painter = QPainter(self) painter.setRenderHint(QPainter.Antialiasing) # Decoration painter.fillPath(self.path_current, QBrush(self.color)) painter.strokePath(self.path_decoration, QPen(self.color_decoration, self.stroke_decoration))
def paint(self, painter: QtGui.QPainter, option: 'QStyleOptionViewItem', index: QtCore.QModelIndex) -> None: viewOption = QStyleOptionViewItem(option) self.initStyleOption(viewOption, index) QStyledItemDelegate.paint(self, painter, viewOption, index) item = index.model().data(index, Qt.UserRole) if isinstance(item, QVariant): return value = item['value'] type = item['type'] if type == 'checkbox': # 数据转换 value = True if value == 1 else 0 # 绘制单选框 checkBoxStyle = QStyleOptionButton() checkBoxStyle.state = QStyle.State_On if value else QStyle.State_Off checkBoxStyle.state |= QStyle.State_Enabled # 计算位置 size = item['size'] rect = calculate_middle_rect(option.rect, size, size) checkBoxStyle.rect = rect checkBox = QCheckBox() QApplication.style().drawPrimitive(QStyle.PE_IndicatorCheckBox, checkBoxStyle, painter, checkBox) if type == 'tag': painter.setRenderHint(QPainter.Antialiasing, True) painter.setRenderHints(QPainter.SmoothPixmapTransform) path = QPainterPath() # 绘制文本 self.font = ResourceLoader().qt_font_text_tag text_color = item['text_color'] border_color = item['border_color'] text = value padding_v = 2 padding_h = 4 border_radius = 2 border_width = 1 painter.setFont(item['font']) painter.setPen(text_color) fm = QFontMetrics(painter.font()) w = fm.width(text) h = fm.height() # 计算位置 rect = calculate_middle_rect(option.rect, w + padding_h * 2, h + padding_v * 2) rectf = QRectF(rect.x(), rect.y(), rect.width(), rect.height()) painter.drawText( QRect(rectf.x() + padding_h, rectf.y() + padding_v, w, h), Qt.TextWordWrap, text) # 绘制边框 path.addRoundedRect(rectf, border_radius, border_radius) painter.strokePath(path, QPen(border_color, border_width))
def paintEvent(self, event): """ """ self.build_paths() painter = QPainter(self) painter.setRenderHint(QPainter.Antialiasing) painter.fillPath(self.round_rect_path, self.color_back) painter.fillPath(self.top_rect_path, self.color_top) painter.strokePath(self.round_rect_path, QPen(Qt.gray, 1))
def paint(self, painter: QtGui.QPainter, option: 'QStyleOptionViewItem', index: QtCore.QModelIndex) -> None: if not index.isValid(): return if option.state & QStyle.State_Selected: pass if option.state & QStyle.State_MouseOver: pass item = index.model().data(index, Qt.UserRole) if isinstance(item, QVariant): return painter.setRenderHint(QPainter.Antialiasing, True) painter.setRenderHints(QPainter.SmoothPixmapTransform) value = item['value'] type = item['type'] x = option.rect.x() y = option.rect.y() w = option.rect.width() h = option.rect.height() if type == 'device_activity': painter.save() # 图标 icon = QImage(ResourceLoader().icon_path(value['icon'])) rect_icon = QRectF(x + 20, y + 13, 44, 44) painter.drawImage(rect_icon, icon) # 设备标签 painter.setFont(ResourceLoader().qt_font_text_xs) rect = calculate_text_rect('设备', painter=painter, x=x + 70, y=y + 15) painter.drawText(rect, Qt.TextSingleLine, '设备') # 设备编号 painter.setPen(ResourceLoader().qt_color_label_link) painter.setFont(ResourceLoader().qt_font_text_xs) rect = calculate_text_rect(value['device'], painter=painter, x=x + 110, y=y + 15) painter.drawText(rect, Qt.TextSingleLine, value['device']) # 活动内容 painter.setPen(ResourceLoader().qt_color_sub_text) painter.setFont(ResourceLoader().qt_font_text_xss) content = value['content'] + ',' + toDateStr() rect = calculate_text_rect(content, painter=painter, x=x + 70, y=y + 40) painter.drawText(rect, Qt.TextSingleLine, content) # 绘制边框 path = QPainterPath() path.addRoundedRect(QRectF(x + 4, y + 4, w - 8, h - 8), 4, 4) painter.strokePath(path, QPen(ResourceLoader().qt_color_background, 1)) painter.restore() elif type == 'load_more': # 绘制加载图标 # painter.save() # painter.translate(x+16, y+16) # self.loading_rotate += 5 # painter.rotate(self.loading_rotate%360) # icon = qtawesome.icon('mdi.loading', color=ResourceLoader().qt_color_sub_text) # icon_pixmap = icon.pixmap(QSize(32, 32)) # painter.drawPixmap(QRect(-16, -16, 32, 32), icon_pixmap) # painter.restore() # 绘制加载信息 painter.save() painter.setFont(ResourceLoader().qt_font_text_xs) painter.setPen(ResourceLoader().qt_color_sub_text) _rect = calculate_text_rect('~~~ 正在努力加载 ~~~', painter=painter) rect = calculate_middle_rect(option.rect, width=_rect.width(), height=_rect.height()) painter.drawText(rect, Qt.TextSingleLine, '~~~ 正在努力加载 ~~~') painter.restore() elif type == 'no_more': painter.save() painter.setFont(ResourceLoader().qt_font_text_xs) painter.setPen(ResourceLoader().qt_color_sub_text) _rect = calculate_text_rect('--- 我是有底线的 ---', painter=painter) rect = calculate_middle_rect(option.rect, width=_rect.width(), height=_rect.height()) painter.drawText(rect, Qt.TextSingleLine, '--- 我是有底线的 ---') painter.restore() else: self.initStyleOption(option, index) QStyledItemDelegate.paint(self, painter, option, index)