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 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 doubleBufferPaint(self, buffer=None): self.blockSignals(True) #self.drawFrame(painter) if buffer is None: buffer = QImage(self.width(), self.height(), QImage.Format_RGB888) buffer.fill(Qt.black) # calculate offset and scale of droplet image pixmap scale_x, scale_y, offset_x, offset_y = self.get_from_image_transform() db_painter = QPainter(buffer) db_painter.setRenderHints(QPainter.Antialiasing | QPainter.NonCosmeticDefaultPen) db_painter.setBackground(QBrush(Qt.black)) db_painter.setPen(QPen(Qt.black, 0)) db_painter.drawPixmap(offset_x, offset_y, self._pixmap) pen = QPen(Qt.magenta, 1) pen_fine = QPen(Qt.blue, 1) pen.setCosmetic(True) db_painter.setPen(pen) # draw droplet outline and tangent only if evaluate_droplet was successful if self._droplet.is_valid: try: # transforming true image coordinates to scaled pixmap coordinates db_painter.translate(offset_x, offset_y) db_painter.scale(scale_x, scale_y) # drawing tangents and baseline db_painter.drawLine(*self._droplet.line_l) db_painter.drawLine(*self._droplet.line_r) db_painter.drawLine(*self._droplet.int_l, *self._droplet.int_r) # move origin to ellipse origin db_painter.translate(*self._droplet.center) # draw diagnostics # db_painter.setPen(pen_fine) # # lines parallel to coordinate axes # db_painter.drawLine(0,0,20*scale_x,0) # db_painter.drawLine(0,0,0,20*scale_y) # # angle arc # db_painter.drawArc(-5*scale_x, -5*scale_y, 10*scale_x, 10*scale_y, 0, -self._droplet.tilt_deg*16) # rotate coordinates to ellipse tilt db_painter.rotate(self._droplet.tilt_deg) # draw ellipse # db_painter.setPen(pen) db_painter.drawEllipse(-self._droplet.maj / 2, -self._droplet.min / 2, self._droplet.maj, self._droplet.min) # # major and minor axis for diagnostics # db_painter.drawLine(0, 0, self._droplet.maj/2, 0) # db_painter.drawLine(0, 0, 0, self._droplet.min/2) except Exception as ex: logging.error(ex) db_painter.end() self.blockSignals(False) return buffer
def paintEvent(self, paintEvent): # pylint: disable=unused-argument """ The paint event actually drawing the image :param paintEvent: a QPaintEvent instance :return: """ p = QPainter(self) p.scale(self._scale, self._scale) p.drawImage(0, 0, self._img)
def paintEvent(self, ev): """ Manually implemented paint event :param ev: the QT paint event :return: """ h = self.height() w = self.width() p = QPainter(self) p.setClipRect(ev.region().boundingRect()) pen = QPen(QColor(0, 0, 0)) pen.setWidth(4) ls = QFontMetricsF(p.font()).lineSpacing() for idx, t in enumerate(sorted(list(self._loadData.keys()))): y = 10 + idx * ls pen.setColor(ThreadToColor.singleton.get(t)) p.setPen(pen) p.drawLine(QLineF(15, y, 15 + 15, y)) pen.setColor(QColor(0, 0, 0)) p.setPen(pen) p.drawText(QPointF(35, y), t) if len(self._loadData) > 0: right = max([ polygon[polygon.count() - 1].x() for _, polygon in self._loadData.items() ]) else: right = 0.0 p.translate(w - 10 - right * 20, h - 10) p.scale( 20, -(h - 20) ) # x direction: 20 pixels per second, y direction: spread between 10 and h-10 topleft = p.transform().inverted()[0].map(QPointF(10, 10)) pen.setWidthF(0) pen.setCosmetic(True) left = topleft.x() p.setRenderHint(QPainter.Antialiasing, True) p.setPen(pen) p.drawLine(QLineF(left, 0, right, 0)) p.drawLine(QLineF(left, 0, left, 1)) idx = 0 for t, polygon in self._loadData.items(): pen.setColor(ThreadToColor.singleton.get(t)) p.setPen(pen) p.drawPolyline(polygon) p.end()
def paintEvent(self, event): painter = QPainter() painter.begin(self) painter.setRenderHint(QPainter.Antialiasing) painter.fillRect(event.rect(), self.background_brush) center_position = size_to_point(event.rect().size()) / 2 painter.translate(center_position) painter.scale(self.camera.scale_factor, self.camera.scale_factor) painter.translate(-center_position) # Not using QPoint for smoother movement in large zoom (cause it uses integers instead of floats) painter.translate(*self.camera.position) painter.drawPixmap(QPoint(0., 0.), self.pixmap) painter.end() event.accept()
def drawProgress(self, painter: QPainter) -> None: painter.save() width: int = self.width() height: int = self.height() side: int = min(width, height) radius: int = 99 - self.__borderWidth # 绘制外圆 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) # 平移坐标轴中心, 等比例缩放 rectCircle: QRect = QRect(-radius, -radius, radius * 2, radius * 2) painter.translate(width / 2, height / 2) painter.scale(side / 200.0, side / 200.0) painter.drawEllipse(rectCircle) # 绘制圆弧进度 pen.setWidth(self.__lineWidth) pen.setColor(self.__lineColor) painter.setPen(pen) offset: int = radius - self.__lineWidth - 5 rectArc: QRectF = QRectF(-offset, -offset, offset * 2, offset * 2) startAngle: int = offset * 16 spanAngle: int = int(-self.__value * 16) painter.drawArc(rectArc, startAngle, spanAngle) # 绘制进度文字 font: QFont = QFont() font.setPixelSize(offset - 15) painter.setFont(font) strValue: str = str(int(self.__value) * 100 // 360) + '%' painter.drawText(rectCircle, Qt.AlignCenter, strValue) painter.restore()
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 paint(self, painter: QPainter, option: QStyleOptionGraphicsItem, widget: Optional[QWidget]) -> None: # Draw a circle for the node pen = QPen(Qt.gray) pen.setWidth(2) pen.setCosmetic(True) # Combine ideas from these code samples to draw items at a fixed size: # https://stackoverflow.com/questions/1222914/qgraphicsview-and-qgraphicsitem-don%C2%B4t-scale-item-when-scaling-the-view-rect # https://www.qtcentre.org/threads/28691-Scale-independent-QGraphicsItem object_rect = self.boundingRect() mapped_rect = painter.transform().mapRect(object_rect) width_ratio = object_rect.width() / mapped_rect.width() scale_factor = max(1, width_ratio) painter.setPen(pen) painter.setBrush(Qt.gray) scaled_diameter = self.diameter * scale_factor painter.drawEllipse((self.x - scaled_diameter / 2), (self.y - scaled_diameter / 2), scaled_diameter, scaled_diameter) # Draw text for the node name label_path = QPainterPath() label_font = QFont("Calibri", 10 * scale_factor) label_path.addText(self.x, -self.y - self.diameter / 2, label_font, self.name) painter.scale(1.0, -1.0) painter.setBrush(Qt.blue) painter.setPen(Qt.NoPen) painter.drawPath(label_path)
def paint(self, painter: QPainter, option: QStyleOptionGraphicsItem, widget: QWidget = ...): fillColor = self.color.darker( 150) if option.state & QStyle.State_Selected else self.color if option.state & QStyle.State_MouseOver: fillColor = fillColor.lighter(125) lod = option.levelOfDetailFromTransform(painter.worldTransform()) if lod < 0.2: if lod < 0.125: painter.fillRect(QRectF(0, 0, 110, 70), fillColor) return b = painter.brush() painter.setBrush(fillColor) painter.drawRect(13, 13, 97, 57) painter.setBrush(b) return oldPen = painter.pen() pen = oldPen width = 0 if option.state & QStyle.State_Selected: width += 2 pen.setWidth(width) b = painter.brush() painter.setBrush( QBrush( fillColor.darker(120 if option.state & QStyle.State_Sunken else 100))) painter.drawRect(QRect(14, 14, 79, 39)) painter.setBrush(b) if lod >= 1: painter.setPen(QPen(Qt.gray, 1)) painter.drawLine(15, 54, 94, 54) painter.drawLine(94, 53, 94, 15) painter.setPen(QPen(Qt.black, 0)) # Draw text if lod >= 2: font = QFont("Times", 10) font.setStyleStrategy(QFont.ForceOutline) painter.setFont(font) painter.save() painter.scale(0.1, 0.1) painter.drawText( 170, 180, f"Model: VSC-2000 (Very Small Chip) at {self.x}x{self.y}") painter.drawText(170, 200, "Serial number: DLWR-WEER-123L-ZZ33-SDSJ") painter.drawText(170, 220, "Manufacturer: Chip Manufacturer") painter.restore() # Drawlines lines: List[QLineF] = [] if lod >= 0.5: s = 1 if lod > 0.5 else 2 for i in range(0, 11, s): lines.append(QLineF(18 + 7 * i, 13, 18 + 7 * i, 5)) lines.append(QLineF(18 + 7 * i, 54, 18 + 7 * i, 62)) for i in range(0, 7, s): lines.append(QLineF(5, 18 + i * 5, 13, 18 + i * 5)) lines.append(QLineF(94, 18 + i * 5, 102, 18 + i * 5)) if lod >= 0.4: lines.extend([ QLineF(25, 35, 35, 35), QLineF(35, 30, 35, 40), QLineF(35, 30, 45, 35), QLineF(35, 40, 45, 35), QLineF(45, 30, 45, 40), QLineF(45, 35, 55, 35) ]) painter.drawLines(lines[:]) ## Draw red ink if len(self.stuff) > 1: p = painter.pen() painter.setPen( QPen(Qt.red, 1, Qt.SolidLine, Qt.RoundCap, Qt.RoundJoin)) painter.setBrush(Qt.NoBrush) path = QPainterPath() path.moveTo(self.stuff[0]) for i in range(1, len(self.stuff)): path.lineTo(self.stuff[i]) painter.drawPath(path) painter.setPen(p)
def paintEvent(self, event): hour_hand = [QPoint(7, 8), QPoint(-7, 8), QPoint(0, -40)] minute_hand = [QPoint(7, 8), QPoint(-7, 8), QPoint(0, -70)] second_hand = [QPoint(2, 8), QPoint(-2, 8), QPoint(0, -90)] hour_color = QColor(0x8b, 0, 0xff) minute_color = QColor(0, 0xbf, 0xff, 191) second_color = QColor(0xff, 0x82, 0xab, 191) side = min(self.width(), self.height()) painter = QPainter(self) # pre settings painter.setRenderHint(QPainter.Antialiasing) painter.translate(self.width() / 2, self.height() / 2) painter.scale(side / 200, side / 200) painter.setPen(Qt.NoPen) # draw hour scale & hour hand painter.setBrush(hour_color) painter.save() painter.rotate(30 * (self._hour + self._minute / 60)) painter.drawConvexPolygon(hour_hand) painter.restore() painter.setPen(hour_color) for i in range(12): painter.drawLine(88, 0, 96, 0) painter.rotate(30) painter.setPen(Qt.NoPen) # draw minute scale & minute hand painter.setBrush(minute_color) painter.save() painter.rotate(6 * (self._minute + self._second / 60)) painter.drawConvexPolygon(minute_hand) painter.restore() painter.setPen(minute_color) for i in range(60): if i % 5: painter.drawLine(92, 0, 96, 0) painter.rotate(6.0) painter.setPen(Qt.NoPen) # draw second scale & second hand painter.setBrush(second_color) painter.save() painter.rotate(6 * self._second) painter.drawConvexPolygon(second_hand) painter.restore()
def paintEvent(self, event): painter = QPainter(self) painter.setRenderHint(QPainter.HighQualityAntialiasing) # determine max width/height of all labels if self._maxLabelWidth is None or self._maxLabelHeight is None: self._maxLabelWidth = 0 self._maxLabelHeight = 0 for k in self.labels.values(): b = painter.boundingRect( QRectF(0, 0, 0, 0), int(Qt.AlignLeft) | int(Qt.AlignVCenter), str(k)) self._maxLabelWidth = max(self._maxLabelWidth, b.width()) self._maxLabelHeight = max(self._maxLabelHeight, b.height()) barRect = self.rect() if self._orientation == 'Vertical': # translate Y axis matrix = QMatrix() matrix.translate(0, self.height() - self.margin) matrix.scale(1, -1) painter.setMatrix(matrix) self.gradient.setFinalStop(0, self.height() - self.margin) barRect.setWidth(self._barThickness) barRect.setHeight(barRect.height() - 2 * self.margin) painter.fillRect(barRect, self.gradient) # restore normal coordinates painter.scale(1, -1) self.setMinimumWidth(self._barThickness + self._labelMargin + self._maxLabelWidth) elif self._orientation == 'Horizontal': self.margin = self._maxLabelWidth / 2 + self._labelMargin barRect.setHeight(self._barThickness) barRect.setLeft( self.margin ) # reduces width by margin (as opposed to shifting) # Reduce the width by another margin to pull off the right hand side barRect.setWidth(barRect.width() - self.margin) painter.fillRect(barRect, self.gradient) line_step = barRect.width() / 20 pos = barRect.left() while pos <= barRect.right(): painter.drawLine(pos, 2 * (barRect.bottom() - barRect.top()) / 3, pos, barRect.bottom()) pos += line_step self.setMinimumHeight(self._maxLabelHeight + self._barThickness + self._labelMargin) for pos, label in self.labels.items(): # Figure coordinate position. 1=height-margin for vertical, or width-margin for horizontal if self._orientation == 'Vertical': lpos = -1 * ((self.height() - (2 * self.margin)) * pos) painter.drawText(self._barThickness + self._labelMargin, (.5 * self._maxLabelHeight) + lpos, label) elif self._orientation == 'Horizontal': text_rect = painter.boundingRect(QRectF(0, 0, 0, 0), int(Qt.AlignLeft), str(label)) lpos = ((self.width() - (2 * self.margin)) * pos ) # Center position lleft = lpos - text_rect.width() / 2 painter.drawText(lleft + self.margin, self.height() - 1, label)