我们从Python开源项目中,提取了以下50个代码示例,用于说明如何使用PyQt5.QtCore.QRectF()。
def fontValuePixmap(font): f = QFont(font) img = QImage(16, 16, QImage.Format_ARGB32_Premultiplied) img.fill(0) p = QPainter(img) p.setRenderHint(QPainter.TextAntialiasing, True) p.setRenderHint(QPainter.Antialiasing, True) f.setPointSize(13) p.setFont(f) t = QTextOption() t.setAlignment(Qt.AlignCenter) p.drawText(QRectF(0, 0, 16, 16), 'A', t) p.end() return QPixmap.fromImage(img)
def paint( self, painter: QPainter, option: QStyleOptionGraphicsItem, widget: QWidget): pen = QPen() pen.setWidth(3) painter.setRenderHint(QPainter.Antialiasing) pen.setColor(QColor(61, 61, 61, 255)) painter.setPen(pen) painter.setBrush(QBrush(QColor(61, 61, 61, 255), Qt.SolidPattern)) painter.drawRect( QRectF(-self.maxWidth / 2, -10, self.maxWidth, 20)) painter.setBrush(QBrush(QColor(240, 217, 108, 255), Qt.SolidPattern)) painter.drawRect( QRectF(-self.maxWidth / 2, -10, self.displayWidth, 20)) path = QPainterPath() path.addText(-self.maxWidth / 2, 35, QFont('monospace', 18, QFont.Bold), f'{self.name} Lv.{self.level} Exp. {self.actualExperience:{len(str(self.maxExperience))}}/{self.maxExperience}') # pen.setColor(Qt.white) pen.setWidth(2) painter.setPen(pen) painter.setBrush(QBrush(QColor(0, 0, 0, 61), Qt.SolidPattern)) painter.drawPath(path)
def paint( self, painter: QPainter, option: QStyleOptionGraphicsItem, widget: QWidget): super().paint(painter, option, widget) painter.setPen(const.HeroNamePen) painter.setRenderHint(QPainter.Antialiasing) painter.drawText( QRectF( -self.width / 2 - 15, -self.offsetY - 14, self.width + 30, 14), Qt.AlignCenter, self.name)
def boundingRect(self): """ Override inherited function to enlarge selection of Arrow to include all @param flag: The flag to enable or disable Selection """ if not self.sc: # since this function is called before paint; and scale is unknown return QtCore.QRectF(self.startp.x(), self.startp.y(), 1e-9, 1e-9) arrowSize = self.arrowSize / self.sc extra = arrowSize # self.pen.width() + if self.endp is None: dx = cos(self.angle) * self.length / self.sc dy = sin(self.angle) * self.length / self.sc endp = QtCore.QPointF(self.startp.x() - dx, self.startp.y() + dy) else: endp = QtCore.QPointF(self.endp.x, -self.endp.y) brect = QtCore.QRectF(self.startp, QtCore.QSizeF(endp.x()-self.startp.x(), endp.y()-self.startp.y())).normalized().adjusted(-extra, -extra, extra, extra) return brect
def paint(self, painter, option, widget=None): """ paint() """ painter.setPen(self.pen) demat = painter.deviceTransform() self.sc = demat.m11() diameter1 = self.diameter / self.sc diameter2 = (self.diameter - 4) / self.sc rectangle1 = QtCore.QRectF(-diameter1 / 2, -diameter1 / 2, diameter1, diameter1) rectangle2 = QtCore.QRectF(-diameter2 / 2, -diameter2 / 2, diameter2, diameter2) startAngle1 = 90 * 16 spanAngle = 90 * 16 startAngle2 = 270 * 16 painter.drawEllipse(rectangle1) painter.drawEllipse(rectangle2) painter.drawPie(rectangle2, startAngle1, spanAngle) painter.setBrush(self.color) painter.drawPie(rectangle2, startAngle2, spanAngle)
def setSelected(self, value): """ Sets *value*-th glyph as the selected glyph, or none if *value* is None. *value* should be less than the number of glyphRecords present in the widget. """ self._selected = value if self._selected is not None and self._glyphRecordsRects is not None: scrollArea = self._scrollArea if scrollArea is not None: rect = None for r, indexRecord in self._glyphRecordsRects.items(): if indexRecord == self._selected: rect = QRectF(*r) break if rect is not None: center = rect.center() scrollArea.ensureVisible( center.x(), center.y(), .6 * rect.width(), .6 * rect.height()) self.update()
def drawGlyphBackground(self, painter, glyph, rect, selected=False): if glyph.name == ".notdef": painter.fillRect(QRectF(*rect), self._notdefBackgroundColor) if selected: if self._glyphSelectionColor is not None: selectionColor = self._glyphSelectionColor else: palette = self.palette() active = palette.currentColorGroup() != QPalette.Inactive opacityMultiplier = platformSpecific.colorOpacityMultiplier() selectionColor = palette.color(QPalette.Highlight) selectionColor.setAlphaF( .2 * opacityMultiplier if active else .9) xMin, yMin, width, height = rect painter.save() if self._drawMetrics: pen = painter.pen() pen.setStyle(Qt.DotLine) pen.setColor(self._metricsColor) painter.setPen(pen) drawing.drawLine(painter, xMin, yMin, xMin, yMin + height) drawing.drawLine( painter, xMin + width, yMin, xMin + width, yMin + height) painter.fillRect(xMin, yMin, width, -26, selectionColor) painter.restore()
def paintWindowFrame(self, painter, option, widget): color = QColor(0, 0, 0, 64) r = self.windowFrameRect() right = QRectF(r.right(), r.top()+10, 10, r.height()-10) bottom = QRectF(r.left()+10, r.bottom(), r.width(), 10) intersectsRight = right.intersects(option.exposedRect) intersectsBottom = bottom.intersects(option.exposedRect) if intersectsRight and intersectsBottom: path = QPainterPath() path.addRect(right) path.addRect(bottom) painter.setPen(Qt.NoPen) painter.setBrush(color) painter.drawPath(path) elif intersectsBottom: painter.fillRect(bottom, color) elif intersectsRight: painter.fillRect(right, color) super(CustomProxy, self).paintWindowFrame(painter, option, widget)
def paintEvent(self, event): painter = QPainter(self) painter.setRenderHint(QPainter.Antialiasing, self.antialiased) painter.translate(self.width() / 2, self.height() / 2) for diameter in range(0, 256, 9): delta = abs((self.frameNo % 128) - diameter / 2) alpha = 255 - (delta * delta) / 4 - diameter if alpha > 0: painter.setPen(QPen(QColor(0, diameter / 2, 127, alpha), 3)) if self.floatBased: painter.drawEllipse(QRectF(-diameter / 2.0, -diameter / 2.0, diameter, diameter)) else: painter.drawEllipse(QRect(-diameter / 2, -diameter / 2, diameter, diameter))
def paint(self, painter, event, elapsed): painter.fillRect(event.rect(), self.background) painter.translate(100, 100) painter.save() painter.setBrush(self.circleBrush) painter.setPen(self.circlePen) painter.rotate(elapsed * 0.030) r = elapsed / 1000.0 n = 30 for i in range(n): painter.rotate(30) radius = 0 + 120.0*((i+r)/n) circleRadius = 1 + ((i+r)/n)*20 painter.drawEllipse(QRectF(radius, -circleRadius, circleRadius*2, circleRadius*2)) painter.restore() painter.setPen(self.textPen) painter.setFont(self.textFont) painter.drawText(QRect(-50, -50, 100, 100), Qt.AlignCenter, "Qt")
def moveLetters(self, adjust): adaptedMoveSpeed = self.moveSpeed * adjust adaptedMorphSpeed = self.morphSpeed * adjust self.postEffect.tick(adjust) if self.morphBetweenModels: move_speed = adaptedMoveSpeed morph_speed = adaptedMorphSpeed else: move_speed = Colors.tickerMoveSpeed morph_speed = -1 for letter in self.letters: letter.guideAdvance(move_speed) letter.guideMove(morph_speed) pos = letter.getGuidedPos() self.postEffect.transform(letter, pos) if self.useSheepDog: letter.setPosUsingSheepDog(pos, QRectF(0, 0, 800, 600)) else: letter.setPos(pos)
def boundingRect(self): return QRectF(-self.size/2, -self.size/2, self.size, self.size)
def boundingRect(self): srcPoint = self.mapFromScene(self.__srcPortCircle.centerInSceneCoords()) dstPoint = self.mapFromScene(self.__dstPortCircle.centerInSceneCoords()) penWidth = self.__defaultPen.width() return QtCore.QRectF( min(srcPoint.x(), dstPoint.x()), min(srcPoint.y(), dstPoint.y()), abs(dstPoint.x() - srcPoint.x()), abs(dstPoint.y() - srcPoint.y()), ).adjusted(-penWidth/2, -penWidth/2, +penWidth/2, +penWidth/2)
def addGraphicsItems(self): self.mainRect = QGraphicsRectItem(QRectF(-15, -15, 30, 30), self) self.nodeTitle = QGraphicsTextItem(type(self).name, self) titleFont = QFont() titleFont.setBold(True) self.nodeTitle.setFont(titleFont) self.selectedChanged(self.isSelected())
def boundingRect(self): halfPenWidth = 1.5 return QRectF( -self.axis - halfPenWidth, -self.axis - halfPenWidth, self.axis * 2 + halfPenWidth, self.axis * 2 + halfPenWidth, )
def boundingRect(self): return QRectF(-self.maxWidth / 2, -15, self.maxWidth, 30 + 40)
def boundingRect(self): halfPenWidth = 3 / 2 return QRectF( -self.width - halfPenWidth, -self.width - halfPenWidth, self.width * 2 + 2 * halfPenWidth, self.width * 2 + 2 * halfPenWidth, )
def boundingRect(self): penWidth = 3 return QRectF( - self.width / 2 - penWidth, self.offsetY, self.width + penWidth, 5 + penWidth )
def drawBackground(self, painter: QPainter, rect: QRectF): backgroundColor = QColor(10, 10, 255, 30) painter.setBrush(backgroundColor) painter.setPen(backgroundColor) for i in range(-5, self.width + 20 + 1, 20): painter.drawLine(i, 5, i, self.height + 5) for i in range(-5, self.height + 20 + 1, 20): painter.drawLine(5, i, self.width + 5, i)
def boundingRect(self): halfPenWidth = 2 return QRectF( -self.radius - halfPenWidth, -self.radius - halfPenWidth, self.radius * 2 + halfPenWidth, self.radius * 2 + halfPenWidth, )
def boundingRect(self): return QRectF(-self.width, 0, self.width, self.height)
def boundingRect(self): """ Override inherited function to enlarge selection of Arrow to include all @param flag: The flag to enable or disable Selection """ if not self.sc: # since this function is called before paint; and scale is unknown return QtCore.QRectF(0, 0, 1e-9, 1e-9) diameter = self.diameter / self.sc return QtCore.QRectF(-diameter / 2, -diameter / 2, diameter, diameter)
def autoscale(self): """ Automatically zooms to the full extend of the current GraphicsScene """ scene = self.scene() width = scene.BB.Pe.x - scene.BB.Ps.x height = scene.BB.Pe.y - scene.BB.Ps.y scext = QtCore.QRectF(scene.BB.Ps.x, -scene.BB.Pe.y, width * 1.05, height * 1.05) self.fitInView(scext, QtCore.Qt.KeepAspectRatio) logger.debug(self.tr("Autoscaling to extend: %s") % scext)
def boundingRect(self): """ Required method for painting. Inherited by Painterpath @return: Gives the Bounding Box """ rect = self.path.boundingRect().getRect() newrect = QtCore.QRectF(self.startp.x()+rect[0]/self.sc, self.startp.y()+rect[1]/self.sc, rect[2]/self.sc, rect[3]/self.sc) return newrect
def paintEvent(self, q_paint_event): # board background color painter = QtGui.QPainter(self) bg_path = QtGui.QPainterPath() bg_path.addRect(0, 0, self.width(), self.height()) painter.fillPath(bg_path, GREEN) # draw the board lines for i in range(8): x_pos = self.width() / 8 * i painter.drawLine(x_pos, 0, x_pos, self.height()) y_pos = self.height() / 8 * i painter.drawLine(0, y_pos, self.width(), y_pos) if self.board is not None and len(self.board) >= 8 * 8: for h in range(8): for w in range(8): pieces_path = QtGui.QPainterPath() w_dist = self.width() / 8 h_dist = self.height() / 8 x_pos = w_dist * w y_pos = h_dist * h bounding_rect = QtCore.QRectF(x_pos, y_pos, w_dist, h_dist) index = (h * 8) + w piece = self.board[index] if piece == 'O': pieces_path.addEllipse(bounding_rect) painter.fillPath(pieces_path, WHITE) elif piece == 'X': pieces_path.addEllipse(bounding_rect) painter.fillPath(pieces_path, BLACK)
def boundingRect(self): if not self.source or not self.dest: return QRectF() penWidth = 1.0 extra = (penWidth + self.arrowSize) / 2.0 return QRectF(self.sourcePoint, QSizeF(self.destPoint.x() - self.sourcePoint.x(), self.destPoint.y() - self.sourcePoint.y()) ).normalized().adjusted(-extra, -extra, extra, extra)
def __init__(self): QGraphicsView.__init__(self) self.setScene(GraphicsScene()) self.setSceneRect(QRectF(self.viewport().rect())) self.PIX = QPixmap() self.timer = QTimer() self.timer.timeout.connect(self.proccessImage) self.path = QPainterPath() self.item = GraphicsPathItem() self.scene().addItem(self.item)
def mousePressEvent(self, event): if event.button() == Qt.LeftButton: selected = None for rect, recordIndex in self._glyphRecordsRects.items(): if QRectF(*rect).contains(event.localPos()): selected = recordIndex if self._selected == selected: return self._selected = selected self.selectionModified.emit(self._selected) self.update() else: super(GlyphLineWidget, self).mousePressEvent(event)
def render_pdf(self, pages, path): """Render the document to a pdf file. Args: pages (iter[int]): The page numbers to render path (str): An output file path. Warning: If the file at `path` already exists, it will be overwritten. """ printer = QPrinter() printer.setOutputFormat(QPrinter.PdfFormat) printer.setOutputFileName(os.path.realpath(path)) printer.setResolution(config.PRINT_DPI) printer.setPageLayout(self.document.paper._to_interface()) painter = QtGui.QPainter() painter.begin(printer) # Scaling ratio for Qt point 72dpi -> config.PRINT_DPI ratio = (config.PRINT_DPI / 72) target_rect_unscaled = printer.paperRect(QPrinter.Point) target_rect_scaled = QtCore.QRectF( target_rect_unscaled.x() * ratio, target_rect_unscaled.y() * ratio, target_rect_unscaled.width() * ratio, target_rect_unscaled.height() * ratio, ) for page_number in pages: source_rect = rect_to_qt_rect_f( self.document.paper_bounding_rect(page_number)) self.scene.render(painter, target=target_rect_scaled, source=source_rect) printer.newPage() painter.end()
def calculate_clipping_area(bounding_rect, clip_start_x, clip_width, extra_padding): """Create a QRectF giving the painting area for the object. Args: bounding_rect (QRectF): The full shape's bounding rectangle clip_start_x (Unit or None): The local starting position for the clipping region. Use `None` to render from the start. clip_width (Unit or None): The width of the clipping region. Use `None` to render to the end extra_padding (float): Extra area padding to be added to all sides of the clipping area. This might be useful, for instance, for making sure thick pen strokes render completely. Returns: QRectF """ clip_start_x = (bounding_rect.x() if clip_start_x is None else GraphicUnit(clip_start_x).value) clip_width = ( bounding_rect.width() - (clip_start_x - bounding_rect.x()) if clip_width is None else GraphicUnit(clip_width).value) padding = GraphicUnit(extra_padding).value return QtCore.QRectF(clip_start_x - padding, bounding_rect.y() - padding, clip_width + (padding * 2), bounding_rect.height() + (padding * 2))
def calculate_bounding_rect(bounding_rect, clip_start_x, clip_width, extra_padding): """Create a QRectF giving the bounding rect for the path. Args: bounding_rect (QRectF): The full shape's bounding rectangle clip_start_x (Unit or None): The local starting position for the clipping region. Use `None` to render from the start. clip_width (Unit or None): The width of the clipping region. Use `None` to render to the end extra_padding (float): Extra area padding to be added to all sides of the clipping area. This might be useful, for instance, for making sure thick pen strokes render completely. Returns: QRectF """ clip_start_x = (bounding_rect.x() if clip_start_x is None else GraphicUnit(clip_start_x).value) clip_width = (bounding_rect.width() - clip_start_x if clip_width is None else GraphicUnit(clip_width).value) padding = GraphicUnit(extra_padding).value return QtCore.QRectF(-padding, -padding, clip_width + (padding * 2), bounding_rect.height() + (padding * 2))
def calculate_bounding_rect(self): rect = super().boundingRect() rect.translate(self.origin_offset * -1) scaled_rect = QtCore.QRectF(rect.x() * self.scale_factor, rect.y() * self.scale_factor, rect.width() * self.scale_factor, rect.height() * self.scale_factor) return scaled_rect
def rect_to_qt_rect_f(rect): """Create a QRectF from a Rect Args: rect (Rect): The source rect Returns: QRectF """ return QRectF(unit_to_qt_float(rect.x), unit_to_qt_float(rect.y), unit_to_qt_float(rect.width), unit_to_qt_float(rect.height))
def test_qt_rect_to_rect_with_q_rect_f(): rect = qt_rect_to_rect(QRectF(1.5, 2.5, 3.5, 4.5)) assert(isinstance(rect, Rect)) assert(rect.x == 1.5) assert(rect.y == 2.5) assert(rect.width == 3.5) assert(rect.height == 4.5)
def test_qt_rect_to_rect_with_q_rect_f_and_unit_class(): rect = qt_rect_to_rect(QRectF(1.5, 2.5, 3.5, 4.5), GraphicUnit) assert(isinstance(rect, Rect)) assert(rect.x == GraphicUnit(1.5)) assert(rect.y == GraphicUnit(2.5)) assert(rect.width == GraphicUnit(3.5)) assert(rect.height == GraphicUnit(4.5)) # rect_to_qt_rect #############################################################
def test_rect_to_qt_rect_f(): qrect = rect_to_qt_rect_f(Rect(GraphicUnit(1.2), GraphicUnit(2.2), GraphicUnit(3.2), GraphicUnit(4.2))) assert(isinstance(qrect, QRectF)) assert(qrect.x() == 1.2) assert(qrect.y() == 2.2) assert(qrect.width() == 3.2) assert(qrect.height() == 4.2)
def boundingRect(self): return QRectF(-35, -81, 70, 115)
def paint(self, painter): painter.setPen(QPen(self._color, 2)) painter.setRenderHints(QPainter.Antialiasing, True) rect = QRectF(0, 0, self.width(), self.height()).adjusted(1, 1, -1, -1) painter.drawPie(rect, 90 * 16, 290 * 16)