Qt
Internal/Contributor docs for the Qt SDK. Note: These are NOT official API docs; those are found at https://doc.qt.io/
Loading...
Searching...
No Matches
QBrush Class Reference

\inmodule QtGui More...

#include <qbrush.h>

+ Collaboration diagram for QBrush:

Public Types

using DataPtr = std::unique_ptr<QBrushData, QBrushDataPointerDeleter>
 

Public Member Functions

 QBrush ()
 Constructs a default black brush with the style Qt::NoBrush (i.e.
 
 QBrush (Qt::BrushStyle bs)
 Constructs a black brush with the given style.
 
 QBrush (const QColor &color, Qt::BrushStyle bs=Qt::SolidPattern)
 Constructs a brush with the given color and style.
 
 QBrush (Qt::GlobalColor color, Qt::BrushStyle bs=Qt::SolidPattern)
 Constructs a brush with the given color and style.
 
 QBrush (const QColor &color, const QPixmap &pixmap)
 Constructs a brush with the given color and the custom pattern stored in pixmap.
 
 QBrush (Qt::GlobalColor color, const QPixmap &pixmap)
 Constructs a brush with the given color and the custom pattern stored in pixmap.
 
 QBrush (const QPixmap &pixmap)
 Constructs a brush with a black color and a texture set to the given pixmap.
 
 QBrush (const QImage &image)
 Constructs a brush with a black color and a texture set to the given image.
 
 QBrush (const QBrush &brush)
 Constructs a copy of other.
 
 QBrush (const QGradient &gradient)
 Constructs a brush based on the given gradient.
 
 ~QBrush ()
 Destroys the brush.
 
QBrushoperator= (const QBrush &brush)
 Assigns the given brush to this brush and returns a reference to this brush.
 
void swap (QBrush &other) noexcept
 
QBrushoperator= (Qt::BrushStyle style)
 This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.
 
QBrushoperator= (QColor color)
 
QBrushoperator= (Qt::GlobalColor color)
 This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.
 
 operator QVariant () const
 Returns the brush as a QVariant.
 
Qt::BrushStyle style () const
 Returns the brush style.
 
void setStyle (Qt::BrushStyle)
 Sets the brush style to style.
 
QTransform transform () const
 
void setTransform (const QTransform &)
 
QPixmap texture () const
 Returns the custom brush pattern, or a null pixmap if no custom brush pattern has been set.
 
void setTexture (const QPixmap &pixmap)
 Sets the brush pixmap to pixmap.
 
QImage textureImage () const
 
void setTextureImage (const QImage &image)
 
const QColorcolor () const
 Returns the brush color.
 
void setColor (const QColor &color)
 Sets the brush color to the given color.
 
void setColor (Qt::GlobalColor color)
 This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.Sets the brush color to the given color.
 
const QGradientgradient () const
 Returns the gradient describing this brush.
 
bool isOpaque () const
 Returns true if the brush is fully opaque otherwise false.
 
bool operator== (const QBrush &b) const
 Returns true if the brush is equal to the given brush; otherwise returns false.
 
bool operator!= (const QBrush &b) const
 Returns true if the brush is different from the given brush; otherwise returns false.
 
bool isDetached () const
 
DataPtrdata_ptr ()
 

Friends

class QRasterPaintEngine
 
class QRasterPaintEnginePrivate
 
struct QSpanData
 
class QPainter
 
bool Q_GUI_EXPORT qHasPixmapTexture (const QBrush &brush)
 
bool comparesEqual (const QBrush &lhs, QColor rhs) noexcept
 
bool comparesEqual (const QBrush &lhs, Qt::BrushStyle rhs) noexcept
 

Related Symbols

(Note that these are not member symbols.)

QDataStreamoperator<< (QDataStream &stream, const QBrush &brush)
 Writes the given brush to the given stream and returns a reference to the stream.
 
QDataStreamoperator>> (QDataStream &stream, QBrush &brush)
 Reads the given brush from the given stream and returns a reference to the stream.
 

Detailed Description

\inmodule QtGui

The QBrush class defines the fill pattern of shapes drawn by QPainter.

A brush has a style, a color, a gradient and a texture.

The brush style() defines the fill pattern using the Qt::BrushStyle enum. The default brush style is Qt::NoBrush (depending on how you construct a brush). This style tells the painter to not fill shapes. The standard style for filling is Qt::SolidPattern. The style can be set when the brush is created using the appropriate constructor, and in addition the setStyle() function provides means for altering the style once the brush is constructed.

Brush Styles

The brush color() defines the color of the fill pattern. The color can either be one of Qt's predefined colors, Qt::GlobalColor, or any other custom QColor. The currently set color can be retrieved and altered using the color() and setColor() functions, respectively.

The gradient() defines the gradient fill used when the current style is either Qt::LinearGradientPattern, Qt::RadialGradientPattern or Qt::ConicalGradientPattern. Gradient brushes are created by giving a QGradient as a constructor argument when creating the QBrush. Qt provides three different gradients: QLinearGradient, QConicalGradient, and QRadialGradient

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);

