QCanvasImage Class

QCanvasImage is the image class for QCanvasPainter. More...

Header: #include <QCanvasImage>
CMake: find_package(Qt6 REQUIRED COMPONENTS CanvasPainter)
target_link_libraries(mytarget PRIVATE Qt6::CanvasPainter)
Since: Qt 6.11
Status: Technical Preview

Public Functions

QCanvasImage &operator=(QCanvasImage &&other)

Detailed Description

QCanvasImage is the image class used by QCanvasPainter. To be able to paint images, they fist need to be made available with e.g. QCanvasPainter::addImage(). Then images can be painted as-is using QCanvasPainter::drawImage() or used with QCanvasImagePattern brush to fill / stroke.

Here is a simple example:

 static QImage logoImage(":/qtlogo.png");
 // Paint an image pattern.
 QCanvasImage bg = painter.addImage(logoImage,
              QCanvasPainter::ImageFlag::Repeat |
              QCanvasPainter::ImageFlag::GenerateMipmaps);
 QCanvasImagePattern ip(bg, 0, 0, 44, 32);
 painter.setFillStyle(ip);
 painter.fillRect(50, 50, 320, 230);
 // Paint a single image, with tint color.
 QCanvasImage logo = painter.addImage(logoImage);
 logo.setTintColor("#2cde85");
 painter.drawImage(logo, 100, 80);

In the above example the QImage is static and addImage() is called on every repaint. This is not a problem as when the image and flags remain the same, addImage() fetches the image from cache instead of uploads it again as a texture. But a more common approac is having QCanvasImage variables as class members and calling QCanvasPainter::addImage() e.g. in QCanvasPainterItemRenderer::initializeResources().

Similarly to QCanvasBrush and QCanvasOffscreenCanvas, QCanvasImage is explicitly shared. See Implicit Data Sharing and QSharedDataPointer for details.

Note: A QCanvasImage object contains only a handle to a graphics resource, such as a texture. Even when a detach occurs, the actual resource, i.e. the underlying texture and the image data in it, is never actually copied or duplicated. The actual owner of the real graphics resource (e.g., a QRhiTexture) is the QCanvasPainter that handed out the QCanvasImage via addImage().

A QCanvasImage always belongs to the QCanvasPainter that created it. Manually removing images is done by calling removeImage(). In most cases this will not be necessary, however, since the painter will automatically destroy any images during its own destruction.

Member Function Documentation

[default] QCanvasImage &QCanvasImage::operator=(QCanvasImage &&other)

Move-assigns other to this QCanvasImage instance.