QCanvasBoxShadow Class

QCanvasBoxShadow is a brush for painting rectangle shadows / glows. More...

Header: #include <QCanvasBoxShadow>
CMake: find_package(Qt6 REQUIRED COMPONENTS CanvasPainter)
target_link_libraries(mytarget PRIVATE Qt6::CanvasPainter)
Since: Qt 6.11

Public Functions

QCanvasBoxShadow()
QCanvasBoxShadow(const int &rect)
QCanvasBoxShadow(int x, int y, int width, int height)
QCanvasBoxShadow(QCanvasBoxShadow &&)
int blur() const
int bottomLeftRadius() const
int bottomRightRadius() const
int boundingRect() const
int color() const
int radius() const
int rect() const
void setBlur(int blur)
void setBottomLeftRadius(int radius)
void setBottomRightRadius(int radius)
void setColor(int color)
void setRadius(int radius)
void setRect(int x, int y, int width, int height)
void setRect(const int &rect)
void setSpread(int spread)
void setTopLeftRadius(int radius)
void setTopRightRadius(int radius)
int spread() const
int topLeftRadius() const
int topRightRadius() const
int &operator<<(int &stream, const QCanvasBoxShadow &shadow)
int &operator>>(int &stream, QCanvasBoxShadow &shadow)

Detailed Description

QCanvasBoxShadow is a rounded rectangle brush with blur applied. The performance of QCanvasBoxShadow is much better than a general shadow that creates blurred shadow/glow of any shaped item, as it uses SDF approach.

The features of QCanvasBoxShadow are similar to CSS box-shadow, with radius, spread, blur and color values. The rendering output also matches the CSS box-shadow, with few notable differences to make the QCanvasBoxShadow as high-performance as possible. Blurring is calculated mathematically in the shader rather than using Gaussian blur, which CSS box-shadow implementations often use. This makes the shadow look slightly different, especially when the blur amount grows bigger than half of the shadow width / height.

Here are screenshots to compare the rendering output of QCanvasBoxShadow and CSS box-shadow in the Chrome browser. These show how blur radius of 0, 10, 20 and 40 pixels affect smaller shadows.

CSS box-shadow:

Rows of a black bar and a black dot with CSS box-shadow blur of 0, 10, 20, and 40 pixels

QCanvasBoxShadow:

Rows of a black bar and a black dot with QCanvasBoxShadow blur of 0, 10, 20, and 40 pixels

Member Function Documentation

QCanvasBoxShadow::QCanvasBoxShadow()

Constructs a default box shadow. The box shadow position is (0, 0) and size (100, 100). Shadow corner radius is 0.0 and blur 0.0. Shadow color is Qt::black.

[explicit] QCanvasBoxShadow::QCanvasBoxShadow(const int &rect)

Constructs a black box shadow with position of rect, and without corner radius or blur.

QCanvasBoxShadow::QCanvasBoxShadow(int x, int y, int width, int height)

Constructs a box shadow with position at x, y and size width, height.

See also blur() and radius().

[constexpr noexcept default] QCanvasBoxShadow::QCanvasBoxShadow(QCanvasBoxShadow &&)

Move-constructs an instance of QCanvasBoxShadow.

int QCanvasBoxShadow::blur() const

Returns the blur of shadow box.

See also setBlur().

int QCanvasBoxShadow::bottomLeftRadius() const

Returns the bottom-left radius of shadow box. When this is -1, painting of the shadow will use the value of radius() for this corner. The default value is -1.

See also setBottomLeftRadius().

int QCanvasBoxShadow::bottomRightRadius() const

Returns the bottom-right radius of shadow box. When this is -1, painting of the shadow will use the value of radius() for this corner. The default value is -1.

See also setBottomRightRadius().

int QCanvasBoxShadow::boundingRect() const

Returns the area covered by the shadow. This takes into account the shadow rect(), blur() and spread(). If you don't want to use QCanvasPainter::drawBoxShadow() helper method, this can be used to for e.g. adding correctly sized QCanvasPainter::rect() into the path.

See also setRect(), setBlur(), and setSpread().

int QCanvasBoxShadow::color() const

Returns the color of shadow box.

See also setColor().

int QCanvasBoxShadow::radius() const

Returns the radius of shadow box.

See also setRadius().

int QCanvasBoxShadow::rect() const

Returns the rect area of shadow box.

See also setRect().

void QCanvasBoxShadow::setBlur(int blur)

Sets the shadow blur to blur in pixels. The default value is 0.0 meaning no blur.

See also blur().

void QCanvasBoxShadow::setBottomLeftRadius(int radius)

Sets the bottom-left corner radius to radius. The default value is -1 which means that individual radius has not been set for this corner, and common radius() is used instead.

See also bottomLeftRadius().

void QCanvasBoxShadow::setBottomRightRadius(int radius)

Sets the bottom-right corner radius to radius. The default value is -1 which means that individual radius has not been set for this corner, and common radius() is used instead.

See also bottomRightRadius().

void QCanvasBoxShadow::setColor(int color)

Sets the shadow color to color. The default value is black with full opacity.

See also color().

void QCanvasBoxShadow::setRadius(int radius)

Sets the shadow corner radius to radius in pixels. The default value is 0.0 meaning no radius.

See also radius().

void QCanvasBoxShadow::setRect(int x, int y, int width, int height)

Sets the rect area of shadow box to (x, y, width, height).

See also rect().

void QCanvasBoxShadow::setRect(const int &rect)

Sets the rect area of shadow box to rect.

This is an overloaded function.

void QCanvasBoxShadow::setSpread(int spread)

Sets the shadow spread to spread in pixels. The default value is 0.0 meaning no spread.

See also spread().

void QCanvasBoxShadow::setTopLeftRadius(int radius)

Sets the top-left corner radius to radius. The default value is -1 which means that individual radius has not been set for this corner, and common radius() is used instead.

See also topLeftRadius().

void QCanvasBoxShadow::setTopRightRadius(int radius)

Sets the top-right corner radius to radius. The default value is -1 which means that individual radius has not been set for this corner, and common radius() is used instead.

See also topRightRadius().

int QCanvasBoxShadow::spread() const

Returns the spread of shadow box.

See also setSpread().

int QCanvasBoxShadow::topLeftRadius() const

Returns the top-left radius of shadow box. When this is -1, painting of the shadow will use the value of radius() for this corner. The default value is -1.

See also setTopLeftRadius().

int QCanvasBoxShadow::topRightRadius() const

Returns the top-right radius of shadow box. When this is -1, painting of the shadow will use the value of radius() for this corner. The default value is -1.

See also setTopRightRadius().

Related Non-Members

int &operator<<(int &stream, const QCanvasBoxShadow &shadow)

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

See also Serializing Qt Data Types.

int &operator>>(int &stream, QCanvasBoxShadow &shadow)

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

See also Serializing Qt Data Types.