在本页

QCanvasImagePattern Class

QCanvasImagePattern 是一种用于绘制图像图案的画笔。更多

Header: #include <QCanvasImagePattern>
CMake: find_package(Qt6 REQUIRED COMPONENTS CanvasPainter)
target_link_libraries(mytarget PRIVATE Qt6::CanvasPainter)
Qt 6.11
继承于 QCanvasBrush
状态:技术预览版

公共函数

QCanvasImagePattern()
QCanvasImagePattern(const QCanvasImage &image)
QCanvasImagePattern(const QCanvasImage &image, const QRectF &rect, float angle = 0.0f, const QColor &tintColor = QColorConstants::White)
QCanvasImagePattern(const QCanvasImage &image, float x, float y, float width, float height, float angle = 0.0f, const QColor &tintColor = QColorConstants::White)
~QCanvasImagePattern()
QCanvasImage image() const
QSizeF imageSize() const
float rotation() const
void setImage(const QCanvasImage &image)
void setImageSize(float width, float height)
void setImageSize(QSizeF size)
void setRotation(float rotation)
void setStartPosition(float x, float y)
void setStartPosition(QPointF point)
void setTintColor(const QColor &color)
QPointF startPosition() const
QColor tintColor() const
operator QVariant() const
bool operator!=(const QCanvasImagePattern &lhs, const QCanvasImagePattern &rhs)
QDataStream &operator<<(QDataStream &stream, const QCanvasImagePattern &pattern)
bool operator==(const QCanvasImagePattern &lhs, const QCanvasImagePattern &rhs)
QDataStream &operator>>(QDataStream &stream, QCanvasImagePattern &pattern)

详细说明

QCanvasImagePattern 是一种用于绘制图像图案的画笔。要使用QCanvasImage 作为图案,应打开一些重复标志,如QCanvasPainter::ImageFlag::Repeat

下面是一个简单的示例:

// Setup 2 image patterns
static QImage image1(":/pattern2.png");
static QImage image2(":/pattern3.png");
auto flags = QCanvasPainter::ImageFlag::Repeat |
             QCanvasPainter::ImageFlag::GenerateMipmaps;
QCanvasImage bg1 = painter.addImage(image1, flags);
QCanvasImage bg2 = painter.addImage(image2, flags);
QCanvasImagePattern ip1(bg1, 0, 0, 64, 64);
QCanvasImagePattern ip2(bg2, 0, 0, 32, 32);
// Fill and stroke round rectangle with
// these image pattern brushes.
painter.beginPath();
painter.roundRect(50, 50, 180, 180, 40);
painter.setFillStyle(ip1);
painter.setStrokeStyle(ip2);
painter.fill();
painter.setLineWidth(20);
painter.stroke();

注意: 在使用图像图案时,图像通常会被缩放得更小,因此为所使用的QCanvasImage 设置QCanvasPainter::ImageFlag::GenerateMipmaps 可能会有助于获得平滑的图案。

成员函数文档

QCanvasImagePattern::QCanvasImagePattern()

构造默认图像模式。图像不会被设置,请在此构造函数后使用setImage() 。图案起始位置为(0, 0),图案大小为(100, 100)。图案角度为 0.0,色调为白色(无色调)。

另请参见 setImage()。

QCanvasImagePattern::QCanvasImagePattern(const QCanvasImage &image)

构建图像模式。模式将使用image 。请注意,image 通常至少应设置QCanvasPainter::ImageFlag::Repeat 标志。图案起始位置为(0,0),图案大小为(100,100)。图案角度为 0.0,色调为白色(不着色)。

QCanvasImagePattern::QCanvasImagePattern(const QCanvasImage &image, const QRectF &rect, float angle = 0.0f, const QColor &tintColor = QColorConstants::White)

构建图像模式。模式将使用image 。请注意,image 通常至少应设置QCanvasPainter::ImageFlag::Repeat 标志。图案图像的位置和大小由rect 定义。图案角度为angle ,色调颜色为tintColor

QCanvasImagePattern::QCanvasImagePattern(const QCanvasImage &image, float x, float y, float width, float height, float angle = 0.0f, const QColor &tintColor = QColorConstants::White)

构建图像模式。模式将使用image 。请注意,image 通常至少应设置QCanvasPainter::ImageFlag::Repeat 标志。图案图像的起始位置为 (x,y) ,图案大小为 (width,height) 。图案角度为angle ,色调颜色为tintColor

[noexcept] QCanvasImagePattern::~QCanvasImagePattern()

销毁图像图案。

QCanvasImage QCanvasImagePattern::image() const

返回图案的图像。

另请参见 setImage()。

QSizeF QCanvasImagePattern::imageSize() const

以模式为单位返回单张图像的大小。

另请参阅 setImageSize()。

float QCanvasImagePattern::rotation() const

以弧度为单位返回图案的旋转角度。

另请参阅 setRotation()。

void QCanvasImagePattern::setImage(const QCanvasImage &image)

将图案的图像设置为image 。 注意:在图像图案中使用图像时,通常应设置Repeat 标志。

另请参阅 image() 。

void QCanvasImagePattern::setImageSize(float width, float height)

将图案中单幅图像的大小设置为 (width,height)。

另请参阅 imageSize().

void QCanvasImagePattern::setImageSize(QSizeF size)

设置图案中单幅图像的大小为size

这是一个重载函数。

void QCanvasImagePattern::setRotation(float rotation)

将图案旋转设置为rotation ,单位为弧度。旋转围绕图像startPosition() 进行。默认值为0.0 ,表示不旋转图像。

另请参阅 rotation()。

void QCanvasImagePattern::setStartPosition(float x, float y)

将图像图案的起点设置为 (x,y)。起始位置指图案中图像的左上角。然后,图案将从这里扩展到所有位置(如果已设置图像Repeat 标志)。

另请参阅 startPosition()。

void QCanvasImagePattern::setStartPosition(QPointF point)

将图像图案的起点设置为point 。起点位置指图案中图像的左上角。然后,图案将从此处扩展到所有位置(如果已设置图像Repeat 标志)。

这是一个重载函数。

void QCanvasImagePattern::setTintColor(const QColor &color)

将图案着色设置为color 。图案图像的颜色将与着色器中的着色相乘。默认值为白色,即不着色。

注: 要全局设置 alpha,请使用QCanvasPainter::setGlobalAlpha()

另请参见 tintColor()。

QPointF QCanvasImagePattern::startPosition() const

返回图像图案的起点。

另请参见 setStartPosition()。

QColor QCanvasImagePattern::tintColor() const

返回图案的色调。

另请参见 setTintColor()。

QCanvasImagePattern::operator QVariant() const

QVariant 的形式返回图像模式。

相关非会员

[noexcept] bool operator!=(const QCanvasImagePattern &lhs, const QCanvasImagePattern &rhs)

如果图像模式lhsrhs 不同,则返回true ;否则返回false

另请参阅 operator==().

QDataStream &operator<<(QDataStream &stream, const QCanvasImagePattern &pattern)

将给定的pattern 写入给定的stream ,并返回对stream 的引用。

另请参阅 序列化 Qt 数据类型

[noexcept] bool operator==(const QCanvasImagePattern &lhs, const QCanvasImagePattern &rhs)

如果图像模式lhs 等于rhs ,则返回true ;否则返回false

另请参阅 operator!=().

QDataStream &operator>>(QDataStream &stream, QCanvasImagePattern &pattern)

从给定的stream 中读取给定的pattern ,并返回对stream 的引用。

另请参阅 序列化 Qt 数据类型

© 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.