本页内容

QBrush Class

QBrush 类定义了由QPainter 绘制的图形的填充图案。更多内容...

头文件: #include <QBrush>
CMake: find_package(Qt6 REQUIRED COMPONENTS Gui)
target_link_libraries(mytarget PRIVATE Qt6::Gui)
qmake: QT += gui

公共函数

QBrush()
QBrush(Qt::BrushStyle style)
QBrush(const QGradient &gradient)
QBrush(const QImage &image)
QBrush(const QPixmap &pixmap)
QBrush(Qt::GlobalColor color, Qt::BrushStyle style = Qt::SolidPattern)
QBrush(Qt::GlobalColor color, const QPixmap &pixmap)
QBrush(const QColor &color, Qt::BrushStyle style = Qt::SolidPattern)
QBrush(const QColor &color, const QPixmap &pixmap)
QBrush(const QBrush &other)
~QBrush()
const QColor &color() const
const QGradient *gradient() const
bool isOpaque() const
void setColor(const QColor &color)
void setColor(Qt::GlobalColor color)
void setStyle(Qt::BrushStyle style)
void setTexture(const QPixmap &pixmap)
void setTextureImage(const QImage &image)
void setTransform(const QTransform &matrix)
Qt::BrushStyle style() const
void swap(QBrush &other)
QPixmap texture() const
QImage textureImage() const
QTransform transform() const
operator QVariant() const
bool operator!=(const QBrush &brush) const
QBrush &operator=(QBrush &&other)
QBrush &operator=(const QBrush &brush)
(since 6.9) QBrush &operator=(QColor color)
(since 6.9) QBrush &operator=(Qt::BrushStyle style)
(since 6.9) QBrush &operator=(Qt::GlobalColor color)
bool operator==(const QBrush &brush) const
QDataStream &operator<<(QDataStream &stream, const QBrush &brush)
QDataStream &operator>>(QDataStream &stream, QBrush &brush)

详细说明

画笔具有样式、颜色、渐变和纹理。

画笔style() 使用Qt::BrushStyle 枚举来定义填充图案。默认的画笔样式为Qt::NoBrush (取决于您如何构建画笔)。此样式指示绘图程序不要填充形状。 填充的标准样式为Qt::SolidPattern 。该样式可在创建画笔时通过相应的构造函数进行设置,此外,setStyle() 函数还提供了在画笔构建完成后更改样式的方法。

笔刷样式

画笔的color() 方法定义了填充图案的颜色。该颜色可以是 Qt 的预定义颜色之一(Qt::GlobalColor ),也可以是任何其他自定义颜色(QColor )。可以通过color() 和setColor() 函数分别获取和修改当前设置的颜色。

gradient() 函数定义了在当前样式为Qt::LinearGradientPattern 、Qt::RadialGradientPattern 或Qt::ConicalGradientPattern 时使用的渐变填充。创建渐变画笔时,需在 QBrush 的构造函数中将QGradient 作为参数传入。Qt 提供了三种不同的渐变:QLinearGradient 、QConicalGradient 和QRadialGradient ——它们均继承自QGradient 。

    QRadialGradient gradient(50, 50, 50, 50, 50);
    gradient.setColorAt(0, QColor::fromRgbF(0, 1, 0, 1));
    gradient.setColorAt(1, QColor::fromRgbF(0, 0, 0, 0));

    QBrush brush(gradient);

texture() 定义了当当前样式为Qt::TexturePattern 时所使用的位图。您可以通过在创建画笔时提供位图,或者使用setTexture() 来创建带有纹理的画笔。

请注意,调用setTexture()会使style()等于Qt::TexturePattern ,无论之前的样式设置如何。此外,如果样式为渐变,调用setColor()将不会产生影响。如果样式为Qt::TexturePattern ,情况也是如此,除非当前纹理为QBitmap 。

