QCBoxShadow Class
QCBoxShadow is a brush for painting rectangle shadows / glows. More...
| Header: | #include <QCBoxShadow> |
| CMake: | find_package(Qt6 REQUIRED COMPONENTS CanvasPainter)target_link_libraries(mytarget PRIVATE Qt6::CanvasPainter) |
| Since: | Qt 6.11 |
| Inherits: | QCBrush |
| Status: | Technical Preview |
Public Functions
| QCBoxShadow() | |
| QCBoxShadow(const QRectF &rect, float radius = 0.0f, float blur = 0.0f, const QColor &color = QColorConstants::Black) | |
| QCBoxShadow(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(const QRectF &rect) |
| void | setRect(float x, float y, float width, float height) |
| void | setSpread(float spread) |
| void | setTopLeftRadius(float radius) |
| void | setTopRightRadius(float radius) |
| float | spread() const |
| float | topLeftRadius() const |
| float | topRightRadius() const |
| bool | operator!=(const QCBoxShadow &shadow) const |
| bool | operator==(const QCBoxShadow &shadow) const |
Related Non-Members
| QDataStream & | operator<<(QDataStream &stream, const QCBoxShadow &shadow) |
| QDataStream & | operator>>(QDataStream &stream, QCBoxShadow &shadow) |
Detailed Description
QCBoxShadow is a rounded rectangle brush with blur applied. The performance of QCBoxShadow is much better than a general shadow that creates blurred shadow/glow of any shaped item, as it uses SDF approach.
The features of QCBoxShadow 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 QCBoxShadow 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 QCBoxShadow 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:

QCBoxShadow:

Member Function Documentation
QCBoxShadow::QCBoxShadow()
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.
QCBoxShadow::QCBoxShadow(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.
QCBoxShadow::QCBoxShadow(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 QCBoxShadow::blur() const
Returns the blur of shadow box.
See also setBlur().
float QCBoxShadow::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 QCBoxShadow::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 QCBoxShadow::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 QCPainter::drawBoxShadow() helper method, this can be used to for e.g. adding correctly sized QCPainter::rect() into the path.
See also setRect(), setBlur(), and setSpread().
QColor QCBoxShadow::color() const
Returns the color of shadow box.
See also setColor().
float QCBoxShadow::radius() const
Returns the radius of shadow box.
See also setRadius().
QRectF QCBoxShadow::rect() const
Returns the rect area of shadow box.
See also setRect().
void QCBoxShadow::setBlur(float blur)
Sets the shadow blur to blur in pixels. The default value is 0.0 meaning no blur.
See also blur().
void QCBoxShadow::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 QCBoxShadow::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 QCBoxShadow::setColor(const QColor &color)
Sets the shadow color to color. The default value is black with full opacity.
See also color().
void QCBoxShadow::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 QCBoxShadow::setRect(const QRectF &rect)
Sets the rect area of shadow box to rect.
See also rect().
void QCBoxShadow::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 QCBoxShadow::setSpread(float spread)
Sets the shadow spread to spread in pixels. The default value is 0.0 meaning no spread.
See also spread().
void QCBoxShadow::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 QCBoxShadow::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 QCBoxShadow::spread() const
Returns the spread of shadow box.
See also setSpread().
float QCBoxShadow::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 QCBoxShadow::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().
bool QCBoxShadow::operator!=(const QCBoxShadow &shadow) const
Returns true if the box shadow is different from the given shadow; otherwise false.
See also operator==().
bool QCBoxShadow::operator==(const QCBoxShadow &shadow) const
Returns true if the box shadow is equal to the given shadow; otherwise false.
See also operator!=().
Related Non-Members
QDataStream &operator<<(QDataStream &stream, const QCBoxShadow &shadow)
Writes the given shadow to the given stream and returns a reference to the stream.
See also Serializing Qt Data Types.
QDataStream &operator>>(QDataStream &stream, QCBoxShadow &shadow)
Reads the given shadow from the given stream and returns a reference to the stream.
See also Serializing Qt Data Types.