def paintEvent(self, event): painter = QPainter(self) painter.setRenderHints(QPainter.Antialiasing) side = min(self.width(), self.height()) painter.scale(side / 32.0, side / 32.0) painter.setPen(Qt.NoPen) if not self.is_pressed: painter.setBrush(self.back_color) else: painter.setBrush( QColor(self.back_color.red() + 30, self.back_color.green() + 30, self.back_color.blue() + 30)) painter.drawRoundedRect(QRect(0, 0, 32, 32), 8, 8) if self.is_checked: check_path = QPainterPath() check_path.moveTo(self.x1) check_path.lineTo(self.x2) check_path.lineTo(self.x3) pen = QPen(self.check_color, self.check_thick, Qt.SolidLine) painter.setPen(pen) painter.drawPath(check_path)
def paintEvent(self, event): painter = QPainter(self) painter.setRenderHints(QPainter.Antialiasing) if self.label_width <= 0: return painter.setPen(Qt.NoPen) if self.hover: painter.setBrush( QColor(self.back_color.red() + 30, self.back_color.green() + 30, self.back_color.blue() + 30)) else: painter.setBrush(self.back_color) painter.drawRoundedRect(QRect(0, 0, self.width(), self.height()), self.border_radius, self.border_radius) x1 = QPointF(self.label_width + float(self.height() / 3), float(self.height() * 0.45)) x2 = QPointF( self.label_width + float(self.height() * (0.66 + self.rate) / 2), float(self.height() * 0.55)) x3 = QPointF(self.width() - float(self.height() / 3), float(self.height() * 0.45)) check_path = QPainterPath() check_path.moveTo(x1) check_path.lineTo(x2) check_path.lineTo(x3) pen = QPen(self.text_color, self.drop_thick, Qt.SolidLine) painter.setPen(pen) painter.drawPath(check_path)
def paintEvent(self, event): painter = QPainter(self) painter.setRenderHints(QPainter.Antialiasing) painter.setPen(Qt.NoPen) painter.setBrush(self.back_color) painter.drawRoundedRect(QRect(0, 0, self.width(), self.height()), self.border_radius, self.border_radius)
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): """Paint the background, range bar and splitters. Parameters ---------- event : qtpy.QEvent Event from the Qt context. """ painter, w, h = QPainter(self), self.width(), self.height() half_width = self.slider_width / 2 halfdiff = w / 2 - half_width # Background painter.setPen(self.background_color) painter.setBrush(self.background_color) painter.drawRect(halfdiff, 0, self.slider_width, h) # Range Bar painter.setPen(self.bar_color) painter.setBrush(self.bar_color) if self.collapsed: painter.drawRect( halfdiff, h - self.display_max, self.slider_width, self.display_max, ) else: painter.drawRect( halfdiff, h - self.display_max, self.slider_width, self.display_max - self.display_min, ) painter.setRenderHints(QPainter.Antialiasing) # Splitters painter.setPen(self.handle_border_color) painter.setBrush(self.handle_color) painter.drawEllipse( w / 2 - self.handle_radius, h - self.display_min - self.handle_radius, self.handle_width, self.handle_width, ) # upper painter.drawEllipse( w / 2 - self.handle_radius, h - self.display_max - self.handle_radius, self.handle_width, self.handle_width, ) # lower
def paintEvent(self, event): """Paint the background, range bar and splitters. Parameters ---------- event : qtpy.QEvent Event from the Qt context. """ painter, w, h = QPainter(self), self.width(), self.height() half_width = self.slider_width / 2 - 1 halfdiff = int(h / 2 - half_width) # Background painter.setPen(self.background_color) painter.setBrush(self.background_color) painter.drawRoundedRect(0, halfdiff, w, self.slider_width, 2, 2) # Range Bar painter.setPen(self.bar_color) painter.setBrush(self.bar_color) if self.collapsed: painter.drawRect(0, halfdiff, self.display_max, self.slider_width) else: painter.drawRect( self.display_min, halfdiff, self.display_max - self.display_min, self.slider_width, ) painter.setRenderHints(QPainter.Antialiasing) # Splitters painter.setPen(self.handle_border_color) painter.setBrush(self.handle_color) painter.drawEllipse( self.display_min - self.handle_radius, int(h / 2 - self.handle_radius + 1), self.handle_width - 1, self.handle_width - 1, ) # left painter.drawEllipse( self.display_max - self.handle_radius, int(h / 2 - self.handle_radius + 1), self.handle_width - 1, self.handle_width - 1, ) # right
def paintEvent(self, event): painter = QPainter(self) painter.setRenderHints(QPainter.Antialiasing) painter.setPen(Qt.NoPen) if self.hover: painter.setBrush( QColor(self.back_color.red() + 30, self.back_color.green() + 30, self.back_color.blue() + 30)) self.lineEdit.set_back_color( QColor(self.back_color.red() + 30, self.back_color.green() + 30, self.back_color.blue() + 30)) else: painter.setBrush(self.back_color) self.lineEdit.set_back_color(self.back_color) painter.drawRoundedRect(QRect(0, 0, self.width(), self.height()), self.border_radius, self.border_radius) pen = QPen(self.down_arrow_color, self.drop_thick, Qt.SolidLine) painter.setPen(pen) self.down_arrow_poly = QPolygon() x1 = QPoint(self.label_width + float(self.height() / 3), float(self.height() * 0.6)) x2 = QPoint( self.label_width + float(self.height() / 3) + float(self.height() * self.rate / 2), float(self.height() * 0.75)) x3 = QPoint(self.width() - float(self.height() / 3), float(self.height() * 0.6)) self.down_arrow_poly << x1 << x2 << x3 check_path = QPainterPath() check_path.moveTo(x1) check_path.lineTo(x2) check_path.lineTo(x3) check_path.lineTo(x1) painter.drawPath(check_path) if self.down_arrow_color == self.arrow_activate_color: painter.fillPath(check_path, QBrush(self.down_arrow_color)) else: if self.hover: painter.fillPath( check_path, QColor(self.back_color.red() + 30, self.back_color.green() + 30, self.back_color.blue() + 30)) else: painter.fillPath(check_path, QBrush(self.back_color)) pen = QPen(self.up_arrow_color, self.drop_thick, Qt.SolidLine) painter.setPen(pen) x1 = QPoint(self.label_width + float(self.height() / 3), float(self.height() * 0.4)) x2 = QPoint( self.label_width + float(self.height() / 3) + float(self.height() * self.rate / 2), float(self.height() * 0.25)) x3 = QPoint(self.width() - float(self.height() / 3), float(self.height() * 0.4)) self.up_arrow_poly = QPolygon() self.up_arrow_poly << x1 << x2 << x3 check_path = QPainterPath() check_path.moveTo(x1) check_path.lineTo(x2) check_path.lineTo(x3) check_path.lineTo(x1) painter.drawPath(check_path) if self.up_arrow_color == self.arrow_activate_color: painter.fillPath(check_path, QBrush(self.up_arrow_color)) else: if self.hover: painter.fillPath( check_path, QColor(self.back_color.red() + 30, self.back_color.green() + 30, self.back_color.blue() + 30)) else: painter.fillPath(check_path, QBrush(self.back_color))
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)