Qt之QGraphicsEffect阴影、模糊效果

时间:2023-02-03 09:58:54

Qt之QGraphicsEffect阴影、模糊效果

 

 

效果图

阴影和模糊效果

Qt之QGraphicsEffect阴影、模糊效果

正常效果

Qt之QGraphicsEffect阴影、模糊效果

代码

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
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31

customshadoweffect.cpp

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

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));
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76

QGraphicsBlurEffect 模糊度setBlurRadius参考文献

Qt之QGraphicsEffect阴影、模糊效果

Applying It

CustomShadow::CustomShadow(QWidget *parent)
: QWidget(parent)
{
ui.setupUi(this);
ui.pushButton->installEventFilter(this);
ui.widget->installEventFilter(this);
//模糊效果
auto blurEffect = new QGraphicsBlurEffect(ui.lineEdit);
blurEffect->setBlurRadius(2);
ui.lineEdit->setGraphicsEffect(blurEffect);
}

bool CustomShadow::eventFilter(QObject *obj, QEvent *event)
{
if (obj == ui.pushButton) {
if (event->type() == QEvent::Enter)
{
Shadow* bodyShadow = new Shadow(ui.pushButton);
ui.pushButton->setGraphicsEffect(bodyShadow);
return true;
}
else if (event->type() == QEvent::Leave)
{
ui.pushButton->setGraphicsEffect(nullptr);
return true;
}
else {
return false;
}
}
else if (obj == ui.widget) {
if (event->type() == QEvent::Enter)
{
Shadow* bodyShadow = new Shadow(ui.widget);
ui.widget->setGraphicsEffect(bodyShadow);
return true;
}
else if (event->type() == QEvent::Leave)
{
ui.widget->setGraphicsEffect(nullptr);
return true;
}
else {
return false;
}
}
else {
// pass the event on to the parent class
return __super::eventFilter(obj, event);
}
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51

结尾

只为记录,只为分享! 愿所写能对你有所帮助。不忘记点个赞,谢谢~

参考地址

Qt: shadow around window 

http://blog.csdn.net/ly305750665/article/details/79209774