本文整理匯總了Python中qwt.qt.QtGui.QPainter類的典型用法代碼示例。如果您正苦於以下問題:Python QPainter類的具體用法?Python QPainter怎麽用?Python QPainter使用的例子?那麽, 這裏精選的類代碼示例或許可以為您提供幫助。
在下文中一共展示了QPainter類的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。
示例1: paintEvent
def paintEvent(self, event):
painter = QPainter(self)
painter.setClipRegion(event.region())
opt = QStyleOption()
opt.initFrom(self)
self.style().drawPrimitive(QStyle.PE_Widget, opt, painter, self)
self.draw(painter)
示例2: fillPixmap
def fillPixmap(self, widget, pixmap, offset=None):
"""
Fill a pixmap with the content of a widget
In Qt >= 5.0 `QPixmap.fill()` is a nop, in Qt 4.x it is buggy
for backgrounds with gradients. Thus `fillPixmap()` offers
an alternative implementation.
:param QWidget widget: Widget
:param QPixmap pixmap: Pixmap to be filled
:param QPoint offset: Offset
.. seealso::
:py:meth:`QPixmap.fill()`
"""
if offset is None:
offset = QPoint()
rect = QRect(offset, pixmap.size())
painter = QPainter(pixmap)
painter.translate(-offset)
autoFillBrush = widget.palette().brush(widget.backgroundRole())
if not (widget.autoFillBackground() and autoFillBrush.isOpaque()):
bg = widget.palette().brush(QPalette.Window)
qwtFillRect(widget, painter, rect, bg)
if widget.autoFillBackground():
qwtFillRect(widget, painter, rect, autoFillBrush)
if widget.testAttribute(Qt.WA_StyledBackground):
painter.setClipRegion(rect)
opt = QStyleOption()
opt.initFrom(widget)
widget.style().drawPrimitive(QStyle.PE_Widget, opt,
painter, widget)
示例3: toImage
def toImage(self, *args):
if len(args) == 0:
if self.isNull():
return QImage()
sz = self.defaultSize()
w = np.ceil(sz.width())
h = np.ceil(sz.height())
image = QImage(w, h, QImage.Format_ARGB32)
image.fill(0)
r = QRect(0, 0, sz.width(), sz.height())
painter = QPainter(image)
self.render(painter, r, Qt.KeepAspectRatio)
painter.end()
return image
elif len(args) in (1, 2):
size = args[0]
aspectRatioMode = Qt.IgnoreAspectRatio
if len(args) == 2:
aspectRatioMode = args[-1]
image = QImage(size, QImage.Format_ARGB32_Premultiplied)
image.fill(0)
r = QRect(0, 0, size.width(), size.height())
painter = QPainter(image)
self.render(painter, r, aspectRatioMode)
return image
示例4: legendIcon
def legendIcon(self, index, size):
"""
:param int index: Index of the legend entry (ignored as there is only one)
:param QSizeF size: Icon size
:return: Icon representing the marker on the legend
.. seealso::
:py:meth:`qwt.plot.QwtPlotItem.setLegendIconSize()`,
:py:meth:`qwt.plot.QwtPlotItem.legendData()`
"""
if size.isEmpty():
return QwtGraphic()
icon = QwtGraphic()
icon.setDefaultSize(size)
icon.setRenderHint(QwtGraphic.RenderPensUnscaled, True)
painter = QPainter(icon)
painter.setRenderHint(QPainter.Antialiasing,
self.testRenderHint(QwtPlotItem.RenderAntialiased))
if self.__data.style != QwtPlotMarker.NoLine:
painter.setPen(self.__data.pen)
if self.__data.style in (QwtPlotMarker.HLine, QwtPlotMarker.Cross):
y = .5*size.height()
QwtPainter.drawLine(painter, 0., y, size.width(), y)
if self.__data.style in (QwtPlotMarker.VLine, QwtPlotMarker.Cross):
x = .5*size.width()
QwtPainter.drawLine(painter, x, 0., x, size.height())
if self.__data.symbol:
r = QRect(0, 0, size.width(), size.height())
self.__data.symbol.drawSymbol(painter, r)
return icon
示例5: borderPath
def borderPath(self, rect):
"""
Calculate the painter path for a styled or rounded border
When the canvas has no styled background or rounded borders
the painter path is empty.
:param QRect rect: Bounding rectangle of the canvas
:return: Painter path, that can be used for clipping
"""
if self.testAttribute(Qt.WA_StyledBackground):
recorder = QwtStyleSheetRecorder(rect.size())
painter = QPainter(recorder)
opt = QStyleOption()
opt.initFrom(self)
opt.rect = rect
self.style().drawPrimitive(QStyle.PE_Widget, opt, painter, self)
painter.end()
if not recorder.background.path.isEmpty():
return recorder.background.path
if not recorder.border.rectList.isEmpty():
return qwtCombinePathList(rect, recorder.border.pathlist)
elif self.__data.borderRadius > 0.:
fw2 = self.frameWidth()*.5
r = QRectF(rect).adjusted(fw2, fw2, -fw2, -fw2)
path = QPainterPath()
path.addRoundedRect(r, self.__data.borderRadius,
self.__data.borderRadius)
return path
return QPainterPath()
示例6: defaultIcon
def defaultIcon(brush, size):
icon = QwtGraphic()
if not size.isEmpty():
icon.setDefaultSize(size)
r = QRectF(0, 0, size.width(), size.height())
painter = QPainter(icon)
painter.fillRect(r, brush)
return icon
示例7: toImage
def toImage(self, *args):
"""
.. py:method:: toImage()
Convert the graphic to a `QImage`
All pixels of the image get initialized by 0 ( transparent )
before the graphic is scaled and rendered on it.
The format of the image is `QImage.Format_ARGB32_Premultiplied`.
The size of the image is the default size ( ceiled to integers )
of the graphic.
:return: The graphic as image in default size
.. py:method:: toImage(size, [aspectRatioMode=Qt.IgnoreAspectRatio])
Convert the graphic to a `QImage`
All pixels of the image get initialized by 0 ( transparent )
before the graphic is scaled and rendered on it.
The format of the image is `QImage.Format_ARGB32_Premultiplied`.
:param QSize size: Size of the image
:param `Qt.AspectRatioMode` aspectRatioMode: Aspect ratio how to scale the graphic
:return: The graphic as image
.. seealso::
:py:meth:`toPixmap()`, :py:meth:`render()`
"""
if len(args) == 0:
if self.isNull():
return QImage()
sz = self.defaultSize()
w = np.ceil(sz.width())
h = np.ceil(sz.height())
image = QImage(w, h, QImage.Format_ARGB32)
image.fill(0)
r = QRect(0, 0, sz.width(), sz.height())
painter = QPainter(image)
self.render(painter, r, Qt.KeepAspectRatio)
painter.end()
return image
elif len(args) in (1, 2):
size = args[0]
aspectRatioMode = Qt.IgnoreAspectRatio
if len(args) == 2:
aspectRatioMode = args[-1]
image = QImage(size, QImage.Format_ARGB32_Premultiplied)
image.fill(0)
r = QRect(0, 0, size.width(), size.height())
painter = QPainter(image)
self.render(painter, r, aspectRatioMode)
return image
示例8: drawSeries
def drawSeries(self, seriesItem, from_, to):
"""When observing an measurement while it is running, new points have
to be added to an existing seriesItem. drawSeries() can be used to
display them avoiding a complete redraw of the canvas.
Setting plot().canvas().setAttribute(Qt.WA_PaintOutsidePaintEvent, True)
will result in faster painting, if the paint engine of the canvas widget
supports this feature."""
if seriesItem is None or seriesItem.plot() is None:
return
canvas = seriesItem.plot().canvas()
canvasRect = canvas.contentsRect()
plotCanvas = canvas #XXX: cast to QwtPlotCanvas
if plotCanvas and qwtHasBackingStore(plotCanvas):
painter = QPainter(plotCanvas.backingStore()) #XXX: cast plotCanvas.backingStore() to QPixmap
if self.__data.hasClipping:
painter.setClipRegion(self.__data.clipRegion)
qwtRenderItem(painter, canvasRect, seriesItem, from_, to)
if self.testAttribute(self.FullRepaint):
plotCanvas.repaint()
return
immediatePaint = True
if not canvas.testAttribute(Qt.WA_WState_InPaintEvent):
if QT_VERSION >= 0x050000 or\
not canvas.testAttribute(Qt.WA_PaintOutsidePaintEvent):
immediatePaint = False
if immediatePaint:
if not self.__data.painter.isActive():
self.reset()
self.__data.painter.begin(canvas)
canvas.installEventFilter(self)
if self.__data.hasClipping:
self.__data.painter.setClipRegion(
QRegion(canvasRect) & self.__data.clipRegion)
elif not self.__data.painter.hasClipping():
self.__data.painter.setClipRect(canvasRect)
qwtRenderItem(self.__data.painter,
canvasRect, seriesItem, from_, to)
if self.__data.attributes & self.AtomicPainter:
self.reset()
elif self.__data.hasClipping:
self.__data.painter.setClipping(False)
else:
self.reset()
self.__data.seriesItem = seriesItem
self.__data.from_ = from_
self.__data.to = to
clipRegion = QRegion(canvasRect)
if self.__data.hasClipping:
clipRegion &= self.__data.clipRegion
canvas.installEventFilter(self)
canvas.repaint(clipRegion)
canvas.removeEventFilter(self)
self.__data.seriesItem = None
示例9: drawSymbols
def drawSymbols(self, painter, points, numPoints=None):
"""
Render an array of symbols
Painting several symbols is more effective than drawing symbols
one by one, as a couple of layout calculations and setting of pen/brush
can be done once for the complete array.
:param QPainter painter: Painter
:param QPolygonF points: Positions of the symbols in screen coordinates
"""
#TODO: remove argument numPoints (not necessary in `PythonQwt`)
if numPoints is not None and numPoints <= 0:
return
useCache = False
if QwtPainter.roundingAlignment(painter) and\
not painter.transform().isScaling():
if self.__data.cache.policy == QwtSymbol.Cache:
useCache = True
elif self.__data.cache.policy == QwtSymbol.AutoCache:
if painter.paintEngine().type() == QPaintEngine.Raster:
useCache = True
else:
if self.__data.style in (QwtSymbol.XCross, QwtSymbol.HLine,
QwtSymbol.VLine, QwtSymbol.Cross):
pass
elif self.__data.style == QwtSymbol.Pixmap:
if not self.__data.size.isEmpty() and\
self.__data.size != self.__data.pixmap.pixmap.size():
useCache = True
else:
useCache = True
if useCache:
br = QRect(self.boundingRect())
rect = QRect(0, 0, br.width(), br.height())
if self.__data.cache.pixmap.isNull():
self.__data.cache.pixmap = QwtPainter.backingStore(None, br.size())
self.__data.cache.pixmap.fill(Qt.transparent)
p = QPainter(self.__data.cache.pixmap)
p.setRenderHints(painter.renderHints())
p.translate(-br.topLeft())
pos = QPointF()
self.renderSymbols(p, pos, 1)
dx = br.left()
dy = br.top()
for point in points:
left = round(point.x())+dx
top = round(point.y())+dy
painter.drawPixmap(left, top, self.__data.cache.pixmap)
else:
painter.save()
self.renderSymbols(painter, points, numPoints)
painter.restore()
示例10: paintEvent
def paintEvent(self, event):
painter = QPainter(self)
if not self.contentsRect().contains(event.rect()):
painter.save()
painter.setClipRegion(event.region() & self.frameRect())
self.drawFrame(painter)
painter.restore()
painter.setClipRegion(event.region() & self.contentsRect())
self.drawContents(painter)
示例11: qwtPathGraphic
def qwtPathGraphic(path, pen, brush):
graphic = QwtGraphic()
graphic.setRenderHint(QwtGraphic.RenderPensUnscaled)
painter = QPainter(graphic)
painter.setPen(pen)
painter.setBrush(brush)
painter.drawPath(path)
painter.end()
return graphic
示例12: renderDocument
def renderDocument(self, plot, filename, sizeMM=(300, 200), resolution=85,
format_=None):
if isinstance(sizeMM, tuple):
sizeMM = QSizeF(*sizeMM)
if format_ is None:
ext = osp.splitext(filename)[1]
if not ext:
raise TypeError("Unable to determine target format from filename")
format_ = ext[1:]
if plot is None or sizeMM.isEmpty() or resolution <= 0:
return
title = plot.title().text()
if not title:
title = "Plot Document"
mmToInch = 1./25.4
size = sizeMM * mmToInch * resolution
documentRect = QRectF(0.0, 0.0, size.width(), size.height())
fmt = format_.lower()
if fmt in ("pdf", "ps"):
printer = QPrinter()
if fmt == "pdf":
printer.setOutputFormat(QPrinter.PdfFormat)
else:
printer.setOutputFormat(QPrinter.PostScriptFormat)
printer.setColorMode(QPrinter.Color)
printer.setFullPage(True)
printer.setPaperSize(sizeMM, QPrinter.Millimeter)
printer.setDocName(title)
printer.setOutputFileName(filename)
printer.setResolution(resolution)
painter = QPainter(printer)
self.render(plot, painter, documentRect)
painter.end()
elif fmt == "svg":
generator = QSvgGenerator()
generator.setTitle(title)
generator.setFileName(filename)
generator.setResolution(resolution)
generator.setViewBox(documentRect)
painter = QPainter(generator)
self.render(plot, painter, documentRect)
painter.end()
elif fmt in QImageWriter.supportedImageFormats():
imageRect = documentRect.toRect()
dotsPerMeter = int(round(resolution*mmToInch*1000.))
image = QImage(imageRect.size(), QImage.Format_ARGB32)
image.setDotsPerMeterX(dotsPerMeter)
image.setDotsPerMeterY(dotsPerMeter)
image.fill(QColor(Qt.white).rgb())
painter = QPainter(image)
self.render(plot, painter, imageRect)
painter.end()
image.save(filename, fmt)
else:
raise TypeError("Unsupported file format '%s'" % fmt)
示例13: qwtBackgroundWidget
def qwtBackgroundWidget(w):
if w.parentWidget() is None:
return w
if w.autoFillBackground():
brush = w.palette().brush(w.backgroundRole())
if brush.color().alpha() > 0:
return w
if w.testAttribute(Qt.WA_StyledBackground):
image = QImage(1, 1, QImage.Format_ARGB32)
image.fill(Qt.transparent)
painter = QPainter(image)
painter.translate(-w.rect().center())
qwtDrawStyledBackground(w, painter)
painter.end()
if qAlpha(image.pixel(0, 0)) != 0:
return w
return qwtBackgroundWidget(w.parentWidget())
示例14: qwtFillBackground
def qwtFillBackground(*args):
if len(args) == 2:
painter, canvas = args
rects = []
if canvas.testAttribute(Qt.WA_StyledBackground):
recorder = QwtStyleSheetRecorder(canvas.size())
p = QPainter(recorder)
qwtDrawStyledBackground(canvas, p)
p.end()
if recorder.background.brush.isOpaque():
rects = recorder.clipRects
else:
rects += [canvas.rect()]
else:
r = canvas.rect()
radius = canvas.borderRadius()
if radius > 0.:
sz = QSizeF(radius, radius)
rects += [QRectF(r.topLeft(), sz),
QRectF(r.topRight()-QPointF(radius, 0), sz),
QRectF(r.bottomRight()-QPointF(radius, radius), sz),
QRectF(r.bottomLeft()-QPointF(0, radius), sz)]
qwtFillBackground(painter, canvas, rects)
elif len(args) == 3:
painter, widget, fillRects = args
if not fillRects:
return
if painter.hasClipping():
clipRegion = painter.transform().map(painter.clipRegion())
else:
clipRegion = widget.contentsRect()
bgWidget = qwtBackgroundWidget(widget.parentWidget())
for fillRect in fillRects:
rect = fillRect.toAlignedRect()
if clipRegion.intersects(rect):
pm = QPixmap(rect.size())
QwtPainter.fillPixmap(bgWidget, pm, widget.mapTo(bgWidget, rect.topLeft()))
painter.drawPixmap(rect, pm)
else:
raise TypeError("%s() takes 2 or 3 argument(s) (%s given)"\
% ("qwtFillBackground", len(args)))
示例15: legendIcon
def legendIcon(self, index, size):
if size.isEmpty():
return QwtGraphic()
graphic = QwtGraphic()
graphic.setDefaultSize(size)
graphic.setRenderHint(QwtGraphic.RenderPensUnscaled, True)
painter = QPainter(graphic)
painter.setRenderHint(QPainter.Antialiasing,
self.testRenderHint(QwtPlotItem.RenderAntialiased))
if self.__data.legendAttributes == 0 or\
(self.__data.legendAttributes & QwtPlotCurve.LegendShowBrush):
brush = self.__data.brush
if brush.style() == Qt.NoBrush and self.__data.legendAttributes == 0:
if self.style() != QwtPlotCurve.NoCurve:
brush = QBrush(self.pen().color())
elif self.__data.symbol and\
self.__data.symbol.style() != QwtSymbol.NoSymbol:
brush = QBrush(self.__data.symbol.pen().color())
if brush.style() != Qt.NoBrush:
r = QRectF(0, 0, size.width(), size.height())
painter.fillRect(r, brush)
if self.__data.legendAttributes & QwtPlotCurve.LegendShowLine:
if self.pen() != Qt.NoPen:
pn = self.pen()
# pn.setCapStyle(Qt.FlatCap)
painter.setPen(pn)
y = .5*size.height()
QwtPainter.drawLine(painter, 0., y, size.width(), y)
if self.__data.legendAttributes & QwtPlotCurve.LegendShowSymbol:
if self.__data.symbol:
r = QRectF(0, 0, size.width(), size.height())
self.__data.symbol.drawSymbol(painter, r)
return graphic