The texture() defines the pixmap used when the current style is Qt::TexturePattern. You can create a brush with a texture by providing the pixmap when the brush is created or by using setTexture().

Note that applying setTexture() makes style() == Qt::TexturePattern, regardless of previous style settings. Also, calling setColor() will not make a difference if the style is a gradient. The same is the case if the style is Qt::TexturePattern style unless the current texture is a QBitmap.

The isOpaque() function returns true if the brush is fully opaque otherwise false. A brush is considered opaque if:

\list

  • The alpha component of the color() is 255.
  • Its texture() does not have an alpha channel and is not a QBitmap.
  • The colors in the gradient() all have an alpha component that is 255. \endlist

\table 100% \row

  • \inlineimage brush-outline.png Outlines

To specify the style and color of lines and outlines, use the QPainter's \l {QPen}{pen} combined with Qt::PenStyle and Qt::GlobalColor:

Note that, by default, QPainter renders the outline (using the currently set pen) when drawing shapes. Use \l {Qt::NoPen}{painter.setPen(Qt::NoPen)} to disable this behavior.

\endtable

For more information about painting in general, see the \l{Paint System}.

See also
Qt::BrushStyle, QPainter, QColor

Definition at line 28 of file qbrush.h.

Member Typedef Documentation

◆ DataPtr

Definition at line 80 of file qbrush.h.

Constructor & Destructor Documentation

◆ QBrush() [1/10]

QBrush::QBrush ( )

Constructs a default black brush with the style Qt::NoBrush (i.e.

this brush will not fill shapes).

Definition at line 425 of file qbrush.cpp.

◆ QBrush() [2/10]

QBrush::QBrush ( Qt::BrushStyle style)

Constructs a black brush with the given style.

See also
setStyle()

Definition at line 465 of file qbrush.cpp.

◆ QBrush() [3/10]

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

Constructs a brush with the given color and style.

See also
setColor(), setStyle()

Definition at line 476 of file qbrush.cpp.

◆ QBrush() [4/10]

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

Constructs a brush with the given color and style.

See also
setColor(), setStyle()

Definition at line 493 of file qbrush.cpp.

◆ QBrush() [5/10]

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

Constructs a brush with the given color and the custom pattern stored in pixmap.

The style is set to Qt::TexturePattern. The color will only have an effect for QBitmaps.

See also
setColor(), setTexture()

Definition at line 508 of file qbrush.cpp.

◆ QBrush() [6/10]

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

Constructs a brush with the given color and the custom pattern stored in pixmap.

The style is set to Qt::TexturePattern. The color will only have an effect for QBitmaps.

See also
setColor(), setTexture()

Definition at line 524 of file qbrush.cpp.

◆ QBrush() [7/10]

