c++ - Qt:窗口周围的阴影

标签 c++ qt window shadow qwidget

我可以给小部件添加阴影:

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

但是这个影子只会在左边和下面。我需要小部件周围的阴影。如何添加?

最佳答案

您可以创建自定义效果。我将分享一个有效的实现。

自定义阴影效果.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

自定义阴影效果.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));
}

应用它(到图形项目):

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

应用它(到子部件):

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

结果: Effect applied to a child widget in a QMainWindow

关于c++ - Qt:窗口周围的阴影,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23718827/

相关文章:

swift - 从其他 View Controller 打开窗口+ View

window - 在 windows live messenger 上使用控制发送用于自动输入文本

c++ - 读取字符串作为命令行参数 C++

c++ - 我无法重写基类的方法,因为我的派生类是模板化的

c++ - Qt 中的截屏支持?

qt - QML中的树遍历,检查QModelIndex是否有效

c++ - *.pro 文件的用途是什么?

c++ memset导致int **指针的段错误

c++ - C++ 中的模板断言?

android - Android Window.FEATURE_NO_TITLE 的对面?