QCanvasImage Class
QCanvasImage 是QCanvasPainter 的图像类。更多
| 头文件: | #include <QCanvasImage> |
| CMake: | find_package(Qt6 REQUIRED COMPONENTS CanvasPainter)target_link_libraries(mytarget PRIVATE Qt6::CanvasPainter) |
| 自 | Qt 6.11 |
| 状态 | 技术预览版 |
公共函数
| QCanvasImage() | |
| QCanvasImage(const QCanvasImage &image) | |
| QCanvasImage(QCanvasImage &&other) | |
| ~QCanvasImage() | |
| int | height() const |
| int | id() const |
| bool | isNull() const |
| void | setTintColor(const QColor &color) |
| QSize | size() const |
| qsizetype | sizeInBytes() const |
| void | swap(QCanvasImage &other) |
| QColor | tintColor() const |
| int | width() const |
| operator QVariant() const | |
| QCanvasImage & | operator=(QCanvasImage &&other) |
| QCanvasImage & | operator=(const QCanvasImage &image) |
相关非成员
| bool | operator!=(const QCanvasImage &lhs, const QCanvasImage &rhs) |
| bool | operator==(const QCanvasImage &lhs, const QCanvasImage &rhs) |
详细说明
QCanvasImage 是QCanvasPainter 使用的图像类。要想绘制图像,需要使用QCanvasPainter::addImage() 等函数来提供图像。然后,可以使用QCanvasPainter::drawImage() 或QCanvasImagePattern 笔刷填充/描边来绘制图像。
下面是一个简单的例子:
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);

在上述示例中,QImage 是静态的,每次重绘时都会调用 addImage()。这不是问题,因为当图像和标记保持不变时,addImage() 会从缓存中获取图像,而不是将其作为纹理再次上传。但更常见的方法是将 QCanvasImage 变量作为类成员并调用QCanvasPainter::addImage() ,例如在QCanvasPainterItemRenderer::initializeResources() 中。
与QCanvasBrush 和QCanvasOffscreenCanvas 类似,QCanvasImage 也是显式共享的。详见隐式数据共享和QSharedDataPointer 。
注意: QCanvasImage 对象只包含一个图形资源(如纹理)的句柄。即使发生分离,实际资源(即底层纹理和其中的图像数据)也不会被实际复制或复制。真正图形资源(如QRhiTexture )的实际所有者是通过addImage() 发送 QCanvasImage 的QCanvasPainter 。
QCanvasImage 始终属于创建它的QCanvasPainter 。调用removeImage() 可以手动删除图像。不过,在大多数情况下并不需要这样做,因为画图器在销毁自己的图像时会自动销毁任何图像。
成员函数文档
QCanvasImage::QCanvasImage()
构建默认图像。
QCanvasImage::QCanvasImage(const QCanvasImage &image)
构建一个图像,它是给定image 的副本。
[constexpr noexcept] QCanvasImage::QCanvasImage(QCanvasImage &&other)
移动-从other 构建一个新的 QCanvasImage。
[noexcept] QCanvasImage::~QCanvasImage()
破坏图像。
int QCanvasImage::height() const
返回图像的高度。
int QCanvasImage::id() const
返回此图像的纹理 ID。如果图像尚未分配,则返回 0。
另请参阅 isNull()。
bool QCanvasImage::isNull() const
如果图像尚未分配,则返回 true。详情请查看QCanvasPainter::addImage()。
void QCanvasImage::setTintColor(const QColor &color)
设置图像的色调color 。该颜色将与图像颜色相乘,对图像进行着色。例如,可用于突出图标或根据主题调整图像。
另请参阅 tintColor()。
QSize QCanvasImage::size() const
返回图像的大小(宽和高)。
qsizetype QCanvasImage::sizeInBytes() const
以字节为单位返回图像的大小。
QCanvasPainter addImage() 返回后,将不再保留 CPU 端 数据的副本。如果源是 ,则首先就没有 CPU 端图像数据。因此,该函数的结果是用于底层纹理的 GPU 内存的近似值。QImage offscreen canvas
注意: 该值只是基于图像格式和尺寸的估计值。Qt 并不了解纹理数据在 GPU 端内存中的存储和布局。
注: 此函数不考虑 mipmap 或多采样数据。
[noexcept] void QCanvasImage::swap(QCanvasImage &other)
将此图像与other 互换。该操作速度非常快,从未出现过故障。
QColor QCanvasImage::tintColor() const
返回图像的色调。默认值为Qt::white 。
另请参阅 setTintColor()。
int QCanvasImage::width() const
返回图像的宽度。
QCanvasImage::operator QVariant() const
以QVariant 的形式返回图像。
[noexcept] QCanvasImage &QCanvasImage::operator=(QCanvasImage &&other)
Move-assignsother 到此QCanvasImage 实例。
QCanvasImage &QCanvasImage::operator=(const QCanvasImage &image)
将给定的image 分配给此图像,并返回此图像的引用。
相关非会员
[noexcept] bool operator!=(const QCanvasImage &lhs, const QCanvasImage &rhs)
如果图像句柄lhs 与rhs 不同,则返回true ;否则返回false 。
另请参阅 operator==().
[noexcept] bool operator==(const QCanvasImage &lhs, const QCanvasImage &rhs)
如果图像句柄lhs 等于rhs ,则返回true ;否则返回false 。
注: 相等意味着两个图像对象引用了相同的图形资源,且色调颜色相同。不比较内容(像素数据)。
另请参见 operator!=()。
© 2026 The Qt Company Ltd. Documentation contributions included herein are the copyrights of their respective owners. The documentation provided herein is licensed under the terms of the GNU Free Documentation License version 1.3 as published by the Free Software Foundation. Qt and respective logos are trademarks of The Qt Company Ltd. in Finland and/or other countries worldwide. All other trademarks are property of their respective owners.