QBrush::QBrush ( const QPixmap & pixmap)

Constructs a brush with a black color and a texture set to the given pixmap.

The style is set to Qt::TexturePattern.

See also
setTexture()

Definition at line 439 of file qbrush.cpp.

◆ QBrush() [8/10]

QBrush::QBrush ( const QImage & image)

Constructs a brush with a black color and a texture set to the given image.

The style is set to Qt::TexturePattern.

See also
setTextureImage()

Definition at line 453 of file qbrush.cpp.

◆ QBrush() [9/10]

QBrush::QBrush ( const QBrush & brush)

Constructs a copy of other.

Definition at line 534 of file qbrush.cpp.

◆ QBrush() [10/10]

QBrush::QBrush ( const QGradient & gradient)

Constructs a brush based on the given gradient.

The brush style is set to the corresponding gradient style (either Qt::LinearGradientPattern, Qt::RadialGradientPattern or Qt::ConicalGradientPattern).

Definition at line 547 of file qbrush.cpp.

◆ ~QBrush()

QBrush::~QBrush ( )

Destroys the brush.

Definition at line 570 of file qbrush.cpp.

Member Function Documentation

◆ color()

const QColor & QBrush::color ( ) const
inline

Returns the brush color.

See also
setColor()

Definition at line 140 of file qbrush.h.

◆ data_ptr()

DataPtr & QBrush::data_ptr ( )
inline

Definition at line 110 of file qbrush.h.

◆ gradient()

const QGradient * QBrush::gradient ( ) const

Returns the gradient describing this brush.

Definition at line 865 of file qbrush.cpp.

◆ isDetached()

bool QBrush::isDetached ( ) const
inline

Definition at line 142 of file qbrush.h.

◆ isOpaque()

bool QBrush::isOpaque ( ) const

Returns true if the brush is fully opaque otherwise false.

A brush is considered opaque if:

\list

  • The alpha component of the color() is 255.
  • Its texture() does not have an alpha channel and is not a QBitmap.
  • The colors in the gradient() all have an alpha component that is 255.
  • It is an extended radial gradient. \endlist

Definition at line 904 of file qbrush.cpp.

◆ operator QVariant()

QBrush::operator QVariant ( ) const

Returns the brush as a QVariant.

Definition at line 710 of file qbrush.cpp.

◆ operator!=()

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

Returns true if the brush is different from the given brush; otherwise returns false.

Two brushes are different if they have different styles, colors or transforms or different pixmaps or gradients depending on the style.

See also
operator==()

Definition at line 78 of file qbrush.h.

◆ operator=() [1/4]

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

Assigns the given brush to this brush and returns a reference to this brush.

Definition at line 651 of file qbrush.cpp.

◆ operator=() [2/4]

QBrush & QBrush::operator= ( QColor color)

Definition at line 670 of file qbrush.cpp.

◆ operator=() [3/4]

QBrush & QBrush::operator= ( Qt::BrushStyle style)

This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.

Since
6.9

Makes this brush a black brush of the given style, and returns a reference to this brush.

Definition at line 685 of file qbrush.cpp.

◆ operator=() [4/4]

QBrush & QBrush::operator= ( Qt::GlobalColor color)
inline

This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.

Since
6.9

Makes this brush a solid pattern brush of the given color, and returns a reference to this brush.

Definition at line 53 of file qbrush.h.

◆ operator==()

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

Returns true if the brush is equal to the given brush; otherwise returns false.

Two brushes are equal if they have equal styles, colors and transforms and equal pixmaps or gradients depending on the style.

See also
operator!=()

Definition at line 972 of file qbrush.cpp.

◆ setColor() [1/2]

void QBrush::setColor ( const QColor & color)

Sets the brush color to the given color.

Note that calling setColor() will not make a difference if the style is a gradient. The same is the case if the style is Qt::TexturePattern style unless the current texture is a QBitmap.

See also
color()