isOpaque() 函数在画笔完全不透明时返回true ,否则返回 false。当满足以下条件时,画笔被视为不透明:

  • color() 的 alpha 分量为 255。
  • 其texture() 没有 alpha 通道,且不是QBitmap 。
  • gradient() 中的所有颜色其 alpha 分量均为 255。
两个带有轮廓且相互重叠的矩形要指定线条和轮廓的样式与颜色,请将QPainter 的pen 与Qt::PenStyle 和Qt::GlobalColor 结合使用:
QPainter painter(this);

painter.setBrush(Qt::cyan);
painter.setPen(Qt::darkCyan);
painter.drawRect(0, 0, 100,100);

painter.setBrush(Qt::NoBrush);
painter.setPen(Qt::darkGreen);
painter.drawRect(40, 40, 100, 100);

请注意,默认情况下,QPainter 在绘制形状时会(使用当前设置的画笔)渲染轮廓。使用 painter.setPen(Qt::NoPen) 可禁用此行为。

有关绘图的一般信息,请参阅“绘图系统”。

另请参阅 Qt::BrushStyle 、QPainter 和QColor 。

成员函数文档

QBrush::QBrush()

创建一个默认的黑色画笔,其样式为Qt::NoBrush (即该画笔不会填充形状)。

QBrush::QBrush(Qt::BrushStyle style)

根据给定的style 构建一个黑色画笔。

另请参阅 setStyle()。

QBrush::QBrush(const QGradient &gradient)

根据给定的gradient 创建一个画笔。

画笔样式将设置为相应的渐变样式(Qt::LinearGradientPattern 、Qt::RadialGradientPattern 或Qt::ConicalGradientPattern )。

QBrush::QBrush(const QImage &image)

创建一个颜色为黑色、纹理设置为给定image 的画笔。样式设置为Qt::TexturePattern 。

另请参阅 setTextureImage()。

QBrush::QBrush(const QPixmap &pixmap)

创建一个颜色为黑色、纹理设置为给定pixmap 的画笔。样式设置为Qt::TexturePattern 。

另请参阅 setTexture()。

QBrush::QBrush(Qt::GlobalColor color, Qt::BrushStyle style = Qt::SolidPattern)

根据给定的color 和style 创建一个画笔。

另请参阅 setColor() 和setStyle()。

QBrush::QBrush(Qt::GlobalColor color, const QPixmap &pixmap)

根据给定的color 和存储在pixmap 中的自定义图案创建一个画笔。

样式设置为Qt::TexturePattern 。颜色仅对QBitmaps有效。

另请参阅 setColor() 和setTexture()。

QBrush::QBrush(const QColor &color, Qt::BrushStyle style = Qt::SolidPattern)

根据给定的color 和style 创建一个画笔。

另请参阅 setColor() 和setStyle()。

QBrush::QBrush(const QColor &color, const QPixmap &pixmap)

根据给定的color 和存储在pixmap 中的自定义图案创建一个画笔。

样式设置为Qt::TexturePattern 。颜色仅对QBitmaps有效。

另请参阅 setColor() 和setTexture()。

QBrush::QBrush(const QBrush &other)

创建other 的副本。

[noexcept] QBrush::~QBrush()

摧毁了画笔。

const QColor &QBrush::color() const

返回画笔颜色。

另请参阅 ` setColor()`。

const QGradient *QBrush::gradient() const

返回描述该画笔的渐变。

bool QBrush::isOpaque() const

如果画笔完全不透明,则返回true ;否则返回false。满足以下条件时,画笔被视为不透明:

  • color() 的 alpha 分量为 255。
  • 其 `texture()` 没有 alpha 通道,且不是 `QBitmap`。
  • gradient() 中的所有颜色其 alpha 分量均为 255。
  • 这是一个扩展的径向渐变。

void QBrush::setColor(const QColor &color)

将画笔颜色设置为给定的color 。

