Qt: shadow around window
Asked Answered
S

2

12

I can add shadow to widget:

QGraphicsDropShadowEffect *bodyShadow = new QGraphicsDropShadowEffect;
bodyShadow->setBlurRadius(9.0);
bodyShadow->setColor(QColor(0, 0, 0, 160));
bodyShadow->setOffset(4.0);
ui->widget->setGraphicsEffect(bodyShadow);

But this shadow will be only at left and down. I need shadow around widget. How to add this?

Sharpnosed answered 18/5, 2014 at 5:23 Comment(1)
I've updated my answer so you can see how the effect can be applied to a widget.Jetport
J
22

You can create your custom effect. I'll share an implementation that works.

customshadoweffect.h

#ifndef CUSTOMSHADOWEFFECT_H
#define CUSTOMSHADOWEFFECT_H

#include <QGraphicsDropShadowEffect>
#include <QGraphicsEffect>

class CustomShadowEffect : public QGraphicsEffect
{
    Q_OBJECT
public:
    explicit CustomShadowEffect(QObject *parent = 0);

    void draw(QPainter* painter);
    QRectF boundingRectFor(const QRectF& rect) const;

    inline void setDistance(qreal distance) { _distance = distance; updateBoundingRect(); }
    inline qreal distance() const { return _distance; }

    inline void setBlurRadius(qreal blurRadius) { _blurRadius = blurRadius; updateBoundingRect(); }
    inline qreal blurRadius() const { return _blurRadius; }

    inline void setColor(const QColor& color) { _color = color; }
    inline QColor color() const { return _color; }

private:
    qreal  _distance;
    qreal  _blurRadius;
    QColor _color;
};

#endif // CUSTOMSHADOWEFFECT_H

customshadoweffect.cpp

#include "customshadoweffect.h"
#include <QPainter>
// #include <QGraphicsEffect>

CustomShadowEffect::CustomShadowEffect(QObject *parent) :
    QGraphicsEffect(parent),
    _distance(4.0f),
    _blurRadius(10.0f),
    _color(0, 0, 0, 80)
{
}

QT_BEGIN_NAMESPACE
  extern Q_WIDGETS_EXPORT void qt_blurImage(QPainter *p, QImage &blurImage, qreal radius, bool quality, bool alphaOnly, int transposed = 0 );
QT_END_NAMESPACE

void CustomShadowEffect::draw(QPainter* painter)
{
    // if nothing to show outside the item, just draw source
    if ((blurRadius() + distance()) <= 0) {
        drawSource(painter);
        return;
    }

    PixmapPadMode mode = QGraphicsEffect::PadToEffectiveBoundingRect;
    QPoint offset;
    const QPixmap px = sourcePixmap(Qt::DeviceCoordinates, &offset, mode);

    // return if no source
    if (px.isNull())
        return;

    // save world transform
    QTransform restoreTransform = painter->worldTransform();
    painter->setWorldTransform(QTransform());

    // Calculate size for the background image
    QSize szi(px.size().width() + 2 * distance(), px.size().height() + 2 * distance());

    QImage tmp(szi, QImage::Format_ARGB32_Premultiplied);
    QPixmap scaled = px.scaled(szi);
    tmp.fill(0);
    QPainter tmpPainter(&tmp);
    tmpPainter.setCompositionMode(QPainter::CompositionMode_Source);
    tmpPainter.drawPixmap(QPointF(-distance(), -distance()), scaled);
    tmpPainter.end();

    // blur the alpha channel
    QImage blurred(tmp.size(), QImage::Format_ARGB32_Premultiplied);
    blurred.fill(0);
    QPainter blurPainter(&blurred);
    qt_blurImage(&blurPainter, tmp, blurRadius(), false, true);
    blurPainter.end();

    tmp = blurred;

    // blacken the image...
    tmpPainter.begin(&tmp);
    tmpPainter.setCompositionMode(QPainter::CompositionMode_SourceIn);
    tmpPainter.fillRect(tmp.rect(), color());
    tmpPainter.end();

    // draw the blurred shadow...
    painter->drawImage(offset, tmp);

    // draw the actual pixmap...
    painter->drawPixmap(offset, px, QRectF());

    // restore world transform
    painter->setWorldTransform(restoreTransform);
}

QRectF CustomShadowEffect::boundingRectFor(const QRectF& rect) const
{
    qreal delta = blurRadius() + distance();
    return rect.united(rect.adjusted(-delta, -delta, delta, delta));
}

Applying it (to a graphics item):

// ...
CustomShadowEffect *bodyShadow = new CustomShadowEffect();
bodyShadow->setBlurRadius(20.0);
bodyShadow->setDistance(6.0);
bodyShadow->setColor(QColor(0, 0, 0, 80));
item->setGraphicsEffect(bodyShadow);
// ...

Applying it (to a child widget):

//...
CustomShadowEffect *bodyShadow = new CustomShadowEffect();
bodyShadow->setBlurRadius(20.0);
bodyShadow->setDistance(6.0);
bodyShadow->setColor(QColor(0, 0, 0, 80));
ui->widget->setAutoFillBackground(true);
ui->widget->setGraphicsEffect(bodyShadow);
// ...

Result: Effect applied to a child widget in a QMainWindow

Jetport answered 20/5, 2014 at 6:47 Comment(4)
Thank you for this implementation. But sadly this cannot be used in PySide since qt_blurImage is inaccessible.Chigoe
@Chigoe Try this in PySide: _qt_blurImage = getattr(ctypes.CDLL('QtGui4.dll'), '?qt_blurImage@@YAXPEAVQPainter@@AAVQImage@@N_N2H@Z')Hertzog
How should I use this for QQuickPaintedItem item?Operator
Get this error: undefined reference to `__imp__ZN6Symlex12qt_blurImageEP8QPainterR6QImagedbbi'Pigpen
C
2

I love mhcuervo's example of how to implement a custom effect. That said, fwiw, I was able to get a shadow around the child widget using your code, an offset of 0, and a large enough blur radius to spread it out around the child widget. (this, using Qt 5.4).

Cence answered 19/4, 2016 at 21:56 Comment(1)
Yes, this very simple answer worked for me too, for widgets of arbitrary geometry.Speedy

© 2022 - 2024 — McMap. All rights reserved.