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参考文献

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

Qt之QGraphicsEffect阴影、模糊效果的更多相关文章

  1. Qt之圆角阴影边框

    Qt的主窗体要做出类似WIN7那种圆角阴影边框,这一直是美工的需求. 这里是有一些门道的,尤其是,这里藏着一个很大的秘密. 这个秘密是一个QT的至少横跨3个版本,存在了2年多的BUG... https ...

  2. QT笔记之实现阴影窗口

    方法一: 代码实现 在窗口构造函数中加入:setAttribute(Qt::WA_TranslucentBackground),保证不被绘制上的部分透明 重写void paintEvent(QPain ...

  3. Qt之自定义QLineEdit右键菜单

    一.QLineEdit说明 QLineEdit是单行文本框,不同于QTextEdit,他只能显示一行文本,通常可以用作用户名.密码和搜索框等.它还提供了一些列的信号和槽,方便我们使用,有兴趣的小伙伴可 ...

  4. HTML5 Canvas阴影用法演示

    HTML5 Canvas阴影用法演示 HTML5 Canvas中提供了设置阴影的四个属性值分别为: context.shadowColor = “red” 表示设置阴影颜色为红色 context.sh ...

  5. canvas设置阴影

    canvas设置阴影 属性 shadowOffsetX = float 阴影向右偏移量 shadowOffsetY = float 阴影向下偏移量 shadowBlur = float 阴影模糊效果 ...

  6. CSS知识总结(五)

    CSS常用样式 3.边框样式 1)边框线 border-style : none | hidden | dotted | dashed | solid | double | groove | ridg ...

  7. ppmoney 总结一

    1.JQ $.get() <!DOCTYPE html> <html lang="en"> <head> <meta charset=&q ...

  8. #8.11.16总结#CSS常用样式总结(二)

    border  边框 简写:border:1px solid #000; 等效于:border-width:1px;border-style:solid;border-color:#000; 顺序:b ...

  9. h5 canvas 画图

    h5 canvas 画图 <!DOCTYPE html> <html lang="en"> <head> <meta charset=&q ...

随机推荐

  1. 【水水水】【洛谷 U4566】赛车比赛

    题目背景 kkk在赛车~ 题目描述 现在有N辆赛车行驶在一条直线跑道(你可以认为跑道无限长)上.它们各自以某种速度匀速前进,如果有两辆车A车和B车,A车在B车的后面,且A车的速度大于B车的速度,那么经 ...

  2. poj1639 Picnic Planning,K度限制生成树

    题意: 矮人虽小却喜欢乘坐巨大的轿车,车大到能够装下不管多少矮人.某天,N(N≤20)个矮人打算到野外聚餐.为了集中到聚餐地点,矮人A 要么开车到矮人B 家中,留下自己的轿车在矮人B 家,然后乘坐B ...

  3. Android SDK location should not contain whitespace, as this cause problems with NDK tools

    解决方案一: The easiest solution is to move the SDK somewhere else, where there is no space or other whit ...

  4. Spring boot quartz的相关资源

    https://github.com/82253452/banner https://github.com/lvhao/schedule-job/tree/master/src/main/java/c ...

  5. Codeforces 39J Spelling Check hash

    主题链接:点击打开链接 意甲冠军: 特定2弦 选择中删除一个字符串的第一个字母,得2个字符串全然同样 问哪些位置能够选 思路: hash求前缀后缀.然后枚举位置 #include <cstdio ...

  6. Windows Container 和 Docker

    Windows Container 和 Docker 微软在2016年的Ignite技术大会上正式发布了Windows Server 2016,其中的容器服务已经可以作为生产环境使用.这意味着Wind ...

  7. cordova使用cordova-plugin-baidumaplocation插件获取定位

    原文:cordova使用cordova-plugin-baidumaplocation插件获取定位 版权声明:本文为博主原创文章,未经博主允许不得转载. https://blog.csdn.net/m ...

  8. altium designer电气符号和包的常用元素

    1. 标准电阻:RES1.RES2.包裹:AXIAL-0.3至AXIAL-1.0 两port可变电阻器:RES3.RES4.包裹:AXIAL-0.3至AXIAL-1.0 三port可变电阻:RESIS ...

  9. 解决:eth0安装插卡无法自己主动,网卡的配置信息不network-scripts于

    问题方案:eth0安装插卡无法自己主动,网卡的配置信息不network-scripts于 解决: 1>vi /etc/rc.d/rc.loacl 最后加 ifup eth0 2>reboo ...

  10. windows通过使用xShell远程linux上传文件

    上传文件rz与sz命令,远程linux您需要在系统上安装lrzsz工具包 安装例如,下面的: [xxxx@xxxx /]# yum install lrzsz 注意:我用命令yum,假设在Intern ...