Definition at line 761 of file qbrush.cpp.

◆ setColor() [2/2]

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

This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.Sets the brush color to the given color.

Definition at line 113 of file qbrush.h.

◆ setStyle()

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

Sets the brush style to style.

See also
style()

Definition at line 729 of file qbrush.cpp.

◆ setTexture()

void QBrush::setTexture ( const QPixmap & pixmap)

Sets the brush pixmap to pixmap.

The style is set to Qt::TexturePattern.

The current brush color will only have an effect for monochrome pixmaps, i.e. for QPixmap::depth() == 1 (\l {QBitmap}{QBitmaps}).

See also
texture()

Definition at line 802 of file qbrush.cpp.

◆ setTextureImage()

void QBrush::setTextureImage ( const QImage & image)
Since
4.2

Sets the brush image to image. The style is set to Qt::TexturePattern.

Note the current brush color will not have any affect on monochrome images, as opposed to calling setTexture() with a QBitmap. If you want to change the color of monochrome image brushes, either convert the image to QBitmap with QBitmap::fromImage() and set the resulting QBitmap as a texture, or change the entries in the color table for the image.

See also
textureImage(), setTexture()

Definition at line 850 of file qbrush.cpp.

◆ setTransform()

void QBrush::setTransform ( const QTransform & matrix)
Since
4.3

Sets matrix as an explicit transformation matrix on the current brush. The brush transformation matrix is merged with QPainter transformation matrix to produce the final result.

See also
transform()

Definition at line 941 of file qbrush.cpp.

◆ style()

Qt::BrushStyle QBrush::style ( ) const
inline

Returns the brush style.

See also
setStyle()

Definition at line 139 of file qbrush.h.

◆ swap()

void QBrush::swap ( QBrush & other)
inlinenoexcept
Since
4.8 \memberswap{brush}

Definition at line 48 of file qbrush.h.

◆ texture()

QPixmap QBrush::texture ( ) const

Returns the custom brush pattern, or a null pixmap if no custom brush pattern has been set.

See also
setTexture()

Definition at line 785 of file qbrush.cpp.

◆ textureImage()

QImage QBrush::textureImage ( ) const
Since
4.2

Returns the custom brush pattern, or a null image if no custom brush pattern has been set.

If the texture was set as a QPixmap it will be converted to a QImage.

See also
setTextureImage()

Definition at line 826 of file qbrush.cpp.

◆ transform()

QTransform QBrush::transform ( ) const
inline
Since
4.3

Returns the current transformation matrix for the brush.

See also
setTransform()

Definition at line 141 of file qbrush.h.

Friends And Related Symbol Documentation

◆ comparesEqual [1/2]

bool comparesEqual ( const QBrush & lhs,
QColor rhs )
friend

Definition at line 90 of file qbrush.h.

◆ comparesEqual [2/2]

bool comparesEqual ( const QBrush & lhs,
Qt::BrushStyle rhs )
friend

Definition at line 98 of file qbrush.h.

◆ operator<<()

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

Writes the given brush to the given stream and returns a reference to the stream.

See also
{Serializing Qt Data Types}

◆ operator>>()

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

Reads the given brush from the given stream and returns a reference to the stream.

See also
{Serializing Qt Data Types}

◆ qHasPixmapTexture

bool Q_GUI_EXPORT qHasPixmapTexture ( const QBrush & brush)
friend

Definition at line 206 of file qbrush.cpp.

◆ QPainter

friend class QPainter
friend

Definition at line 86 of file qbrush.h.

◆ QRasterPaintEngine

friend class QRasterPaintEngine
friend

Definition at line 83 of file qbrush.h.

◆ QRasterPaintEnginePrivate

friend class QRasterPaintEnginePrivate
friend

Definition at line 84 of file qbrush.h.

◆ QSpanData

friend struct QSpanData
friend

Definition at line 85 of file qbrush.h.


The documentation for this class was generated from the following files: