def drawPercent(self, painter: QPainter) -> None: painter.save() # 百分比选择框的区域要小于边框宽度,同时偏移一点,是的看起来美观 offset: int = self.__borderWidth + 2 y: int = int(self.__rightHeight * (1 - self.__percent / 100)) # 禁用状态颜色要变暗 pen: QPen = QPen() pen.setWidthF(self.__percentBorder) pen.setColor(self.__percentColor if self.isEnabled() else self. __disableColor.name()) painter.setPen(pen) painter.setBrush(QColor(0, 0, 0, 50)) rect: QRect = QRect(offset, y + offset, self.width() - offset * 2, self.__percentHeight) painter.drawRoundedRect(rect, self.__percentRadius, self.__percentRadius) textFont: QFont = QFont() textFont.setPixelSize(rect.width() // 3) painter.setFont(textFont) if self.__showValue: painter.drawText(rect, Qt.AlignCenter, '%d' % round(self.__value)) else: painter.drawText(rect, Qt.AlignCenter, '%d%%' % round(self.__percent)) painter.restore()
def paintEvent(self, event): rect = QRect(10, 20, 80, 60) path = QPainterPath() path.moveTo(20, 80) path.lineTo(20, 30) path.cubicTo(80, 0, 50, 50, 80, 80) startAngle = 30 * 16 arcLength = 120 * 16 painter = QPainter(self) painter.setPen(self.pen) painter.setBrush(self.brush) if self.antialiased: painter.setRenderHint(QPainter.Antialiasing) for x in range(0, self.width(), 100): for y in range(0, self.height(), 100): painter.save() painter.translate(x, y) if self.transformed: painter.translate(50, 50) painter.rotate(60.0) painter.scale(0.6, 0.9) painter.translate(-50, -50) if self.shape == RenderArea.Line: painter.drawLine(rect.bottomLeft(), rect.topRight()) elif self.shape == RenderArea.Points: painter.drawPoints(RenderArea.points) elif self.shape == RenderArea.Polyline: painter.drawPolyline(RenderArea.points) elif self.shape == RenderArea.Polygon: painter.drawPolygon(RenderArea.points) elif self.shape == RenderArea.Rect: painter.drawRect(rect) elif self.shape == RenderArea.RoundedRect: painter.drawRoundedRect(rect, 25, 25, Qt.RelativeSize) elif self.shape == RenderArea.Ellipse: painter.drawEllipse(rect) elif self.shape == RenderArea.Arc: painter.drawArc(rect, startAngle, arcLength) elif self.shape == RenderArea.Chord: painter.drawChord(rect, startAngle, arcLength) elif self.shape == RenderArea.Pie: painter.drawPie(rect, startAngle, arcLength) elif self.shape == RenderArea.Path: painter.drawPath(path) elif self.shape == RenderArea.Text: painter.drawText(rect, Qt.AlignCenter, "PySide 2\nQt %s" % qVersion()) elif self.shape == RenderArea.Pixmap: painter.drawPixmap(10, 10, self.pixmap) painter.restore() painter.setPen(self.palette().dark().color()) painter.setBrush(Qt.NoBrush) painter.drawRect(QRect(0, 0, self.width() - 1, self.height() - 1))
def drawBg(self, painter: QPainter) -> None: painter.save() width: int = self.width() height: int = self.height() side: int = min(width, height) pen: QPen = QPen() pen.setWidth(self.__borderWidth) pen.setColor(self.__borderColor) painter.setPen(pen if self.__borderWidth > 0 else Qt.NoPen) painter.setBrush(self.bgColor) rect: QRect = QRect( ((width - self.__tempWidth) // 2) + self.__borderWidth, self.__borderWidth, self.__tempWidth - (self.__borderWidth * 2), height - (self.__borderWidth * 2)) painter.drawRoundedRect(rect, self.__borderRadius, self.__borderRadius) font: QFont = QFont() font.setPixelSize(side - 18) painter.setFont(font) painter.setPen(self.__lineColor) painter.drawText(rect, Qt.AlignCenter, "完成" if self.__status == 2 else "开始") painter.restore()
def paintEvent(self, arg__1: QPaintEvent): # super(Push, self).paintEvent(arg__1) # lightgrey painter = QPainter() painter.begin(self) pen = QPen(QColor(162, 181, 205), 2, Qt.SolidLine) painter.setBrush(QBrush(QColor(255, 255, 255))) painter.setPen(pen) painter.setRenderHint(QPainter.Antialiasing, True) rect = QRect() rect.moveTo(1, 10) rect.setWidth(140) rect.setHeight(140) painter.drawRoundedRect(rect, 5, 5) painter.drawConvexPolygon( [QPoint(60, 10), QPoint(80, 10), QPoint(70, 1)]) pen.setColor(QColor(255, 255, 255)) painter.setPen(pen) painter.drawLine(63, 10, 77, 10) pen.setWidth(1) painter.setPen(pen) painter.drawPoints([QPoint(61, 10), QPoint(78, 10)]) painter.end()
def group_object_pixmap(self, object_class_name): if object_class_name in self.group_obj_pixmap_cache: return self.group_obj_pixmap_cache[object_class_name] object_pixmap = self.object_pixmap(object_class_name) size = object_pixmap.size() width, height = size.width(), size.height() radius = width / 8 pen_width = width / 32 margin = width / 16 pen = QPen(QApplication.palette().shadow().color()) pen.setWidth(pen_width) path = QPainterPath() path.addRoundedRect(0, 0, width, height, radius, radius) pixmap = QPixmap(size) pixmap.fill(Qt.transparent) painter = QPainter(pixmap) painter.setRenderHint(QPainter.Antialiasing, True) painter.fillPath(path, QApplication.palette().window()) painter.setPen(pen) painter.drawRoundedRect(pixmap.rect().adjusted(pen_width, pen_width, -pen_width, -pen_width), radius, radius) painter.drawPixmap( pixmap.rect().adjusted(margin, margin, -width / 2, -height / 2), object_pixmap, object_pixmap.rect() ) painter.drawPixmap( pixmap.rect().adjusted(width / 2, margin, -margin, -height / 2), object_pixmap, object_pixmap.rect() ) painter.drawPixmap( pixmap.rect().adjusted(width / 2, height / 2, -margin, -margin), object_pixmap, object_pixmap.rect() ) painter.drawPixmap( pixmap.rect().adjusted(margin, height / 2, -width / 2, -margin), object_pixmap, object_pixmap.rect() ) painter.end() self.group_obj_pixmap_cache[object_class_name] = pixmap return pixmap
def _draw_item_background(self, p: QPainter): """ 绘制项目的背景 :param p: 画刷 :return: None """ if self._m_start_rect.isNull(): return p.save() lgt = QLinearGradient(self._m_start_rect.topLeft(), self._m_start_rect.bottomRight()) lgt.setColorAt(0.0, self._m_item_start_color) lgt.setColorAt(1.0, self._m_item_end_color) p.setPen(Qt.NoPen) p.setBrush(lgt) p.drawRoundedRect(self._m_start_rect, self._m_item_radius, self._m_item_radius) # 绘制 hover 状态下的item if self._m_current_hover_index != -1: hover_rect = QRectF(self._m_item_maps[self._m_current_hover_index][1]) lgt = QLinearGradient(hover_rect.topLeft(), hover_rect.bottomRight()) lgt.setColorAt(0.0, self._m_item_hover_start_color) lgt.setColorAt(1.0, self._m_item_hover_end_color) p.setPen(Qt.NoPen) p.setBrush(lgt) p.drawRoundedRect(hover_rect, self._m_item_radius, self._m_item_radius) p.restore()
def paint(self, painter: QPainter): brush = QBrush(QColor("#007430")) painter.setBrush(brush) painter.setPen(Qt.NoPen) painter.setRenderHint(QPainter.Antialiasing) itemSize = self.size() painter.drawRoundedRect(0, 0, itemSize.width(), itemSize.height() - 10, 10, 10) if self.rightAligned: points = [ QPointF(itemSize.width() - 10.0, itemSize.height() - 10.0), QPointF(itemSize.width() - 20.0, itemSize.height()), QPointF(itemSize.width() - 30.0, itemSize.height() - 10.0), ] else: points = [ QPointF(10.0, itemSize.height() - 10.0), QPointF(20.0, itemSize.height()), QPointF(30.0, itemSize.height() - 10.0), ] painter.drawConvexPolygon(points)
def drawBg(self, painter: QPainter) -> None: painter.save() # 设置边框颜色及宽度 pen: QPen = QPen() pen.setColor(self.__borderColor) pen.setWidthF(self.__borderWidth) painter.setPen(pen) # 绘制区域要减去边框宽度 rect: QRect = QRect() rect.setX(self.__borderWidth) rect.setY(self.__borderWidth) rect.setWidth(self.width() - self.__borderWidth * 2) rect.setHeight(self.height() - self.__borderWidth * 2) # 如果背景图片存在则显示背景图片,否则显示背景色 if not self.__bgImage.isNull(): # 等比例缩放绘制 img: QPixmap = self.__bgImage.scaled(rect.width(), rect.height(), Qt.KeepAspectRatio, Qt.SmoothTransformation) painter.drawPixmap((self.rect().width() - img.width()) / 2, (self.rect().height() - img.height()) / 2, img) else: if self.__colorMode == ColorButton.ColorMode.ColorMode_Normal: if self.__isPressed: painter.setBrush(QBrush(self.__pressedColor)) else: painter.setBrush(QBrush(self.__normalColor)) elif self.__colorMode == ColorButton.ColorMode.ColorMode_Replace: gradient: QLinearGradient = QLinearGradient(QPoint(0, 0), QPoint(0, self.height())) if self.__isPressed: gradient.setColorAt(0.0, self.__pressedColor) gradient.setColorAt(0.49, self.__pressedColor) gradient.setColorAt(0.50, self.__normalColor) gradient.setColorAt(1.0, self.__normalColor) else: gradient.setColorAt(0.0, self.__normalColor) gradient.setColorAt(0.49, self.__normalColor) gradient.setColorAt(0.50, self.__pressedColor) gradient.setColorAt(1.0, self.__pressedColor) painter.setBrush(gradient) elif self.__colorMode == ColorButton.ColorMode.ColorMode_Shade: gradient: QLinearGradient = QLinearGradient(QPoint(0, 0), QPoint(0, self.height())) if self.__isPressed: gradient.setColorAt(0.0, self.__pressedColor) gradient.setColorAt(1.0, self.__normalColor) else: gradient.setColorAt(0.0, self.__normalColor) gradient.setColorAt(1.0, self.__pressedColor) painter.setBrush(gradient) painter.drawRoundedRect(rect, self.__borderRadius, self.__borderRadius) painter.restore()
def drawBg(self, painter: QPainter) -> None: """ 绘制背景色 """ painter.save() painter.setPen(Qt.NoPen) bgGradient: QLinearGradient = QLinearGradient(QPoint(0, 0), QPoint(0, self.height())) bgGradient.setColorAt(0.0, self.__bgColorStart) bgGradient.setColorAt(1.0, self.__bgColorEnd) painter.setBrush(bgGradient) painter.drawRoundedRect(self.rect(), self.__bgRadius, self.__bgRadius) painter.restore()
def drawBg(self, painter: QPainter) -> None: painter.save() painter.setPen(Qt.NoPen) painter.setBrush(self.__bgColor) rect: QRect = QRect(self.__padding, self.height() // 3 * 2, self.width() - self.__padding * 2, self.height() // 3) painter.drawRoundedRect(rect, self.__radius, self.__radius) painter.restore()
def drawBar(self, painter: QPainter) -> None: """ 绘制当前条目选中背景 """ painter.save() pen: QPen = QPen() # PySide2.QtGui.QPen barGradient: QLinearGradient = QLinearGradient( self.__barRect.topLeft(), self.__barRect.bottomLeft()) barGradient.setColorAt(0.0, self.__barColorStart) barGradient.setColorAt(1.0, self.__barColorEnd) painter.setBrush(barGradient) if self.barStyle == NavBar.BarStyle.BARSTYLE_RECT: painter.setPen(Qt.NoPen) painter.drawRoundedRect(self.__barRect, self.__barRadius, self.__barRadius) painter.restore() return else: pen.setWidthF(self.__lineWidth) pen.setBrush(barGradient) painter.setPen(pen) painter.drawRoundedRect(self.__barRect, self.__barRadius, self.__barRadius) pen.setColor(self.__lineColor) painter.setPen(pen) offset: Decimal = Decimal(self.__lineWidth) / 2 if self.__barStyle == NavBar.BarStyle.BARSTYLE_LINE_TOP: painter.drawLine(int(self.__barRect.left()), self.__barRect.top() + offset, int(self.__barRect.right()), self.__barRect.top() + offset) elif self.__barStyle == NavBar.BarStyle.BARSTYLE_LINE_TOP: painter.drawLine(self.__barRect.right() - offset, int(self.__barRect.top()), self.__barRect.right() - offset, int(self.__barRect.bottom())) elif self.__barStyle == NavBar.BarStyle.BARSTYLE_LINE_TOP: painter.drawLine(int(self.__barRect.left()), self.__barRect.bottom() - offset, int(self.__barRect.right()), self.__barRect.bottom() - offset) elif self.__barStyle == NavBar.BarStyle.BARSTYLE_LINE_TOP: painter.drawLine(self.__barRect.left() + offset, int(self.__barRect.top()), self.__barRect.left() + offset, int(self.__barRect.bottom())) # 这里还可以增加右侧倒三角型 painter.restore()
def paintEvent(self, event): """Paint the background, range bar and splitters. Parameters ---------- event : PySide2.QEvent Event from the Qt context. """ painter, w, h = QPainter(self), self.width(), self.height() half_width = self.slider_width / 2 halfdiff = int(w / 2 - half_width) # Background painter.setPen(self.background_color) painter.setBrush(self.background_color) painter.drawRoundedRect(halfdiff, 0, self.slider_width, h, 2, 2) # 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( int(w / 2 - self.handle_radius), h - self.display_min - self.handle_radius, self.handle_width, self.handle_width, ) # upper painter.drawEllipse( int(w / 2 - self.handle_radius), h - self.display_max - self.handle_radius, self.handle_width, self.handle_width, ) # lower
def paintEvent(self, event): # pylint: disable=invalid-name, unused-argument p = QPainter(self) p.setRenderHint(QPainter.Antialiasing, True) p.setPen(Qt.NoPen) track_opacity = self._track_opacity thumb_opacity = 1.0 text_opacity = 1.0 if self.isEnabled(): track_brush = self._track_color[self.isChecked()] thumb_brush = self._thumb_color[self.isChecked()] text_color = self._text_color[self.isChecked()] else: track_opacity *= 0.8 track_brush = self.palette().shadow() thumb_brush = self.palette().mid() text_color = self.palette().shadow().color() p.setBrush(track_brush) p.setOpacity(track_opacity) p.drawRoundedRect( self._margin, self._margin, self.width() - 2 * self._margin, self.height() - 2 * self._margin, self._track_radius, self._track_radius, ) p.setBrush(thumb_brush) p.setOpacity(thumb_opacity) p.drawEllipse( self.offset - self._thumb_radius, self._base_offset - self._thumb_radius, 2 * self._thumb_radius, 2 * self._thumb_radius, ) p.setPen(text_color) p.setOpacity(text_opacity) font = p.font() font.setPixelSize(1.5 * self._thumb_radius) p.setFont(font) p.drawText( QRectF( self.offset - self._thumb_radius, self._base_offset - self._thumb_radius, 2 * self._thumb_radius, 2 * self._thumb_radius, ), Qt.AlignCenter, self._thumb_text[self.isChecked()], )
def _draw_bar_background(self, p: QPainter): """ 绘制导航栏的背景 :param p: 画刷 :return: None """ p.save() p.setPen(Qt.NoPen) lgt = QLinearGradient(QPointF(0, 0), QPointF(0, self.height())) lgt.setColorAt(0.0, self._m_bar_start_color) lgt.setColorAt(1.0, self._m_bar_end_color) p.setBrush(lgt) p.drawRoundedRect(self.rect(), self._m_bar_radius, self._m_bar_radius) p.restore()
def paintEvent(self, event): s = self.size() qp = QPainter() qp.begin(self) qp.setRenderHint(QPainter.Antialiasing, True) pen = QPen(Qt.NoPen) qp.setPen(pen) qp.setBrush(QColor(154, 205, 50)) if self.__enabled: qp.setBrush(QColor(154, 190, 50)) qp.drawRoundedRect(0, 0, s.width(), s.height(), 10, 10) lg = QLinearGradient(0, 25, 70, 0) lg.setColorAt(0, QColor(154, 184, 50)) lg.setColorAt(0.35, QColor(154, 210, 50)) lg.setColorAt(0.85, QColor(154, 184, 50)) qp.setBrush(lg) qp.drawRoundedRect(1, 1, s.width() - 2, s.height() - 2, 8, 8) else: qp.setBrush(QColor(150, 150, 150)) qp.drawRoundedRect(0, 0, s.width(), s.height(), 10, 10) lg = QLinearGradient(5, 25, 60, 0) lg.setColorAt(0, QColor(190, 190, 190)) lg.setColorAt(0.35, QColor(230, 230, 230)) lg.setColorAt(0.85, QColor(190, 190, 190)) qp.setBrush(lg) qp.drawRoundedRect(1, 1, s.width() - 2, s.height() - 2, 8, 8) qp.end()
def drawCurrentBg_ZFB(self, painter: QPainter) -> None: painter.save() # 圆半径为高度一定比例,计算宽度,将宽度等分 width: int = self.width() // self.__maxStep height: int = self.height() // 3 radius: int = height // 3 initX: int = width // 2 initY: int = self.height() // 2 # 绘制当前圆 painter.setPen(Qt.NoPen) painter.setBrush(self.__currentBackground) for i in range(self.__currentStep - 1): initX += width painter.drawEllipse(QPoint(initX, initY), radius, radius) initX = initX - width // 4 initY = 0 height = self.height() // 4 # 绘制当前上部提示信息背景 bgRect: QRect = QRect(initX, initY, width // 2, height) painter.setBrush(self.__currentBackground) painter.drawRoundedRect(bgRect, height / 2, height / 2) # 绘制当前上部提示信息 font: QFont = QFont() font.setPixelSize(height / 1.9) font.setBold(True) painter.setFont(font) painter.setPen(self.__currentForeground) painter.drawText(bgRect, Qt.AlignCenter, self.__topInfo[self.__currentStep - 1]) # 绘制倒三角 centerX: int = initX + width // 4 offset: int = 10 pts: QPolygon = QPolygon() pts.append(QPoint(centerX - offset, height)) pts.append(QPoint(centerX + offset, height)) pts.append(QPoint(centerX, height + offset)) painter.setPen(Qt.NoPen) painter.drawPolygon(pts) painter.restore()
def paint(self, painter: QPainter, option: QStyleOptionGraphicsItem, index: QWidget) -> None: """ Paint the graphics of the action wrapper including action name, number, and ports. :param painter: This draws the widget. :type painter: QPainter :param option: Option for the style of graphic. :type option: QStyleOptionGraphicsItem :param index: Index for the painted graphic. :type index: QWidget :return: None :rtype: NoneType """ ActionGraphics.paint(self, painter, option, index) # Get dimensions of the action x, y, width, height = self.getActionRect(self._action.getInputPorts(), self._action.getOutputPorts()) # Draw the number tag. number = str( self._action.getParent().getActions().index(self._action) + 1) offset = 5 radius = 15 size = ActionGraphics.H_SPACE / 2 - offset * 2 painter.setBrush(QColor(29, 110, 37)) painter.drawRoundedRect(QRectF(x + offset, y + offset, size, size), radius, radius) painter.setPen(ActionWrapperGraphics.TAG_TEXT_COLOR) painter.setBrush(ActionWrapperGraphics.TAG_TEXT_COLOR) painter.setFont(ActionWrapperGraphics.TAG_FONT) fm = QFontMetricsF(ActionWrapperGraphics.TAG_FONT) pixelsWide = fm.width(number) pixelsHigh = fm.height() # TODO: fix text positioning - font metrics aren't working well painter.drawText(x + offset + size / 2 - pixelsWide, y + offset + size / 2 + pixelsHigh / 2, number) # Draw the name of the action painter.setPen(ActionWrapperGraphics.NAME_TEXT_COLOR) painter.setBrush(ActionWrapperGraphics.NAME_TEXT_COLOR) painter.setFont(ActionWrapperGraphics.NAME_FONT) fm = QFontMetricsF(ActionWrapperGraphics.NAME_FONT) br = fm.boundingRect(self._action.getName()) # TODO: fix text positioning - font metrics aren't working well t = fm.elidedText(self._action.getName(), Qt.ElideRight, self._width - offset * 2) painter.drawText(x + offset, br.height(), t)
def drawBg(self, painter: QPainter) -> None: width: int = self.width() height: int = self.height() endX: int = width - self.__arrowSize endY: int = height - self.__arrowSize painter.save() painter.setPen(Qt.NoPen) painter.setBrush(self.__background) pts: QPolygon = QPolygon() if self.__arrowPosition == NavLabel.ArrowPosition.ArrowPosition_Right: self.__bgRect = QRect(0, 0, endX, height) pts.append(QPoint(endX, int(height / 2 - self.__arrowSize))) pts.append(QPoint(endX, int(height / 2 + self.__arrowSize))) pts.append(QPoint(width, int(height / 2))) elif self.__arrowPosition == NavLabel.ArrowPosition.ArrowPosition_Left: self.__bgRect = QRect(self.__arrowSize, 0, width - self.__arrowSize, height) pts.append( QPoint(self.__arrowSize, int(height / 2 - self.__arrowSize))) pts.append( QPoint(self.__arrowSize, int(height / 2 + self.__arrowSize))) pts.append(QPoint(0, int(height / 2))) elif self.__arrowPosition == NavLabel.ArrowPosition.ArrowPosition_Bottom: self.__bgRect = QRect(0, 0, width, endY) pts.append(QPoint(int(width / 2 - self.__arrowSize), endY)) pts.append(QPoint(int(width / 2 + self.__arrowSize), endY)) pts.append(QPoint(int(width / 2), height)) elif self.__arrowPosition == NavLabel.ArrowPosition.ArrowPosition_Top: self.__bgRect = QRect(0, self.__arrowSize, width, height - self.__arrowSize) pts.append( QPoint(int(width / 2 - self.__arrowSize), self.__arrowSize)) pts.append( QPoint(int(width / 2 + self.__arrowSize), self.__arrowSize)) pts.append(QPoint(int(width / 2), 0)) # 绘制圆角矩形和三角箭头 if not self.__showArrow: bgRect = self.rect() painter.drawRoundedRect(self.__bgRect, self.__borderRadius, self.__borderRadius) else: painter.drawRoundedRect(self.__bgRect, self.__borderRadius, self.__borderRadius) painter.drawPolygon(pts) painter.restore()
def paintEvent(self, event: QPaintEvent) -> None: path = QPainterPath() path.moveTo(20, 80) path.lineTo(20, 30) path.cubicTo(80, 0, 50, 50, 80, 80) painter = QPainter(self) painter.setPen(self.pen) painter.setBrush(self.brush) painter.setRenderHint(QPainter.Antialiasing, True) x_inc = int(self.width() / (len(self.sb20.chainrings()) + len(self.sb20.cogs()) + 3)) w = int(2 * x_inc / 3) x = x_inc h_max = self.height() * 0.9 y_0 = self.height() * 0.05 factor = h_max / self.sb20.chainrings()[-1] for chainring in self.sb20.chainrings(): h = chainring * factor r = QRect(x, int(y_0 + (h_max - h) / 2), w, int(h)) if self.sb20.service_connected( ) and chainring == self.sb20.chainring_size(): painter.setBrush(Qt.SolidPattern) else: painter.setBrush(Qt.NoBrush) painter.drawRoundedRect(r, 25, 25, Qt.RelativeSize) x += x_inc x = self.width() - (len(self.sb20.cogs()) + 1) * x_inc factor = h_max / self.sb20.cogs()[0] for cog in self.sb20.cogs(): h = cog * factor r = QRect(x, int(y_0 + (h_max - h) / 2), w, int(h)) if self.sb20.service_connected() and cog == self.sb20.cog_size(): painter.setBrush(Qt.SolidPattern) else: painter.setBrush(Qt.NoBrush) painter.drawRoundedRect(r, 25, 25, Qt.RelativeSize) x += x_inc painter.setRenderHint(QPainter.Antialiasing, False) painter.setPen(self.palette().dark().color()) painter.setBrush(Qt.NoBrush) painter.drawRect(QRect(0, 0, self.width() - 1, self.height() - 1))
def drawValue(self, painter: QPainter) -> None: painter.save() painter.setPen(Qt.NoPen) # 定义了画刷则取画刷,可以形成渐变效果 painter.setBrush( QBrush(self.__valueColor if self.__valueBrush == Qt.NoBrush else self.__valueBrush)) # 计算当前值对应的百分比 step: float = self.__value / (self.__maxValue - self.__minValue) progress: int = int((self.width() - self.__padding * 2) * step) rect: QRect = QRect(self.__padding, (self.height() // 3) * 2, progress, int(self.height() / 3)) painter.drawRoundedRect(rect, self.__radius, self.__radius) painter.restore()
def drawBorder(self, painter: QPainter) -> None: if self.__borderWidth <= 0: return painter.save() pen: QPen = QPen() pen.setWidth(self.__borderWidth) pen.setColor(self.__tempColor) painter.setPen(pen) painter.setBrush(Qt.NoBrush) rect: QRect = QRect(self.__borderWidth // 2, self.__borderWidth // 2, self.width() - self.__borderWidth, self.height() - self.__borderWidth) painter.drawRoundedRect(rect, self.__borderRadius, self.__borderRadius) painter.restore()
def paint(self, painter: QtGui.QPainter, option: QtWidgets.QStyleOptionViewItem, index: QtCore.QModelIndex) -> None: self._painter = painter painter.save() self.initStyleOption(option, index) # construct new style option object with different colurs, then pass that to super newOption = QtWidgets.QStyleOptionViewItem(option) # draw outline and background backgroundBrush = self.backgroundBrush(painter, newOption, index) outlinePen = self.outlinePen(painter, newOption, index) if backgroundBrush or outlinePen: newOption.Alternate = False # no override from alternate row colours newOption.None_ = True # strange glyphs beyond all knowledge painter.setBrush(backgroundBrush) painter.setPen(outlinePen) painter.drawRoundedRect(newOption.rect, 2, 2) # # set pen for text #painter.setPen(self.textPen(painter, newOption, index)) # I'm not sure what the interaction is between the painter # and the palette style below for text # # reset pen for text # textColour = self.textColour(painter, newOption, index) # if textColour is None: # textColour = option.palette.text().color() # #print("textColour", textColour) # newOption.palette.setColor( # QtGui.QPalette.Text, # QtGui.QColor(textColour), # ) painter.setPen(self.textPen(painter, newOption, index)) #painter.setBrush(QtCore.Qt.NoBrush) #painter.drawText(newOption.rect, newOption.text) super(EasyDelegate, self).paint(painter, newOption, index) painter.restore()
def drawBg(self, painter: QPainter) -> None: painter.save() # 不可用背景灰色 if self.isEnabled(): linearGradient: QLinearGradient = QLinearGradient() if self.__hsbMode: # 起始坐标和结束坐标换个位置可改变颜色顺序 linearGradient.setStart( QPoint(self.__bgRect.x(), self.__bgRect.height())) linearGradient.setFinalStop( QPoint(self.__bgRect.x(), self.__bgRect.y())) # 由下往上,饱和度百分值由0增加到1.0 for i in [i * 0.0625 for i in range(17)]: linearGradient.setColorAt(i, QColor.fromHsvF(i, 1, 1, 1)) painter.setPen(Qt.NoPen) else: linearGradient.setStart(QPointF(0, 0)) linearGradient.setFinalStop(QPointF(0, self.height())) linearGradient.setColorAt(0.0, self.__topColor) linearGradient.setColorAt(1.0, self.__bottomColor) pen: QPen = QPen() pen.setWidthF(self.__borderWidth) pen.setColor(self.__borderColor) pen.setCapStyle(Qt.RoundCap) pen.setJoinStyle(Qt.RoundJoin) painter.setPen(pen) painter.setBrush(linearGradient) else: painter.setPen(Qt.NoPen) painter.setBrush(self.__disableColor) painter.drawRoundedRect(self.__bgRect, self.__borderRadius, self.__borderRadius) painter.restore()
def drawTip(self, painter: QPainter) -> None: painter.save() painter.setPen(Qt.NoPen) painter.setBrush( QBrush(self.__tipColor if self.__valueBrush == Qt.NoBrush else self.__valueBrush)) # 计算当前值对应的百分比 step: float = self.__value / (self.__maxValue - self.__minValue) progress: int = int((self.width() - self.__padding * 2) * step) # 绘制上部分提示信息背景 rect: QRect = QRect(progress, 0, self.__padding * 2, int(self.height() / 2.1)) painter.drawRoundedRect(rect, 2, 2) # 绘制倒三角 centerX: int = rect.center().x() initY: int = rect.height() offset: int = 5 pts: QPolygon = QPolygon() pts.append(QPoint(centerX - offset, initY)) pts.append(QPoint(centerX + offset, initY)) pts.append(QPoint(centerX, initY + offset)) painter.drawPolygon(pts) # 绘制文字 strValue: str = '' if self.__percent: temp: float = self.__value / (self.__maxValue - self.__minValue) * 100 strValue = "%s%%" % int(temp) else: strValue = "%s" % int(self.__value) painter.setPen(self.__textColor) painter.drawText(rect, Qt.AlignCenter, strValue) painter.restore()
def _draw_item_line(self, p: QPainter) -> None: """ 绘制项目周边的线条 :param p: 画刷 :return: None """ if self._m_start_rect.isNull(): return if self._m_item_line_style == self.ItemLineStyle.ItemNone: return elif self._m_item_line_style == self.ItemLineStyle.ItemTop: p1 = self._m_start_rect.topLeft() p2 = self._m_start_rect.topRight() elif self._m_item_line_style == self.ItemLineStyle.ItemRight: p1 = self._m_start_rect.topRight() p2 = self._m_start_rect.bottomRight() elif self._m_item_line_style == self.ItemLineStyle.ItemBottom: p1 = self._m_start_rect.bottomLeft() p2 = self._m_start_rect.bottomRight() elif self._m_item_line_style == self.ItemLineStyle.ItemLeft: p1 = self._m_start_rect.topLeft() p2 = self._m_start_rect.bottomLeft() elif self._m_item_line_style == self.ItemLineStyle.ItemRect: p1 = self._m_start_rect.topLeft() p2 = self._m_start_rect.bottomRight() else: return p.save() line_pen = QPen() line_pen.setColor(self._m_item_line_color) line_pen.setWidth(self._m_item_line_width) p.setPen(line_pen) if self._m_item_line_style == self.ItemLineStyle.ItemRect: p.drawRoundedRect(QRectF(p1, p2), self._m_item_radius, self._m_item_radius) else: p.drawLine(p1, p2) p.restore()
def paintEvent(self, event): s = self.size() qp = QPainter() qp.begin(self) qp.setRenderHint(QPainter.Antialiasing, True) qp.setPen(self.penColor) qp.setBrush(self.fillColor) qp.drawRect(0, 0, s.width(), s.height()) if self.drawDrag: pen = QPen(Qt.white, 5) pen.setCapStyle(Qt.RoundCap) qp.setPen(pen) qp.setBrush(self.fillColor) outerWidth = s.width() - 60 outerHeight = s.height() - 60 ow = int(s.width() / 2 - outerWidth / 2) oh = int(s.height() / 2 - outerHeight / 2) qp.drawRoundedRect(ow, oh, outerWidth, outerHeight, 5, 5) qp.setBrush(Qt.white) thickness = 12 length = 50 roundness = thickness / 2 vS = int(s.width() / 2 - thickness / 2) vE = int(s.height() / 2 - length / 2) qp.drawRoundedRect(vS, vE, thickness, length, roundness, roundness) hS = int(s.width() / 2 - length / 2) hE = int(s.height() / 2 - thickness / 2) qp.drawRoundedRect(hS, hE, length, thickness, roundness, roundness) qp.end()
def paintEvent(self, event): s = self.size() qp = QPainter() qp.begin(self) qp.setRenderHint(QPainter.Antialiasing, True) pen = QPen(Qt.NoPen) qp.setPen(pen) qp.setBrush(QColor(120, 120, 120)) qp.drawRoundedRect(0, 0, s.width(), s.height(), 12, 12) lg = QLinearGradient(35, 30, 35, 0) lg.setColorAt(0, QColor(210, 210, 210, 255)) lg.setColorAt(0.25, QColor(255, 255, 255, 255)) lg.setColorAt(0.82, QColor(255, 255, 255, 255)) lg.setColorAt(1, QColor(210, 210, 210, 255)) qp.setBrush(lg) qp.drawRoundedRect(1, 1, s.width() - 2, s.height() - 2, 10, 10) qp.setBrush(QColor(210, 210, 210)) qp.drawRoundedRect(2, 2, s.width() - 4, s.height() - 4, 10, 10) if self.__enabled: lg = QLinearGradient(50, 30, 35, 0) lg.setColorAt(0, QColor(230, 230, 230, 255)) lg.setColorAt(0.25, QColor(255, 255, 255, 255)) lg.setColorAt(0.82, QColor(255, 255, 255, 255)) lg.setColorAt(1, QColor(230, 230, 230, 255)) qp.setBrush(lg) qp.drawRoundedRect(3, 3, s.width() - 6, s.height() - 6, 7, 7) else: lg = QLinearGradient(50, 30, 35, 0) lg.setColorAt(0, QColor(200, 200, 200, 255)) lg.setColorAt(0.25, QColor(230, 230, 230, 255)) lg.setColorAt(0.82, QColor(230, 230, 230, 255)) lg.setColorAt(1, QColor(200, 200, 200, 255)) qp.setBrush(lg) qp.drawRoundedRect(3, 3, s.width() - 6, s.height() - 6, 7, 7) qp.end()
def drawSlider(self, painter: QPainter) -> None: painter.save() painter.setPen(Qt.NoPen) if not self.__checked: painter.setBrush(self.__sliderColorOff) else: painter.setBrush(self.__sliderColorOn) if self.__buttonStyle == SwitchButton.ButtonStyle.ButtonStyle_Rect: sliderWidth: int = self.width() // 2 - self.__space * 2 sliderHeight: int = self.height() - self.__space * 2 sliderRect: QRect = QRect(self.__startX + self.__space, self.__space, sliderWidth , sliderHeight) painter.drawRoundedRect(sliderRect, self.__rectRadius, self.__rectRadius) elif self.__buttonStyle == SwitchButton.ButtonStyle.ButtonStyle_CircleIn: rect: QRect = QRect(0, 0, self.width(), self.height()) sliderWidth: int = min(rect.width(), rect.height()) - self.__space * 2 sliderRect: QRect = QRect(self.__startX + self.__space, self.__space, sliderWidth, sliderWidth) painter.drawEllipse(sliderRect) elif self.__buttonStyle == SwitchButton.ButtonStyle.ButtonStyle_CircleOut: sliderWidth: int = self.height() sliderRect: QRect = QRect(self.__startX, 0, sliderWidth, sliderWidth) color1: QColor = Qt.white if self.__checked else self.__bgColorOff color2: QColor = self.__sliderColorOn if self.__checked else self.__sliderColorOff radialGradient: QRadialGradient = QRadialGradient(sliderRect.center(), sliderWidth // 2) radialGradient.setColorAt(0, color1 if self.__checked else color2) radialGradient.setColorAt(0.5, color1 if self.__checked else color2) radialGradient.setColorAt(0.6, color2 if self.__checked else color1) radialGradient.setColorAt(1.0, color2 if self.__checked else color1) painter.setBrush(radialGradient) painter.drawEllipse(sliderRect) painter.restore()
class Button(QWidget): clicked = Signal() def __init__(self, text): super(Button, self).__init__() self.text = text self.roundness = 10 self.painter = QPainter() self.font = QFont() self.font.setPointSize(14) self.font.setBold(True) metrics = QFontMetrics(self.font) self.setMinimumSize(metrics.width(text) + 40, 50) self.pen = QPen(QColor("#01d277")) self.pen.setWidth(5) self.hover_brush = QBrush(QColor(1, 210, 119, 100)) self.hover = False def enterEvent(self, event): QApplication.setOverrideCursor(Qt.PointingHandCursor) self.hover = True self.repaint() super(Button, self).enterEvent(event) def leaveEvent(self, event): QApplication.restoreOverrideCursor() self.hover = False self.repaint() super(Button, self).leaveEvent(event) def mousePressEvent(self, event): self.clicked.emit() super(Button, self).mousePressEvent(event) def paintEvent(self, event): self.painter.begin(self) self.draw() self.painter.end() def draw(self): rect = self.rect() self.painter.setRenderHint(self.painter.Antialiasing) self.painter.setPen(self.pen) border_rect = QRect(rect.x() + 5, rect.y() + 5, rect.width() - 10, rect.height() - 10) self.painter.drawRoundedRect(border_rect, self.roundness, self.roundness) self.painter.setFont(self.font) self.painter.drawText(border_rect, Qt.AlignVCenter | Qt.AlignHCenter, self.text) if self.hover: self.painter.setBrush(self.hover_brush) self.painter.setPen(Qt.NoPen) self.painter.drawRoundedRect(border_rect, self.roundness, self.roundness)
def drawBg(self, painter: QPainter) -> None: painter.save() painter.setPen(Qt.NoPen) bgColor: QColor = self.__bgColorOn if self.__checked else self.__bgColorOff if not self.isEnabled(): bgColor.setAlpha(60) painter.setBrush(bgColor) if self.__buttonStyle is SwitchButton.ButtonStyle.ButtonStyle_Rect: painter.drawRoundedRect(self.rect(), self.__rectRadius, self.__rectRadius) elif self.__buttonStyle is SwitchButton.ButtonStyle.ButtonStyle_CircleIn: rect: QRect = QRect(0, 0, self.width(), self.height()) side: int = min(rect.width(), rect.height()) # 半径为高度的一半 # 左侧圆 path1: QPainterPath = QPainterPath() path1.addEllipse(rect.x(), rect.y(), side, side) # 右侧圆 path2: QPainterPath = QPainterPath() path2.addEllipse(rect.width() - side, rect.y(), side, side) # 中间矩形 path3: QPainterPath = QPainterPath() path3.addRect(rect.x() + side // 2, rect.y(), rect.width() - side, rect.height()) path: QPainterPath = QPainterPath() path = path3 + path1 + path2 painter.drawPath(path) elif self.__buttonStyle is SwitchButton.ButtonStyle.ButtonStyle_CircleOut: rect: QRect = QRect(self.height() // 2, self.__space, self.width() - self.height(), self.height() - self.__space * 2) painter.drawRoundedRect(rect, self.__rectRadius, self.__rectRadius) if self.__buttonStyle is SwitchButton.ButtonStyle.ButtonStyle_Rect or \ self.__buttonStyle is SwitchButton.ButtonStyle.ButtonStyle_CircleIn: # 绘制文本和小圆,互斥 if self.__showText: sliderWidth: int = min(self.width(), self.height()) - self.__space * 2 if self.__buttonStyle is SwitchButton.ButtonStyle.ButtonStyle_Rect: sliderWidth = self.width() // 2 - 5 elif self.__buttonStyle is SwitchButton.ButtonStyle.ButtonStyle_CircleIn: sliderWidth -= 5 if self.__checked: textRect: QRect = QRect(0, 0, self.width() - sliderWidth, self.height()) painter.setPen(self.__textColorOn) painter.drawText(textRect, Qt.AlignCenter, self.__textOn) else: textRect: QRect = QRect(sliderWidth, 0, self.width() - sliderWidth, self.height()) painter.setPen(self.__textColorOff) painter.drawText(textRect, Qt.AlignCenter, self.__textOff) elif self.__showCircle: side: int = min(self.width(), self.height()) // 2 y: int = (self.height() - side) // 2 if self.__checked: circleRect: QRect = QRect(side // 2, y, side, side) pen: QPen = QPen(self.__textColorOn, 2) painter.setPen(pen) painter.setBrush(Qt.NoBrush) painter.drawEllipse(circleRect) else: circleRect: QRect = QRect(int(self.width() - (side * 1.5)), y, side, side) pen: QPen = QPen(self.__textColorOff, 2) painter.setPen(pen) painter.setBrush(Qt.NoBrush) painter.drawEllipse(circleRect) painter.restore()