请注意,如果样式为渐变,调用 setColor() 不会产生任何效果。如果样式为Qt::TexturePattern 样式,情况也是如此,除非当前纹理为QBitmap 。

另请参阅 color()。

void QBrush::setColor(Qt::GlobalColor color)

将画笔颜色设置为指定的color 。

这是一个重载函数。

void QBrush::setStyle(Qt::BrushStyle style)

将画笔样式设置为“style ”。

另请参阅 style()。

void QBrush::setTexture(const QPixmap &pixmap)

将画笔位图设置为pixmap 。样式设置为Qt::TexturePattern 。

当前画笔颜色仅对单色位图有效,即当QPixmap::depth() == 1(QBitmaps )时。

另请参阅 texture()。

void QBrush::setTextureImage(const QImage &image)

将画笔图像设置为image 。样式设置为Qt::TexturePattern 。

请注意,当前画笔颜色对单色图像没有任何影响,这与使用QBitmap 调用setTexture()的情况不同。若要更改单色图像画笔的颜色,请使用QBitmap::fromImage() 将图像转换为QBitmap ,并将生成的QBitmap 设置为纹理;或者修改该图像的颜色表条目。

另请参阅 textureImage() 和setTexture()。

void QBrush::setTransform(const QTransform &matrix)

将matrix 设置为当前画笔的显式变换矩阵。画笔变换矩阵将与QPainter 的变换矩阵合并,以生成最终结果。

另请参阅 transform()。

Qt::BrushStyle QBrush::style() const

返回画笔样式。

另请参阅 setStyle()。

[noexcept] void QBrush::swap(QBrush &other)

将此画笔替换为other 。此操作非常快速,且从未失败。

QPixmap QBrush::texture() const

返回自定义画笔图案;如果未设置自定义画笔图案,则返回空位图。

另请参阅 setTexture()。

QImage QBrush::textureImage() const

返回自定义画笔图案;如果未设置自定义画笔图案,则返回空图像。

如果纹理被设置为QPixmap ,则将其转换为QImage 。

另请参阅 setTextureImage()。

QTransform QBrush::transform() const

返回画笔的当前变换矩阵。

另请参阅 setTransform()。

QBrush::operator QVariant() const

将画笔作为QVariant

bool QBrush::operator!=(const QBrush &brush) const

如果画笔与给定的brush 不同,则返回true ;否则返回false 。

如果两个画笔的样式、颜色或变换不同,或者根据样式不同而具有不同的位图或渐变,则视为不同。

另请参阅 operator==()。

[noexcept] QBrush &QBrush::operator=(QBrush &&other)

将other 通过Move操作赋值给此QBrush 实例。

QBrush &QBrush::operator=(const QBrush &brush)

将给定的brush 分配给此画笔,并返回对此画笔的引用。

[since 6.9] QBrush &QBrush::operator=(QColor color)

[since 6.9] QBrush &QBrush::operator=(Qt::GlobalColor color)

将此画笔设置为具有给定color 的实心图案画笔,并返回对此画笔的引用。

这些函数是在 Qt 6.9 中引入的。

[since 6.9] QBrush &QBrush::operator=(Qt::BrushStyle style)

将此画笔设置为指定style 的黑色画笔,并返回对此画笔的引用。

这是一个重载函数。

该函数于 Qt 6.9 中引入。

bool QBrush::operator==(const QBrush &brush) const

如果画笔与给定的brush 相等,则返回true ;否则返回false 。

如果两个画笔的样式、颜色和变换相同,并且根据样式不同,其位图或渐变也相同,则这两个画笔被视为相等。

另请参阅 operator!=()。

相关的非成员

QDataStream &operator<<(QDataStream &stream, const QBrush &brush)

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

另请参阅 《Qt 数据类型的序列化》。

QDataStream &operator>>(QDataStream &stream, QBrush &brush)

从给定的“stream ”中读取指定的brush ,并返回对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.