QCanvasPainterWidget Class
QCanvasPainterWidget is a widget for rendering using QCanvasPainter. More...
| Header: | #include <QCanvasPainterWidget> |
| CMake: | find_package(Qt6 REQUIRED COMPONENTS CanvasPainter)target_link_libraries(mytarget PRIVATE Qt6::CanvasPainter) |
| Since: | Qt 6.11 |
| Status: | Technical Preview |
Protected Functions
| void | beginCanvasPainting(QCanvasOffscreenCanvas &canvas) |
| void | endCanvasPainting() |
| virtual void | graphicsResourcesInvalidated() |
| void | initialize(QRhiCommandBuffer *cb) |
| virtual void | initializeResources(QCanvasPainter *painter) |
| virtual void | paint(QCanvasPainter *painter) |
| virtual void | prePaint(QCanvasPainter *painter) |
| void | releaseResources() |
| void | render(QRhiCommandBuffer *cb) |
Detailed Description
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 : public QCanvasPainterWidget { public: void initializeResources(QCanvasPainter *p) override { // load assets if (m_image.isNull()) m_image = p->addImage(QImage("image.png"), QCanvasPainter::ImageFlag::Repeat); } void paint(QCanvasPainter *p) override { // ... draw using m_image } void graphicsResourcesInvalidated() override { // textures are lost, indicate the need for reload m_image = {}; } QCanvasImage m_image; };
Member Function Documentation
[protected] void QCanvasPainterWidget::beginCanvasPainting(QCanvasOffscreenCanvas &canvas)
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 : public QCanvasPainterWidget { public: QCanvasOffscreenCanvas canvas; QCanvasImage canvasImage; void graphicsResourcesInvalidated() override { canvas = {}; // so that the next prePaint() will recreate and redraw the canvas } void prePaint(QCanvasPainter *p) override { if (canvas.isNull()) { canvas = p->createCanvas(QSize(320, 240)); beginCanvasPainting(canvas); p->beginPath(); p->circle(160, 120, 20); p->setFillStyle(Qt::red); p->fill(); endCanvasPainting(); canvasImage = p->addImage(canvas, QCanvasPainter::ImageFlag::Repeat); } } void paint(QCanvasPainter *p) override { // use canvasImage as a brush or with drawImage() } };
[protected] void QCanvasPainterWidget::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().
[virtual protected] void QCanvasPainterWidget::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 QCanvasPainter::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.
See also QRhiWidget::releaseResources().
[protected] void QCanvasPainterWidget::initialize(QRhiCommandBuffer *cb)
[virtual protected] void QCanvasPainterWidget::initializeResources(QCanvasPainter *painter)
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.
[virtual protected] void QCanvasPainterWidget::paint(QCanvasPainter *painter)
Reimplement this method to paint using painter.
The widget is first filled with fillColor().
The default implementation is empty.
[virtual protected] void QCanvasPainterWidget::prePaint(QCanvasPainter *painter)
Reimplement this function to perform drawing into one or more offscreen canvases using painter.
The default implementation is empty.
See also beginCanvasPainting() and endCanvasPainting().