PySide6.QtCanvasPainter.QCanvasPainterItemRenderer¶
- class QCanvasPainterItemRenderer¶
The
QCanvasPainterItemRendererhandles all painting for aQCanvasPainterItem.Details
Warning
This section contains snippets that were automatically translated from C++ to Python and may contain errors.
Implement the
paint()method to perform the rendering.To expose data from the item to the renderer in a thread-safe manner, implement
synchronize().The renderer object lives and operates on the Qt Quick scene graph render thread, if there is one, since that is the thread on which
createItemRenderer()and all the functions in this class are invoked.If the
QCanvasPainterItemis moved to a different window, it will get associated with a new QRhi. Therefore, the renderer object is automatically destroyed, and a new one is created by invokingcreateItemRenderer()again. This allows simple management ofQCanvasImageandQCanvasOffscreenCanvasobjects, because they can be member variables in renderer, set up either ininitializeResources()or inpaint(), without having to reset them when the graphics resources are lost due to the window and QRhi change, since it all happens implicitly by the destruction of the whole renderer object.The below code snippet shows the typical structure of a
QCanvasPainterItemRenderersubclass. SeeQCanvasPainterItemfor an example of theMyItemclass.class MyRenderer(QCanvasPainterItemRenderer): # public def synchronize(item): # copy a custom property value from item in a thread-safe manner m_value = MyItem(item).value() def initializeResources(p): # load assets if m_image.isNull(): m_image = p.addImage(QImage("image.png"), QCanvasPainter.ImageFlag.Repeat) def prePaint(p): # this is where offscreen canvases are drawn into if m_canvas.isNull(): m_canvas = p.createCanvas(QSize(640, 480)) beginCanvasPainting(m_canvas) # ... draw into the offscreen canvas endCanvasPainting(m_canvas) m_canvasImage = p.addImage(m_canvas) def paint(p): center = QPointF(width() / 2, height() / 2) # ... draw using m_value, m_image, and m_canvasImage m_image = QCanvasImage() m_canvas = QCanvasOffscreenCanvas() m_canvasImage = QCanvasImage() m_value = float()
See also
Synopsis¶
Methods¶
def
__init__()def
fillColor()def
height()def
painter()def
width()
Virtual methods¶
def
paint()def
prePaint()def
synchronize()
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__()¶
Constructs a
QCanvasPainterItemRenderer.- beginCanvasPainting(canvas)¶
- Parameters:
canvas –
QCanvasOffscreenCanvas
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().- 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().Return the current fill color of the item. This can be set by the parent
QCanvasPainterItem.- Return type:
bool
Returns
trueif this item renderer uses a shared painter.See also
- height()¶
- Return type:
float
Returns the height of the painted area, in logical units, without the scale factor (device pixel ratio). This is usually the same as the painter item’s height, unless QQuickRhiItem::fixedColorBufferHeight has been set.
- initializeResources(painter)¶
- Parameters:
painter –
QCanvasPainter
Reimplement this method to initialize resources using
painter. This will be called once before the firstsynchronize().- paint(painter)¶
- Parameters:
painter –
QCanvasPainter
Reimplement this method to paint using
painter.This will get called after the item has been filled with
fillColor().paint() is called from renderer thread. To access item data safely, copy it in
synchronize().See also
- painter()¶
- Return type:
Returns the painter attached to this painter item.
- prePaint(painter)¶
- Parameters:
painter –
QCanvasPainter
This function is called at the start of rendering using
painter, beforepaint().There is no render target active when this function is invoked. Call
beginCanvasPainting()to initialize drawing into an offscreen canvas.See also
- Parameters:
enable – bool
Disable painter sharing if
enableisfalse.Painter sharing is enabled by default.
When painter sharing is enabled, all painter items inside the same QQuickWindow will use the same
QCanvasPainter.If disabling painter sharing is desired, this function must be called early enough, for example from the derived class’ constructor. Changing it afterwards, when the item has already initialized for painting, will have no effect.
If two items 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 items are in the same window, and sharing is enabled, they can use images or canvases created by the other item, because they both use the sameQCanvasPainter.Note
Even when
enableis true, painters are not shared between items belonging to different QQuickWindow instances, and by extension, to different scene graphs.See also
- synchronize(item)¶
- Parameters:
item –
QCanvasPainterItem
Reimplement this method to synchronize data between
itemand item painter instances. This will be called beforepaint()each time item needs to be repainted.This method is the only place where it is safe for the painter and the item to read and write each others variables.
Usually you should static_cast
itemto your real item type, and then exchange the data.Note
Make sure to reimplement this overload, taking a
QCanvasPainterItem, instead of the base class’ version that takes a QQuickRhiItem.- width()¶
- Return type:
float
Returns the width of the painted area, in logical units, without the scale factor (device pixel ratio). This is usually the same as the painter item’s width, unless QQuickRhiItem::fixedColorBufferWidth has been set.