PySide6.QtCanvasPainter.QCanvasPainterWidget

class QCanvasPainterWidget

QCanvasPainterWidget is a widget for rendering using QCanvasPainter .

Details

Warning

This section contains snippets that were automatically translated from C++ to Python and may contain errors.

Implement the paint virtual function in a subclass to perform rendering using a QCanvasPainter .

The below code snippet shows the typical structure of a QCanvasPainterWidget subclass:

class MyWidget(QCanvasPainterWidget):

# public
    def initializeResources(p):

        # load assets
        if m_image.isNull():
            m_image = p.addImage(QImage("image.png"), QCanvasPainter.ImageFlag.Repeat)

    def paint(p):

        # ... draw using m_image

    def graphicsResourcesInvalidated():

        # textures are lost, indicate the need for reload
        m_image = {}

    m_image = QCanvasImage()

Inheritance diagram of PySide6.QtCanvasPainter.QCanvasPainterWidget

Synopsis

Methods

Virtual methods

Note

This documentation may contain snippets that were automatically translated from C++ to Python. We always welcome contributions to the snippet translation. If you see an issue with the translation, you can also let us know by creating a ticket on https:/bugreports.qt.io/projects/PYSIDE

__init__([parent=None])
Parameters:

parentQWidget

Constructs a QCanvasPainterWidget with the given parent.

beginCanvasPainting(canvas)
Parameters:

canvasQCanvasOffscreenCanvas

Warning

This section contains snippets that were automatically translated from C++ to Python and may contain errors.

Starts recording QCanvasPainter draw commands targeting canvas.

Note

This function should only be called from prePaint() .

beginCanvasPainting() must always be followed by corresponding endCanvasPainting() before returning from prePaint() .

The following snippet from a QCanvasPainterWidget subclass shows how an offscreen canvas could be rendered into and then used as an image or image pattern when drawing the contents for the widget:

class MyWidget(QCanvasPainterWidget):

# public
    canvas = QCanvasOffscreenCanvas()
    canvasImage = QCanvasImage()
    def graphicsResourcesInvalidated():

        canvas = {} # so that the next prePaint() will recreate and redraw the canvas

    def prePaint(p):

        if canvas.isNull():
            canvas = p.createCanvas(QSize(320, 240))
            beginCanvasPainting(canvas)
            p.beginPath()
            p.circle(160, 120, 20)
            p.setFillStyle(Qt.GlobalColor.red)
            p.fill()
            endCanvasPainting()
            canvasImage = p.addImage(canvas, QCanvasPainter.ImageFlag.Repeat)


    def paint(p):

        # use canvasImage as a brush or with drawImage()
endCanvasPainting()

Indicates the end of the drawing targeting the canvas specified in beginCanvasPainting() .

Note

This function should only be called from prePaint() .

beginCanvasPainting() must always be followed by corresponding endCanvasPainting() before returning from prePaint() .

fillColor()
Return type:

QColor

Returns the current fill color.

See also

setFillColor()

grabCanvas(canvas, resultCallback)
Parameters:

Issues a texture readback request for canvas. callback is invoked either before the function returns, or later, depending on the underlying QRhi and 3D API implementation. Reading back texture contents may involve a GPU->CPU copy, depending on the GPU architecture.

graphicsResourcesInvalidated()

Called when underlying graphics resources, such as textures, are lost.

This indicates that QCanvasImage objects returned from addImage() are no longer valid, and addImage() needs to be called again. If the paint() implementation is such that this does not matter, for example because images are not used, or addImage() is always called, then no action is necessary. Otherwise, it is recommended to toggle a flag, or similar, and act accordingly in the next invocation of paint() .

The same applies to QCanvasOffscreenCanvas objects returned from createCanvas() . When this function is called, the next invocation of paint() should create new canvases and redraw their contents.

Graphics resources can be lost, for example, when the widget is moved to a new top-level window, because that implies being associated with a new QRhi instance.

hasSharedPainter()
Return type:

bool

Returns true if this widget uses a shared painter.

See also

setSharedPainter

initializeResources(painter)
Parameters:

painterQCanvasPainter

Reimplement this method to initialize resources using painter. Generally, this will be called once before the first prePaint() and paint() . An exception is when graphics resources are lost, see graphicsResourcesInvalidated() . In that case, this method will get invoked again afterwards.

The default implementation is empty.

paint(painter)
Parameters:

painterQCanvasPainter

Reimplement this method to paint using painter.

The widget is first filled with fillColor() .

The default implementation is empty.

prePaint(painter)
Parameters:

painterQCanvasPainter

Reimplement this function to perform drawing into one or more offscreen canvases using painter.

The default implementation is empty.

setFillColor(color)
Parameters:

colorQColor

Set the fill color to color. This color will be used to draw the background of the item. The default color is black.

See also

fillColor()

setSharedPainter(enable)
Parameters:

enable – bool

Disable painter sharing if enable is false.

If painter sharing is enabled, all QCanvasPainterWidget instances inside the same QWindow will use the same QCanvasPainter . This function must be early, e.g. from the derived class’ constructor, and must not be changed afterwards.

Painter sharing is enabled by default.

If two widgets use dedicated, non-shared painters, each other’s graphics resources, such as the ones backing QCanvasImage or QOffscreenCanvas, will not be visible to them. Whereas if the widgets are in the same window, and sharing is enabled, they can use images or canvases created by the other widget, because they both use the same QCanvasPainter .

Note

Even when enable is true, painters are not shared between widgets belonging to different windows (top-level widgets).

See also

hasSharedPainter