QwtGraphic

class qwt.graphic.QwtGraphic(*args)[source]

A paint device for scalable graphics

QwtGraphic is the representation of a graphic that is tailored for scalability. Like QPicture it will be initialized by QPainter operations and can be replayed later to any target paint device.

While the usual image representations QImage and QPixmap are not scalable Qt offers two paint devices, that might be candidates for representing a vector graphic:

  • QPicture:

    Unfortunately QPicture had been forgotten, when Qt4 introduced floating point based render engines. Its API is still on integers, what make it unusable for proper scaling.

  • QSvgRenderer, QSvgGenerator:

    Unfortunately QSvgRenderer hides to much information about its nodes in internal APIs, that are necessary for proper layout calculations. Also it is derived from QObject and can’t be copied like QImage/QPixmap.

QwtGraphic maps all scalable drawing primitives to a QPainterPath and stores them together with the painter state changes ( pen, brush, transformation … ) in a list of QwtPaintCommands. For being a complete QPaintDevice it also stores pixmaps or images, what is somehow against the idea of the class, because these objects can’t be scaled without a loss in quality.

The main issue about scaling a QwtGraphic object are the pens used for drawing the outlines of the painter paths. While non cosmetic pens ( QPen.isCosmetic() ) are scaled with the same ratio as the path, cosmetic pens have a fixed width. A graphic might have paths with different pens - cosmetic and non-cosmetic.

QwtGraphic caches 2 different rectangles:

  • control point rectangle:

    The control point rectangle is the bounding rectangle of all control point rectangles of the painter paths, or the target rectangle of the pixmaps/images.

  • bounding rectangle:

    The bounding rectangle extends the control point rectangle by what is needed for rendering the outline with an unscaled pen.

Because the offset for drawing the outline depends on the shape of the painter path ( the peak of a triangle is different than the flat side ) scaling with a fixed aspect ratio always needs to be calculated from the control point rectangle.

class QwtGraphic

Initializes a null graphic

class QwtGraphic(other)

Copy constructor

Parameters:

other (qwt.graphic.QwtGraphic) – Source

reset()[source]

Clear all stored commands

isNull()[source]

Return True, when no painter commands have been stored

isEmpty()[source]

Return True, when the bounding rectangle is empty

setRenderHint(hint, on=True)[source]

Toggle an render hint

testRenderHint(hint)[source]

Test a render hint

boundingRect()[source]

The bounding rectangle is the controlPointRect() extended by the areas needed for rendering the outlines with unscaled pens.

Returns:

Bounding rectangle of the graphic

controlPointRect()[source]

The control point rectangle is the bounding rectangle of all control points of the paths and the target rectangles of the images/pixmaps.

Returns:

Control point rectangle

scaledBoundingRect(sx, sy)[source]

Calculate the target rectangle for scaling the graphic

Parameters:
  • sx (float) – Horizontal scaling factor

  • sy (float) – Vertical scaling factor

Returns:

Scaled bounding rectangle

Note

In case of paths that are painted with a cosmetic pen (see QPen.isCosmetic()) the target rectangle is different to multiplying the bounding rectangle.

sizeMetrics()[source]

Return Ceiled defaultSize()

setDefaultSize(size)[source]

The default size is used in all methods rendering the graphic, where no size is explicitly specified. Assigning an empty size means, that the default size will be calculated from the bounding rectangle.

Parameters:

size (QSizeF) – Default size

defaultSize()[source]

When a non empty size has been assigned by setDefaultSize() this size will be returned. Otherwise the default size is the size of the bounding rectangle.

The default size is used in all methods rendering the graphic, where no size is explicitly specified.

Returns:

Default size

render(*args)[source]
render(painter)[source]

Replay all recorded painter commands

Parameters:

painter (QPainter) – Qt painter

render(painter, size, aspectRatioMode)[source]

Replay all recorded painter commands

The graphic is scaled to fit into the rectangle of the given size starting at ( 0, 0 ).

Parameters:
  • painter (QPainter) – Qt painter

  • size (QSizeF) – Size for the scaled graphic

  • aspectRatioMode (Qt.AspectRatioMode) – Mode how to scale

render(painter, rect, aspectRatioMode)[source]

Replay all recorded painter commands

The graphic is scaled to fit into the given rectangle

Parameters:
  • painter (QPainter) – Qt painter

  • rect (QRectF) – Rectangle for the scaled graphic

  • aspectRatioMode (Qt.AspectRatioMode) – Mode how to scale

render(painter, pos, aspectRatioMode)[source]

Replay all recorded painter commands

The graphic is scaled to the defaultSize() and aligned to a position.

Parameters:
  • painter (QPainter) – Qt painter

  • pos (QPointF) – Reference point, where to render

  • aspectRatioMode (Qt.AspectRatioMode) – Mode how to scale

toPixmap(*args)[source]

Convert the graphic to a QPixmap

All pixels of the pixmap get initialized by Qt.transparent before the graphic is scaled and rendered on it.

The size of the pixmap is the default size ( ceiled to integers ) of the graphic.

Returns:

The graphic as pixmap in default size

toImage(*args)[source]
toImage()[source]

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.

Returns:

The graphic as image in default size

toImage(size[, aspectRatioMode=Qt.IgnoreAspectRatio])[source]

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.

Parameters:
  • size (QSize) – Size of the image

  • aspectRatioMode (Qt.AspectRatioMode) – Aspect ratio how to scale the graphic

Returns:

The graphic as image

See also

toPixmap(), render()

drawPath(path)[source]

Store a path command in the command list

Parameters:

path (QPainterPath) – Painter path

See also

QPaintEngine.drawPath()

drawPixmap(rect, pixmap, subRect)[source]

Store a pixmap command in the command list

Parameters:
  • rect (QRectF) – target rectangle

  • pixmap (QPixmap) – Pixmap to be painted

  • subRect (QRectF) – Reactangle of the pixmap to be painted

See also

QPaintEngine.drawPixmap()

drawImage(rect, image, subRect, flags)[source]

Store a image command in the command list

Parameters:
  • rect (QRectF) – target rectangle

  • image (QImage) – Pixmap to be painted

  • subRect (QRectF) – Reactangle of the pixmap to be painted

  • flags (Qt.ImageConversionFlags) – Pixmap to be painted

See also

QPaintEngine.drawImage()

updateState(state)[source]

Store a state command in the command list

Parameters:

state (QPaintEngineState) – State to be stored

See also

QPaintEngine.updateState()