def point_list_to_polygon(point_list): # type: (Iterable[Point]) -> QPolygon """Converts an iterable of (x, y) points coordinates to a QPolygon suitable for rendering. :param point_list: ordered iterable of points that make up the polygon :return: a polygon with the given points """ polygon = QPolygon() for point in point_list: polygon.push_back(QPoint(*point)) return polygon
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 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 paintEvent(self, event): rect = QRect(0, 0, self.w - 1, self.h - 1) p = QPainter(self) p.setPen(QPen(Qt.black)) p.setBrush(self.color) if self.type == 1: p.drawRect(0, 0, self.w - 1, self.h - 1) p.drawText( rect, Qt.AlignCenter, self.text ) # 이게 더 좋은데? 어떤 방식이지? => # w.setGeometry(self.geometry()) 차이점이 무엇이지? elif self.type == 2: p.drawEllipse(0, 0, self.w - 1, self.h - 1) p.drawText(rect, Qt.AlignCenter, self.text) elif self.type == 3: p.drawEllipse(0, 0, self.w - 1, self.h - 1) p.drawText(rect, Qt.AlignCenter, self.text) elif self.type == 4: points = [ QPoint(self.w / 2, 0), QPoint(0, self.h / 2), QPoint(self.w / 2, self.h), QPoint(self.w, self.h / 2) ] poly = QPolygon(points) p.drawPolygon(poly) self.update() # self.update()
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 AnalogClock(QWidget): hourHand = QPolygon([QPoint(7, 8), QPoint(-7, 8), QPoint(0, -40)]) minuteHand = QPolygon([QPoint(7, 8), QPoint(-7, 8), QPoint(0, -70)]) hourColor = QColor(255, 0, 127) minuteColor = QColor(0, 127, 127, 255) def __init__(self, parent=None): QWidget.__init__(self) timer = QTimer(self) timer.timeout.connect(self.update) timer.start(1000) self.setWindowTitle("Analog Clock") self.resize(200, 200) 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 paintEvent(self, event): painter = QPainter(self) painter.setPen(QPen(Qt.black, 5, Qt.SolidLine)) painter.setBrush(QBrush(Qt.black)) points = [ QPoint(100, 0), QPoint(0, 50), QPoint(100, 100), QPoint(200, 50) ] poly = QPolygon(points) painter.drawPolygon(poly)
def __drawTriangle(self, painter, x, y): brush = QBrush(QColor(255, 255, 255, 160), Qt.SolidPattern) if not self.isCollapsed(): tl, tr, tp = ( QPoint(x + 9, y + 8), QPoint(x + 19, y + 8), QPoint(x + 14, y + 13.0), ) points = [tl, tr, tp] triangle = QPolygon(points) else: tl, tr, tp = ( QPoint(x + 11, y + 6), QPoint(x + 16, y + 11), QPoint(x + 11, y + 16.0), ) points = [tl, tr, tp] triangle = QPolygon(points) currentBrush = painter.brush() painter.setBrush(brush) painter.drawPolygon(triangle) painter.setBrush(currentBrush)
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 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, e): painter = QPainter(self) painter.setPen(QPen(Qt.black, 2, Qt.DashDotLine)) painter.setBrush(QBrush(Qt.green, Qt.DiagCrossPattern)) painter.drawEllipse(100, 100, 400, 200) painter.drawRect(200, 150, 400, 200) points = QPolygon([ QPoint(10, 10), QPoint(10, 100), QPoint(100, 10), QPoint(100, 80) ]) painter.drawPolygon(points)
def paintEvent(self, e): painter = QPainter(self) painter.setPen(QPen(Qt.blue, 7, Qt.DashDotLine)) painter.setBrush(QBrush(Qt.green, Qt.SolidPattern)) painter.drawEllipse(100, 100, 400, 200) points = QPolygon([ QPoint(10, 10), QPoint(10, 100), QPoint(100, 100), QPoint(100, 10), ]) painter.drawPolygon(points)
def drawRobot(self, robot, color): # Position computing relativePoint = self.getRealFromUiCoord(robot.getPosition().x,robot.getPosition().y) w_tr = robot.getWidth()/(2*self.scaleFactor) l_tr = robot.getLength()/(4*self.scaleFactor) x_center = relativePoint.x # w_tr y_center = relativePoint.y #- l_tr pts_list = QPoint(-w_tr,-l_tr), QPoint(-w_tr,l_tr), QPoint(0,l_tr*1.2), QPoint(w_tr,l_tr),QPoint(w_tr,-l_tr),QPoint(-w_tr,-l_tr) polyst = self.rotatePolygon(pts_list,robot.getAngle()) poly = QPolygon(polyst) poly.translate(x_center,y_center) # Draw Item myPen = QPen() myPen.setBrush(QBrush(QColor(color))) myPen.setWidth(5) robotP = QPainter() robotP.begin(self) robotP.setPen(myPen) robotP.drawPolyline(poly) robotP.end()
def polyLine(self, state: PolyLine): LOG.debug(state) p = self._paint(self.surface) p.setPen(QPen(rgb_to_qcolor(state.penColor))) set_rop2(state.rop2, p) polygon = QPolygon() polygon.append(QPoint(state.x0, state.y0)) for (x, y) in state.points: polygon.append(QPoint(x, y)) p.drawPolyline(polygon) self._end(p)
def polygonSc(self, state: PolygonSc): LOG.debug(state) p = self._paint(self.surface) p.setBrush(QBrush(rgb_to_qcolor(state.brushColor))) set_rop2(state.rop2, p) polygon = QPolygon() polygon.append(QPoint(state.x0, state.y0)) for (x, y) in state.points: polygon.append(QPoint(x, y)) p.drawPolygon(polygon, _fill[state.fillMode]) self._end(p)
def draw_needle(self, painter): painter.setBrush(self.indicator_brush) painter.setPen(self.indicator_pen) painter.save() painter.translate(self.width() / 2, self.height() / 2) angle = map_value(self.value, self.config["min"], self.config["max"], 0, 270) angle = min(angle, 270) angle -= 90 + 45 painter.rotate(angle) painter.drawEllipse(QPoint(0, 0), 5, 5) painter.drawPolygon( QPolygon([ QPoint(-5, 0), QPoint(0, -self.__needle_l), QPoint(5, 0), QPoint(-5, 0) ])) 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 drawTriangle(self, painter: QPainter) -> None: """ """ if not self.__showTriangle: return # 选中或者悬停显示 if (not self.__hover) and (not self.isChecked()): 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 == NavButton.TrianglePosition.TRIANGLEPOSITION_LEFT: pts.setPoints(3, self.__triangleLen, midHeight, 0, midHeight - self.__triangleLen, 0, midHeight + self.__triangleLen) elif self.__trianglePosition == NavButton.TrianglePosition.TRIANGLEPOSITION_RIGHT: pts.setPoints(3, width - self.__triangleLen, midHeight, width, midHeight - self.__triangleLen, width, midHeight + self.__triangleLen) elif self.__trianglePosition == NavButton.TrianglePosition.TRIANGLEPOSITION_TOP: pts.setPoints(3, midWidth, self.__triangleLen, midWidth - self.__triangleLen, 0, midWidth + self.__triangleLen, 0) elif self.__trianglePosition == NavButton.TrianglePosition.TRIANGLEPOSITION_BOTTOM: pts.setPoints(3, midWidth, height - self.__triangleLen, midWidth - self.__triangleLen, height, midWidth + self.__triangleLen, height) painter.drawPolygon(pts) painter.restore()
def shape(self) -> QPainterPath: """ Returns the shape of the Port as a QPainterPath. Ports are shaped like an inverted, elongated pentagon. :return: The shape of the Port as a QPainterPath. :rtype: QPainterPath """ # Create the polygon (pentagon-like shape) poly = QPolygon() poly << QPoint(PortGraphics.X_POS, PortGraphics.Y_POS) poly << QPoint(PortGraphics.X_POS + PortGraphics.WIDTH, PortGraphics.Y_POS) poly << QPoint(PortGraphics.X_POS + PortGraphics.WIDTH, PortGraphics.Y_POS + PortGraphics.SIDE_HEIGHT) poly << QPoint(0, PortGraphics.Y_POS + PortGraphics.TOTAL_HEIGHT) poly << QPoint(PortGraphics.X_POS, PortGraphics.Y_POS + PortGraphics.SIDE_HEIGHT) poly << QPoint(PortGraphics.X_POS, PortGraphics.Y_POS) path = QPainterPath() path.addPolygon(poly) return path
def drawPolygon(self, painter, data): painter.save() pen = QPen() pen.setColor(self.lineColor) pen.setWidth(self.lineWidth) painter.setPen(pen) if data['selected']: painter.setBrush(QBrush(self.selectColor)) else: painter.setBrush(QBrush(self.polygonColor)) polygon = QPolygon(data['pos']) painter.drawPolygon(polygon) # dot if self.selectDotVisible and data['selected']: painter.setPen(Qt.NoPen) painter.setBrush(QBrush(self.dotColor)) for point in data['pos']: painter.drawEllipse(point, self.dotRadius, self.dotRadius) painter.restore()
def drawPointerTriangle(self, painter: QPainter) -> None: if not self.__showPointer: return radius: int = 20 offset: int = self.__radiusCircle - 25 painter.save() painter.setPen(Qt.NoPen) painter.setBrush(self.__progressColor) pts: QPolygon = QPolygon() pts.append(QPoint(-radius // 2, offset)) pts.append(QPoint(radius // 2, offset)) pts.append(QPoint(0, radius + offset)) 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) painter.restore()
def polygonCb(self, state: PolygonCb): LOG.debug(state) p = self._paint(self.surface) self._brush(state.brush) p.brush().setColor(rgb_to_qcolor(state.fg)) p.setBackground(QBrush(rgb_to_qcolor(state.bg))) set_rop2(state.rop2, p) # Handle background mode. if state.brush.style in [BrushStyle.PATTERN, BrushStyle.HATCHED]: p.setBackgroundMode(Qt.TransparentMode if state.bgMode == BACKMODE_TRANSPARENT else Qt.OpaqueMode) polygon = QPolygon() polygon.append(QPoint(state.x0, state.y0)) for (x, y) in state.points: polygon.append(QPoint(x, y)) p.drawPolygon(polygon, _fill[state.fillMode]) self._end(p)
def roundedPathFromPolygon(poly:QtGui.QPolygon, roundedRadius:10): """generate a rounded path from polygon vertices this is actually really hard""" poly.toList()
class AnalogClock(QWidget): hourHand = QPolygon([ QPoint(7, 8), QPoint(-7, 8), QPoint(0, -40) ]) minuteHand = QPolygon([ QPoint(7, 8), QPoint(-7, 8), QPoint(0, -70) ]) secondHand = QPolygon([ QPoint(2, 4), QPoint(-2, 4), QPoint(0, -75) ]) hourColor = QColor(200, 0, 200) minuteColor = QColor(0, 200, 200, 191) secondColor = QColor(200, 200, 200, 100) def __init__(self, clock): super(AnalogClock, self).__init__(clock) self.setObjectName(u"analogClock") self.setGeometry(QRect(20, 80, 350, 350)) # self.setAutoFillBackground(True) def paintEvent(self, event): """Update the clock by re-painting it.""" 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) painter.setPen(Qt.NoPen) painter.setBrush(AnalogClock.secondColor) painter.save() painter.rotate(6.0*time.second()) painter.drawConvexPolygon(AnalogClock.secondHand) painter.restore()
class RenderArea(QWidget): points = QPolygon( [QPoint(10, 80), QPoint(20, 10), QPoint(80, 30), QPoint(90, 70)]) Line, Points, Polyline, Polygon, Rect, RoundedRect, Ellipse, Arc, Chord, \ Pie, Path, Text, Pixmap = range(13) def __init__(self, parent=None): super(RenderArea, self).__init__(parent) self.pen = QPen() self.brush = QBrush() self.pixmap = QPixmap() self.shape = RenderArea.Polygon self.antialiased = False self.transformed = False self.pixmap.load(':/images/qt-logo.png') self.setBackgroundRole(QPalette.Base) self.setAutoFillBackground(True) def minimumSizeHint(self): return QSize(100, 100) def sizeHint(self): return QSize(400, 200) def setShape(self, shape): self.shape = shape self.update() def setPen(self, pen): self.pen = pen self.update() def setBrush(self, brush): self.brush = brush self.update() def setAntialiased(self, antialiased): self.antialiased = antialiased self.update() def setTransformed(self, transformed): self.transformed = transformed self.update() 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 setUp(self): self.original = QPolygon([QPoint(1, 2), QPoint(3, 4), QPoint(5, 6)])
class AnalogClock(QWidget): hour_pin = QPolygon([QPoint(2, 5), QPoint(-2, 5), QPoint(0, -45)]) minute_pin = QPolygon([ QPoint(2, 5), QPoint(-2, 5), QPoint(0, -75), ]) hour_pin_color = QColor('#da8602') min_pin_color = QColor('#2196f3') background_color = QColor(255, 255, 255, 32) edge_color = QColor(255, 255, 255, 96) text_color = QColor(120, 255, 255, 128) def __init__(self, parent=None, size=basic_size): super(AnalogClock, self).__init__(parent) self._time = QTime.currentTime() self.setAttribute(Qt.WA_TranslucentBackground) if size is None: size = basic_size self.resize(size, size) else: if size < basic_size: size = basic_size self.resize(size, size) font = QFont() font.setStyleHint(QFont.SansSerif) font.setFamily('monospace') font.setPointSize(12) self.font = font @property def time(self): return self._time @time.setter def time(self, value: QTime): self._time = value self.update_frame() def paintEvent(self, event): side = min(self.width(), self.height()) curr_time = self._time painter = QPainter() painter.begin(self) painter.setRenderHint(QPainter.Antialiasing) painter.translate(self.width() / 2, self.height() / 2) # draw clock frame painter.setBrush(QBrush(self.background_color)) r = side / 2 painter.drawEllipse(QPoint(0, 0), side / 2, side / 2) for i in range(0, 12): x, y = self.rotate_point(0, -r * 0.95, i * 360 / 12) painter.drawEllipse(x - 3, y - 3, 6, 6) painter.setPen(self.text_color) for i in range(0, 12): x, y = self.rotate_point(0, -r * 0.85, i * 360 / 12) painter.drawText(QRect(x - 10, y - 10, 20, 20), Qt.AlignCenter, "%d" % i) painter.setPen(self.background_color) painter.setBrush(QBrush(self.min_pin_color)) for j in range(0, 60): if j % 5 != 0: x, y = self.rotate_point(0, -r * 0.95, j * 360 / 60) painter.drawEllipse(x - 1, y - 1, 2, 2) painter.setClipping(False) # draw hands painter.setBrush(QBrush(self.hour_pin_color)) painter.save() self.hour_pin[2] = QPoint(0, int(-r * 0.6)) painter.rotate(30.0 * (curr_time.hour() + curr_time.minute() / 60.0)) painter.drawConvexPolygon(self.hour_pin) painter.restore() painter.setBrush(QBrush(self.min_pin_color)) painter.save() self.minute_pin[2] = QPoint(0, int(-r * 0.9)) painter.rotate(6.0 * (curr_time.minute() + curr_time.second() / 60.0)) painter.drawConvexPolygon(self.minute_pin) painter.restore() painter.end() @QtCore.Slot() def update_frame(self): time = QTime.currentTime() if time.second() % 10 == 0: self.update() @staticmethod def rotate_point(x, y, degree): theta = degree * math.pi / 180 sin = math.sin(theta) cos = math.cos(theta) return x * cos - y * sin, x * sin + y * cos
def testPolygonShiftOperators(self): p = QPolygon() self.assertEqual(len(p), 0) p << QPoint(10, 20) << QPoint( 20, 30) << [QPoint(20, 30), QPoint(40, 50)] self.assertEqual(len(p), 4)