def drawTriangle(self, painter: QPainter) -> None: if not self.__showTriangle: return painter.save() painter.setPen(Qt.NoPen) painter.setBrush(self.__triangleColor) # 绘制在右侧中间,根据设定的倒三角的边长设定三个点位置 width: int = self.width() height: int = self.height() midWidth: int = width // 2 midHeight: int = height // 2 pts: QPolygon = QPolygon() if self.__trianglePosition == NavLabel.TrianglePosition.TrianglePosition_Left: pts.append(QPoint(self.__triangleLen, midHeight)) pts.append(QPoint(0, midHeight - self.__triangleLen)) pts.append(QPoint(0, midHeight + self.__triangleLen)) elif self.__trianglePosition == NavLabel.TrianglePosition.TrianglePosition_Right: pts.append(QPoint(width - self.__triangleLen, midHeight)) pts.append(QPoint(width, midHeight - self.__triangleLen)) pts.append(QPoint(width, midHeight + self.__triangleLen)) elif self.__trianglePosition == NavLabel.TrianglePosition.TrianglePosition_Top: pts.append(QPoint(midWidth, self.__triangleLen)) pts.append(QPoint(midWidth - self.__triangleLen, 0)) pts.append(QPoint(midWidth + self.__triangleLen, 0)) elif self.__trianglePosition == NavLabel.TrianglePosition.TrianglePosition_Bottom: pts.append(QPoint(midWidth, height - self.__triangleLen)) pts.append(QPoint(midWidth - self.__triangleLen, height)) pts.append(QPoint(midWidth + self.__triangleLen, height)) painter.drawPolygon(pts) painter.restore()
def drawTitle(self, painter: QPainter) -> None: painter.save() painter.setPen(Qt.NoPen) painter.setBrush(self.__tempColor) offset: int = self.__borderWidth - self.__borderWidth // 3 rect: QRect = QRect(offset, offset, self.width() - offset * 2, self.__titleHeight) painter.drawRect(rect) # 绘制标题文字 if self.__isEnable: painter.setPen(self.__alarmTextColor if self.__isAlarm else self.__titleColor) else: painter.setPen(self.__titleDisableColor) painter.setFont(self.__titleFont) # 文字区域要重新计算 offset = self.__borderWidth * 3 textRect: QRect = QRect(offset, 0, self.width() - offset * 2, self.__titleHeight) align: Qt.Alignment = Qt.Alignment if self.__titleAlignment == PanelItem.Alignment.Alignment_Left: align = Qt.AlignLeft | Qt.AlignVCenter elif self.__titleAlignment == PanelItem.Alignment.Alignment_Center: align = Qt.AlignHCenter | Qt.AlignVCenter elif self.__titleAlignment == PanelItem.Alignment.Alignment_Right: align = Qt.AlignRight | Qt.AlignVCenter painter.drawText(textRect, align, self.__titleText) painter.restore()
def paint(self, painter: QPainter, option, widget=...) -> None: painter.setBrush(self.brush()) painter.setPen(self.pen()) painter.setFont(self.font()) painter.drawText(self.boundingRect(), self.alignMode(), self.elidedText())
def drawBg(self, painter: QPainter) -> None: painter.save() # 如果悬停则绘制外边框和内边框 if self.__hover or self.__hovered: painter.setBrush(Qt.NoBrush) pen: QPen = QPen() pen.setWidth(2) # 绘制外边框 pen.setColor(self.__bgColor) painter.setPen(pen) painter.drawRect(5, 5, self.width() - 10, self.height() - 10) # 绘制里边框 pen.setColor("#FFFFFF") painter.setPen(pen) painter.drawRect(7, 7, self.width() - 14, self.height() - 14) # 绘制里边背景 painter.setPen(Qt.NoPen) painter.setBrush(QColor(self.__bgColor)) painter.drawRect(8, 8, self.width() - 16, self.height() - 16) painter.restore()
def paintEvent(self, event): super(PixmapWidget, self).paintEvent(event) if not self.pixmap or self.pixmap.isNull(): return p = QPainter(self) source = QRect(0, 0, self.pixmap.width(), self.pixmap.height()) sw = float(source.width()) sh = float(source.height()) tw = float(self.width())+1 th = float(self.height())+1 tx = 0 ty = 0 if sw/tw > sh/th: ntw = tw nth = sh/sw*tw ty = (th-nth)/2 else: nth = th ntw = sw/sh*th tx = (tw-ntw)/2 target = QRect(tx, ty, ntw, nth) p.setBrush(self.bgBrush) p.setPen(self.bgPen) p.drawRect(self.rect()) p.drawPixmap(target, self.pixmap, source)
def paintEvent(self, event: QPaintEvent): painter = QPainter(self) painter.setRenderHint(QPainter.Antialiasing, True) painter.setBrush(QColor(255, 160, 90)) painter.drawEllipse(QPointF(self.radius, self.radius), self.radius - 1, self.radius - 1) super(ChessPieces, self).paintEvent(event) pass
def drawOverlay(self, painter: QPainter) -> None: if not self.__showOverlay: return radius: int = 80 painter.save() painter.setPen(Qt.NoPen) smallCircle: QPainterPath = QPainterPath() bigCircle: QPainterPath = QPainterPath() radius -= 1 smallCircle.addEllipse(-radius, -radius, radius * 2, radius * 2) radius *= 2 bigCircle.addEllipse(-radius, -radius + 140, radius * 2, radius * 2) # 高光的形状为小圆扣掉大圆的部分 highlight: QPainterPath = smallCircle - bigCircle linearGradient: QLinearGradient = QLinearGradient(0, -radius / 2, 0, 0) self.__overlayColor.setAlpha(100) linearGradient.setColorAt(0.0, self.__overlayColor) self.__overlayColor.setAlpha(30) linearGradient.setColorAt(1.0, self.__overlayColor) painter.setBrush(linearGradient) painter.rotate(-20) painter.drawPath(highlight) painter.restore()
def paintGL(self): r = min(self.width(), self.height()) / 2 self.__text_r = r - (r/10) # radius of the text self.__tick_r = r - (r/8) # outer radius of the tick marks self.__tick_l = (r/6) # length of each tick, extending inwards self.__needle_l = (r/5) * 3 # length of the needle self.font.setPixelSize(int(max(self.width(), self.height())/3)) self.note_font.setPixelSize(int(max(self.width(), self.height())/30)) self.title_font.setPixelSize(int(max(self.width(), self.height())/12)) painter = QPainter() painter.begin(self) painter.setFont(self.font) painter.setPen(self.pen) painter.setBrush(self.brush) painter.setRenderHint(QPainter.Antialiasing) self.draw_title(painter) self.draw_value(painter) if self.config["numerals"]: self.draw_multiplier(painter) self.draw_marks(painter) painter.end()
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): painter = QPainter(self) reset_brush = painter.brush() for figure in self.__figures: if not isinstance(figure, Figure): continue if isinstance(figure, Rectangle): painter.setBrush(QBrush(Qt.red)) painter.drawRect(figure.x, figure.y, figure.width, figure.height) continue # if isinstance(figure, Ellipse): # painter.setBrush(QBrush(Qt.green)) # painter.drawEllipse(figure.x(), figure.y(), figure.width(), figure.height()) # continue if isinstance(figure, CloseFigure): painter.setBrush(QBrush(Qt.blue)) points = [] for point in figure.d: points.append(QPoint(point['x'], point['y'])) painter.drawPolygon(points) continue
def drawText(self, painter: QPainter) -> None: painter.save() painter.setPen(Qt.NoPen) font: QFont = QFont() font.setBold(True) font.setPointSize(10) painter.setFont(font) now: QDateTime = QDateTime.currentDateTime() fm: QFontMetricsF = QFontMetricsF(font) textList: List[AnyStr] = [ now.toString("MM月dd日yyyy"), now.toString("hh:mm:ss.zzz") ] # 绘制文本路径 textPath: QPainterPath = QPainterPath() textPath.addText(-fm.width(textList[0]) / 2.0, -fm.lineSpacing() / 2.0, font, textList[0]) textPath.addText(-fm.width(textList[1]) / 2.0, fm.lineSpacing() / 2.0, font, textList[1]) strokeColor: QColor = self.__textColor.light(80) strokeColor.setAlphaF(0.2) painter.strokePath( textPath, QPen(strokeColor, self.__shadowWidth, Qt.SolidLine, Qt.RoundCap, Qt.RoundJoin)) painter.setBrush(self.__textColor) painter.drawPath(textPath) painter.restore()
def drawBg_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 # 逐个绘制连接线条 pen: QPen = QPen() pen.setWidthF(radius // 4) pen.setCapStyle(Qt.RoundCap) pen.setColor(self.__background) painter.setPen(pen) painter.setBrush(Qt.NoBrush) for i in range(self.__maxStep - 1): painter.drawLine(QPoint(initX, initY), QPoint(initX + width, initY)) initX += width # 逐个绘制圆 initX: int = width // 2 painter.setPen(Qt.NoPen) painter.setBrush(self.__background) for i in range(self.__maxStep): painter.drawEllipse(QPoint(initX, initY), radius, radius) initX += width painter.restore()
def drawBg(self, painter: QPainter) -> None: radius: int = 99 painter.save() painter.setPen(Qt.NoPen) painter.setBrush(self.__bgColor) painter.drawEllipse(-radius, -radius, radius * 2, radius * 2) 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 __init__(self, parent, delegate): QWidget.__init__(self, parent) self.delegate = delegate self.emitted = False self.ui = Ui_AskVerdict() self.ui.setupUi(self) # build verdict icons if not AskVerdict.icons_cache: for text in VERDICTS: color = VERDICT_TO_ROW_COLORS.get(text[0]) pixmap = QPixmap(16, 16) pixmap.fill(Qt.transparent) if color: painter = QPainter(pixmap) painter.setPen(Qt.black) painter.setBrush(color) painter.drawEllipse(0, 0, 15, 15) painter.end() AskVerdict.icons_cache[text] = QIcon(pixmap) # set combo verdict for text in ("other...", "skip", "retry"): self.ui.comboVerdict.addItem(AskVerdict.icons_cache[text], text) model = self.ui.comboVerdict.model() model.itemFromIndex(model.index(0, 0)).setSelectable(False) self.ui.comboVerdict.activated.connect(self.on_dropdown_item_activated) self.ui.goodVerdict.clicked.connect(self.on_good_bad_button_clicked) self.ui.goodVerdict.setIcon(AskVerdict.icons_cache["good"]) self.ui.badVerdict.clicked.connect(self.on_good_bad_button_clicked) self.ui.badVerdict.setIcon(AskVerdict.icons_cache["bad"])
def paintEvent(self, paintEvent: QPaintEvent): painter = QPainter(self) painter.setBackgroundMode(Qt.TransparentMode) painter.setRenderHint(QPainter.Antialiasing) brush = QBrush() brush.setStyle(Qt.SolidPattern) pen = QPen() pen.setJoinStyle(Qt.RoundJoin) pen.setCapStyle(Qt.RoundCap) center = QPoint(self.width() // 2, self.height() // 2) radius = 0.45 * min(self.width(), self.height()) pen.setColor(self.palette().color(self.color[0])) brush.setColor(self.palette().color(self.color[1])) if self.highlight is True: pen.setColor(self.palette().color(QPalette.Highlight)) pen.setWidth(round(0.15 * radius)) painter.setBrush(brush) painter.setPen(pen) painter.drawEllipse(center, radius, radius) if self.checked is True: brush.setColor(self.palette().color(QPalette.Background)) pen.setColor(self.palette().color(QPalette.Background)) painter.setPen(pen) painter.setBrush(brush) painter.drawEllipse(center, 0.40 * radius, 0.40 * radius) del painter, brush, pen
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 drawArc(self, painter: QPainter, radius: int, angle: float, arc_color: QColor) -> None: painter.save() painter.setPen(Qt.NoPen) smallradius: int = radius - self.__radiusWidth maxRaidus: int = radius + self.__shadowWidth minRadius: int = smallradius - self.__shadowWidth # 采用圆形渐变,形成光晕效果 radialGradient: QRadialGradient = QRadialGradient( QPointF(0, 0), maxRaidus) color: QColor = arc_color lightColor: QColor = arc_color.name() color.setAlphaF(0) radialGradient.setColorAt(0, color) radialGradient.setColorAt(minRadius * 1.0 / maxRaidus, color) color.setAlphaF(0.5) radialGradient.setColorAt(smallradius * 1.0 / maxRaidus, color) radialGradient.setColorAt((smallradius + 1) * 1.0 / maxRaidus, lightColor) radialGradient.setColorAt((radius - 1) * 1.0 / maxRaidus, lightColor) radialGradient.setColorAt(radius * 1.0 / maxRaidus, color) color.setAlphaF(0) radialGradient.setColorAt(1, color) painter.setBrush(QBrush(radialGradient)) painter.drawPie(-maxRaidus, -maxRaidus, maxRaidus * 2, maxRaidus * 2, 90 * 16, int(-angle * 16)) painter.restore()
def drawPointerIndicator(self, painter: QPainter) -> None: if not self.__showPointer: return radius: int = self.__radiusCircle - 15 painter.save() painter.setPen(Qt.NoPen) painter.setBrush(self.__progressColor) pts: QPolygon = QPolygon() pts.append(QPoint(-8, 0)) pts.append(QPoint(8, 0)) pts.append(QPoint(0, radius)) painter.rotate(self.__startAngle) degRotate: float = (360.0 - self.__startAngle - self.__endAngle) / ( self.__maxValue - self.__minValue) * (self.__value - self.__minValue) painter.rotate(degRotate) painter.drawConvexPolygon(pts) # 绘制中心圆点 radius = radius // 4 painter.drawEllipse(-radius, -radius, radius * 2, radius * 2) painter.restore()
def paint( self, painter: QtGui.QPainter, option: QtWidgets.QStyleOptionGraphicsItem, widget: QtWidgets.QWidget = None, ): view = self.scene().views()[0] view_width = view.mapToScene(0, 0, self.width, 1).boundingRect().width() width, unit = self.getWidthAndUnit(view_width) # Current scale text = f"{width * self.units[self.unit] / self.units[unit]:.3g} {unit}" width = width * view.transform().m11() fm = QtGui.QFontMetrics(self.font, painter.device()) path = QtGui.QPainterPath() path.addText( self.width / 2.0 - fm.boundingRect(text).width() / 2.0, fm.ascent(), self.font, text, ) painter.strokePath(path, QtGui.QPen(QtCore.Qt.black, 2.0)) painter.fillPath(path, QtGui.QBrush(self.color, QtCore.Qt.SolidPattern)) # Draw the bar rect = QtCore.QRectF(self.width / 2.0 - width / 2.0, fm.height(), width, self.height) painter.setPen(QtGui.QPen(QtCore.Qt.black, 1.0)) painter.setBrush(QtGui.QBrush(self.color, QtCore.Qt.SolidPattern)) painter.drawRect(rect)
def paintEvent(self, event): side = min(self.width(), self.height()) time = QTime.currentTime() painter = QPainter(self) painter.setRenderHint(QPainter.Antialiasing) painter.translate(self.width() / 2, self.height() / 2) painter.scale(side / 200.0, side / 200.0) painter.setPen(Qt.NoPen) painter.setBrush(AnalogClock.hourColor) painter.save() painter.rotate(30.0 * ((time.hour() + time.minute() / 60.0))) painter.drawConvexPolygon(AnalogClock.hourHand) painter.restore() painter.setPen(AnalogClock.hourColor) for i in range(12): painter.drawLine(88, 0, 96, 0) painter.rotate(30.0) painter.setPen(Qt.NoPen) painter.setBrush(AnalogClock.minuteColor) painter.save() painter.rotate(6.0 * (time.minute() + time.second() / 60.0)) painter.drawConvexPolygon(AnalogClock.minuteHand) painter.restore() painter.setPen(AnalogClock.minuteColor) for j in range(60): if (j % 5) != 0: painter.drawLine(92, 0, 96, 0) painter.rotate(6.0)
def update(self, new_vals): self.values = new_vals painter = QPainter(self.label.pixmap()) painter.setRenderHint(QPainter.Antialiasing) painter.setPen(QPen('#333333')) painter.setBrush(QColor('#333333')) painter.drawRect(self.label.rect()) pen = QPen(QColor(255, 255, 255)) pen.setWidth(1) painter.setPen(pen) painter.setBrush(QBrush(Qt.white)) x_old = -1 y_old = -1 for i in range(len(self.values)): v = self.values[i] x = i * (self.label.width() / len(self.values)) y = self.label.height() - self.label.height() * v if self.connect_lines_check_box.isChecked() and i > 0: painter.drawLine(x_old, y_old, x, y) else: painter.drawEllipse(x - 1, y - 1, 2, 2) x_old = x y_old = y self.repaint()
def paintEvent(self, event: QPaintEvent): painter = QPainter(self) bg_color = bg_color_for_object_set(self.object_set, 0) painter.setBrush(QBrush(bg_color)) painter.drawRect(QRect(QPoint(0, 0), self.size())) pattern_table = PatternTable(self.object_set) palette = load_palette(self.object_set, 0) tsa_data = ROM.get_tsa_data(self.object_set) horizontal = self.sprites_horiz block_length = Block.WIDTH * self.zoom for i in range(self.sprites): block = Block(i, palette, pattern_table, tsa_data) x = (i % horizontal) * block_length y = (i // horizontal) * block_length block.draw(painter, x, y, block_length) return
def drawColorPie(self, painter: QPainter) -> None: radius: int = 95 painter.save() painter.setPen(Qt.NoPen) rect: QRectF = QRectF(-radius, -radius, radius * 2, radius * 2) # 计算总范围角度,当前值范围角度,剩余值范围角度 angleAll: float = 360.0 - self.__startAngle - self.__endAngle angleCurrent: float = angleAll * ((self.__value - self.__minValue) / (self.__maxValue - self.__minValue)) angleOther: float = angleAll - angleCurrent # 绘制当前值饼圆 painter.setBrush(self.__progressColor) painter.drawPie(rect, (270 - self.__startAngle - angleCurrent) * 16, angleCurrent * 16) # 绘制剩余值饼圆 painter.setBrush(self.__progressBgColor) painter.drawPie( rect, (270 - self.__startAngle - angleCurrent - angleOther) * 16, angleOther * 16) painter.restore()
def drawSign(self, painter: QPainter) -> None: painter.save() # 如果当前选中则绘制角标 if self.__checked: # 计算角标区域 rec: QRect = QRect(self.width() - self.__signSize, self.height() - self.__signSize, self.__signSize, self.__signSize) # 绘制带边框背景 pen: QPen = QPen() pen.setWidth(3) pen.setColor("#FFFFFF") painter.setPen(pen) painter.setBrush(QColor(self.__signColor)) painter.drawEllipse(rec.x() + 3, rec.y() + 3, self.__signSize - 6, self.__signSize - 6) # 绘制文字 self.__iconFont.setPixelSize(16) painter.setFont(self.__iconFont) painter.drawText(rec, Qt.AlignCenter, chr(0xf00c)) painter.restore()
def drawCoverCircle(self, painter: QPainter) -> None: radius: int = self.__radiusCoverCircle painter.save() painter.setPen(Qt.NoPen) painter.setBrush(self.__bgColor) painter.drawEllipse(-radius, -radius, radius * 2, radius * 2) painter.restore()
def drawOverlay(self, painter: QPainter) -> None: # 如果没有启用绘制遮住颜色或者当前不可用则不用绘制 if not self.__showOverlay or not self.isEnabled(): return painter.save() # 根据当前百分比计算高度 width: int = self.__bgRect.width() - self.__borderWidth height: int = self.__bgRect.height() - self.__borderWidth height = int(height - (self.__percent / 100) * height) if self.__outMode: overlayRect = QRect(self.__borderWidth, self.__borderWidth + self.__percentHeight // 2, width, height) else: overlayRect = QRect(self.__borderWidth, self.__borderWidth, width, height) painter.setPen(Qt.NoPen) painter.setBrush(self.__overlayColor) painter.drawRect(overlayRect) painter.restore()
def drawPointerIndicatorR(self, painter: QPainter) -> None: if not self.__showPointer: return radius: int = self.__radiusCircle - 15 painter.save() pen: QPen = QPen() pen.setWidth(1) pen.setColor(self.__progressColor) painter.setPen(pen) painter.setBrush(self.__progressColor) pts: QPolygon = QPolygon() pts.append(QPoint(-8, 0)) pts.append(QPoint(8, 0)) pts.append(QPoint(0, radius)) painter.rotate(self.__startAngle) degRotate: float = (360.0 - self.__startAngle - self.__endAngle) / ( self.__maxValue - self.__minValue) * (self.__value - self.__minValue) painter.rotate(degRotate) painter.drawConvexPolygon(pts) # 增加绘制圆角直线,与之前三角形重叠,形成圆角指针 pen.setCapStyle(Qt.RoundCap) pen.setWidthF(4) painter.setPen(pen) painter.drawLine(0, 0, 0, radius) # 绘制中心圆点 radius = radius // 4 painter.drawEllipse(-radius, -radius, radius * 2, radius * 2) painter.restore()
class ImageViewer(QWidget): def __init__(self): super(ImageViewer, self).__init__() self.painter = QPainter() self.my_pen = QPen(QColor("red")) self.my_pen.setWidth(5) self.my_brush = QBrush(QColor("#123456")) self.photo = QPixmap() self.photo_rect = QRect() def set_pixmap(self, image_path): self.photo.load(image_path) self.repaint() def paintEvent(self, event): self.painter.begin(self) self.draw() self.painter.end() def draw(self): rect = self.rect() self.painter.setPen(self.my_pen) self.painter.setBrush(self.my_brush) photo = self.photo.scaled(QSize(rect.width(), rect.height()), Qt.KeepAspectRatio, Qt.SmoothTransformation) self.photo_rect.setRect(rect.x(), rect.y(), photo.width(), photo.height()) self.photo_rect.moveCenter(rect.center()) self.painter.drawPixmap(self.photo_rect, photo)
def paint(self, painter: QPainter, option, widget=...): if self.frame() is None: super().paint(painter, option, widget) else: painter.setFont(self.font()) painter.setBrush(self.brush()) painter.drawText(self.frame(), self.alignment(), self.text())
def drawText_TB(self, painter: QPainter) -> None: width: int = self.width() // self.__maxStep height: int = self.height() // 3 initX: int = 0 initY: int = 0 painter.save() font: QFont = QFont() font.setPixelSize(height // 3) painter.setFont(font) painter.setPen(self.__background) painter.setBrush(Qt.NoBrush) # 绘制上部分文字 for i in range(self.__maxStep): textRect: QRect = QRect(initX, initY, width, height) painter.drawText(textRect, Qt.AlignCenter, self.__topInfo[i]) initX += width # 绘制下部分文字 initX = 0 initY: int = self.height() // 3 * 2 for i in range(self.__currentStep): textRect: QRect = QRect(initX, initY, width, height) painter.drawText(textRect, Qt.AlignCenter, self.__bottomInfo[i]) initX += width painter.restore()
def paintEvent(self, event): qp = QPainter(self) if not self.islocationSelected: qp.setPen(QColor(175, 191, 191)) qp.setBrush(QColor(175, 191, 191)) #247,247,235 else: qp.setBrush(QColor(50, 168, 82)) download = qp.drawEllipse(QPoint(62, 62), 62, 62)
def paint(self, painter: QPainter, styleOption: QStyleOptionGraphicsItem, widget: QWidget=None): pen = QPen() pen.setWidthF(0.05) pen.setColor(Qt.darkGray) painter.setPen(pen) brush = QBrush() brush.setColor(self.color) brush.setStyle(Qt.SolidPattern) painter.setBrush(brush) topLeft = QPointF(0, 0) bottomRight = QPointF(1, 1) rectangle = QRectF(topLeft, bottomRight) rectangle.translate(-0.5, -0.5) painter.drawRect(rectangle)
def paintEvent(self, event): painter = QPainter(self) painter.fillRect(self.rect(), Qt.black) if self.pixmap.isNull(): painter.setPen(Qt.white) painter.drawText(self.rect(), Qt.AlignCenter, "Rendering initial image, please wait...") return if self.curScale == self.pixmapScale: painter.drawPixmap(self.pixmapOffset, self.pixmap) else: scaleFactor = self.pixmapScale / self.curScale newWidth = int(self.pixmap.width() * scaleFactor) newHeight = int(self.pixmap.height() * scaleFactor) newX = self.pixmapOffset.x() + (self.pixmap.width() - newWidth) / 2 newY = self.pixmapOffset.y() + (self.pixmap.height() - newHeight) / 2 painter.save() painter.translate(newX, newY) painter.scale(scaleFactor, scaleFactor) exposed, _ = painter.matrix().inverted() exposed = exposed.mapRect(self.rect()).adjusted(-1, -1, 1, 1) painter.drawPixmap(exposed, self.pixmap, exposed) painter.restore() text = "Use mouse wheel or the '+' and '-' keys to zoom. Press and " \ "hold left mouse button to scroll." metrics = painter.fontMetrics() textWidth = metrics.width(text) painter.setPen(Qt.NoPen) painter.setBrush(QColor(0, 0, 0, 127)) painter.drawRect((self.width() - textWidth) / 2 - 5, 0, textWidth + 10, metrics.lineSpacing() + 5) painter.setPen(Qt.white) painter.drawText((self.width() - textWidth) / 2, metrics.leading() + metrics.ascent(), text)
def testSetBrushGradient(self): painter = QPainter() gradient = QLinearGradient(0, 0, 0, 0) painter.setBrush(gradient)