PySide6.QtCanvasPainter.QCanvasImage

class QCanvasImage

QCanvasImage is the image class for QCanvasPainter .

Details

QCanvasImage is the image class used by QCanvasPainter . To be able to paint images, they fist need to be made available with e.g. addImage() . Then images can be painted as-is using 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);
../../_images/image_example_1.png

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 addImage() e.g. in 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.

Synopsis

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__()

Constructs a default image.

__init__(image)
Parameters:

imageQCanvasImage

Constructs an image that is a copy of the given image.

height()
Return type:

int

Returns the height of this image.

id()
Return type:

int

Returns the texture id for this image. When the image has not been assigned yet, this returns 0.

See also

isNull()

isNull()
Return type:

bool

Returns true if the image has not been assigned yet. See addImage() for details.

__ne__(rhs)
Parameters:

rhsQCanvasImage

Return type:

bool

__eq__(rhs)
Parameters:

rhsQCanvasImage

Return type:

bool

setTintColor(color)
Parameters:

colorQColor

Set the tint color of the image. This color will be multiplied with the image color to colorize the image. This can be used for example to highlight icons or to adjust images becase on the theme.

See also

tintColor()

size()
Return type:

QSize

Returns the size (width and height) of this image.

sizeInBytes()
Return type:

int

Returns the size of this image in bytes.

QCanvasPainter does not keep copies of the CPU-side QImage data once addImage() has returned. If the source is a offscreen canvas , then there is no CPU-side image data in the first place. Therefore, the result of this function is an approximation of the GPU memory that is used for the underlying texture.

Note

The value is only an estimate based on the image format and dimensions. Qt has no knowledge of how the data for textures is stored and laid out in memory on the GPU side.

Note

This function does not take mipmap or multisample data into consideration.

swap(other)
Parameters:

otherQCanvasImage

Swaps this image with other. This operation is very fast and never fails.

tintColor()
Return type:

QColor

Returns the tint color of the image. The default value is Qt::white.

See also

setTintColor()

width()
Return type:

int

Returns the width of this image.