- Qt 6.11
- Qt Canvas Painter
- C++ Classes
- QtCanvasPainter (Technical Preview)
- QCanvasBoxShadow
- Qt Canvas Painter | Commercial or GPLv3

# 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
Inherits: | QCanvasBrush
Status: | Technical Preview

- List of all members, including inherited members

## Public Functions

| QCanvasBoxShadow()
| QCanvasBoxShadow(const QRectF &rect, float radius = 0.0f, float blur = 0.0f, const QColor &color = QColorConstants::Black)
| QCanvasBoxShadow(float x, float y, float width, float height, float radius = 0.0f, float blur = 0.0f, const QColor &color = QColorConstants::Black)
float | blur() const
float | bottomLeftRadius() const
float | bottomRightRadius() const
QRectF | boundingRect() const
QColor | color() const
float | radius() const
QRectF | rect() const
void | setBlur(float blur)
void | setBottomLeftRadius(float radius)
void | setBottomRightRadius(float radius)
void | setColor(const QColor &color)
void | setRadius(float radius)
void | setRect(float x, float y, float width, float height)
void | setRect(const QRectF &rect)
void | setSpread(float spread)
void | setTopLeftRadius(float radius)
void | setTopRightRadius(float radius)
float | spread() const
float | topLeftRadius() const
float | topRightRadius() const

## Related Non-Members

bool | operator!=(const QCanvasBoxShadow &lhs, const QCanvasBoxShadow &rhs)
QDataStream & | operator<<(QDataStream &stream, const QCanvasBoxShadow &shadow)
bool | operator==(const QCanvasBoxShadow &lhs, const QCanvasBoxShadow &rhs)
QDataStream & | operator>>(QDataStream &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:

QCanvasBoxShadow:

## 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`.

### QCanvasBoxShadow::QCanvasBoxShadow(const QRectF &rect, float radius = 0.0f, float blur = 0.0f, const QColor &color = QColorConstants::Black)

Constructs a box shadow. The box shadow position and size are defined by rect. Shadow corner radius is radius and blur blur. Shadow color is color.

### QCanvasBoxShadow::QCanvasBoxShadow(float x, float y, float width, float height, float radius = 0.0f, float blur = 0.0f, const QColor &color = QColorConstants::Black)

Constructs a box shadow. The box shadow position is x, y and size width, height. Shadow corner radius is radius and blur blur. Shadow color is color.

### float QCanvasBoxShadow::blur() const

Returns the blur of shadow box.

See also setBlur().

### float 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().

### float 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().

### QRectF 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().

### QColor QCanvasBoxShadow::color() const

Returns the color of shadow box.

See also setColor().

### float QCanvasBoxShadow::radius() const

Returns the radius of shadow box.

See also setRadius().

### QRectF QCanvasBoxShadow::rect() const

Returns the rect area of shadow box.

See also setRect().

### void QCanvasBoxShadow::setBlur(float blur)

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

See also blur().

### void QCanvasBoxShadow::setBottomLeftRadius(float 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(float 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(const QColor &color)

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

See also color().

### void QCanvasBoxShadow::setRadius(float 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(float x, float y, float width, float height)

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

See also rect().

### void QCanvasBoxShadow::setRect(const QRectF &rect)

Sets the rect area of shadow box to rect.

This is an overloaded function.

### void QCanvasBoxShadow::setSpread(float spread)

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

See also spread().

### void QCanvasBoxShadow::setTopLeftRadius(float 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(float 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().

### float QCanvasBoxShadow::spread() const

Returns the spread of shadow box.

See also setSpread().

### float 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().

### float 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

### `[noexcept]` bool operator!=(const QCanvasBoxShadow &lhs, const QCanvasBoxShadow &rhs)

Returns `true` if the box shadow lhs is different from rhs; `false` otherwise.

See also operator==().

### QDataStream &operator<<(QDataStream &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.

### `[noexcept]` bool operator==(const QCanvasBoxShadow &lhs, const QCanvasBoxShadow &rhs)

Returns `true` if the box shadow lhs is equal to rhs; `false` otherwise.

See also operator!=().

### QDataStream &operator>>(QDataStream &stream, QCanvasBoxShadow &shadow)

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

See also Serializing Qt Data Types.