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. 亲测有效,解决Can 't connect to local MySQL server through socket '/tmp/mysql.sock '(2) ";

    版权声明:本文为博主原创文章,未经博主允许不得转载. https://blog.csdn.net/hjf161105/article/details/78850658 最近租了一个阿里云云翼服务器,趁 ...

  2. mysql5.6+主从集的版本号(mysql5.5主机和从机载带后,5.5在设置有一定的差距)

    怎么安装mysql数据库.这里不说了,仅仅说它的主从复制,过程例如以下 在进行主从设置之前 首先确保mysql主从server之间的数据库port防火墙互相打开, 尽量确保主从数据库账户一致性(主从切 ...

  3. 【24.58%】【BZOJ 1001】狼抓兔子

    Time Limit: 15 Sec Memory Limit: 162 MB Submit: 19227 Solved: 4726 [Submit][Status][Discuss] Descrip ...

  4. Android检测网络是否可用并获取网络类型

    在类中使用getSystemService的时候需要这样进行使用:1. public class JajaMenu extends Activity { public static JajaMenu ...

  5. Android 平台下Ftp 使用模拟器需要注意的问题

    以下代码在pc上测试通过,可是在android模拟器上就不工作,不过还可以链接到服务器但不能得到文件 纠结了半天,原来是模式的问题,具体请Google 模拟器中采用建议被动模式 public void ...

  6. 从Client应用场景介绍IdentityServer4(一)

    原文:从Client应用场景介绍IdentityServer4(一) 一.背景 IdentityServer4的介绍将不再叙述,百度下可以找到,且官网的快速入门例子也有翻译的版本.这里主要从Clien ...

  7. sgu209:Areas(计算几何)

    意甲冠军: 给一些直.这架飞机被分成了很多这些线性块.每个块的需求面积封闭曲线图. 分析: ①我们应要求交点22的直线: ②每行上的交点的重排序,借此来离散一整行(正反两条边): ③对于连向一个点的几 ...

  8. 分类算法SVM(支持向量机)

    支持向量机(Support Vector Machine ,SVM)的主要思想是:建立一个最优决策超平面,使得该平面两侧距离该平面最近的两类样本之间的距离最大化,从而对分类问题提供良好的泛化能力.对于 ...

  9. ISO/IEC 27001 信息安全管理体系认证

    一. 信息安全管理体系标准业务介绍 1. 背景介绍 信息作为组织的重要资产,需要得到妥善保护.但随着信息技术的高速发展,特别是Internet的问世及网上交易的启用,许多信息安全的问题也纷纷出现:系统 ...

  10. 计算机的组成 —— PCI(PCIE)、PCB

    1. PCI PCI 是 Peripheral Component Interconnect(外设部件互连标准)的缩写,它是目前个人电脑中使用最为广泛的接口,几乎所有的主板产品上都带有这种插槽. PC ...