PySide6.QtCanvasPainter.QCanvasPainterWidget¶
- class QCanvasPainterWidget¶
QCanvasPainterWidgetis a widget for rendering usingQCanvasPainter.Details
Warning
This section contains snippets that were automatically translated from C++ to Python and may contain errors.
Implement the
paintvirtual function in a subclass to perform rendering using aQCanvasPainter.The below code snippet shows the typical structure of a
QCanvasPainterWidgetsubclass: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()
Synopsis¶
Methods¶
def
__init__()def
fillColor()def
grabCanvas()def
setFillColor()
Virtual methods¶
def
paint()def
prePaint()
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
Constructs a
QCanvasPainterWidgetwith the givenparent.- beginCanvasPainting(canvas)¶
- Parameters:
canvas –
QCanvasOffscreenCanvas
Warning
This section contains snippets that were automatically translated from C++ to Python and may contain errors.
Starts recording
QCanvasPainterdraw commands targetingcanvas.Note
This function should only be called from
prePaint().beginCanvasPainting() must always be followed by corresponding
endCanvasPainting()before returning fromprePaint().The following snippet from a
QCanvasPainterWidgetsubclass 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 fromprePaint().Returns the current fill color.
See also
- grabCanvas(canvas, resultCallback)¶
- Parameters:
canvas –
QCanvasOffscreenCanvasresultCallback –
PyCallable
Issues a texture readback request for
canvas.callbackis invoked either before the function returns, or later, depending on the underlyingQRhiand 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
QCanvasImageobjects returned from addImage() are no longer valid, and addImage() needs to be called again. If thepaint()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 ofpaint().The same applies to
QCanvasOffscreenCanvasobjects returned fromcreateCanvas(). When this function is called, the next invocation ofpaint()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.
See also
- Return type:
bool
Returns
trueif this widget uses a shared painter.See also
- initializeResources(painter)¶
- Parameters:
painter –
QCanvasPainter
Reimplement this method to initialize resources using
painter. Generally, this will be called once before the firstprePaint()andpaint(). An exception is when graphics resources are lost, seegraphicsResourcesInvalidated(). In that case, this method will get invoked again afterwards.The default implementation is empty.
- paint(painter)¶
- Parameters:
painter –
QCanvasPainter
Reimplement this method to paint using
painter.The widget is first filled with
fillColor().The default implementation is empty.
- prePaint(painter)¶
- Parameters:
painter –
QCanvasPainter
Reimplement this function to perform drawing into one or more offscreen canvases using
painter.The default implementation is empty.
See also
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
- Parameters:
enable – bool
Disable painter sharing if
enableisfalse.If painter sharing is enabled, all
QCanvasPainterWidgetinstances inside the same QWindow will use the sameQCanvasPainter. 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
QCanvasImageor 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 sameQCanvasPainter.Note
Even when
enableis true, painters are not shared between widgets belonging to different windows (top-level widgets).See also