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. Asp.net压缩网站中的文件

    为了说明自定义虚拟路径,这里弄个示例,仅仅用一个压缩包存放一个网站的多个文件. 这个东西是要需要通过实现3个抽象类来实现: System.Web.Hosting.VirtualPathProvider ...

  2. 矩阵微分(matrix derivatives)

    关于矩阵求导,得到的导数则是矩阵形式:关于矢量求导,得到的导数则是矢量形式:关于标量求导,得到的仍是标量形式.也即关于谁求导,得到的导数形式便和谁的维度信息一致. fx = f(x) grad = n ...

  3. 「两」创建一个带 ssh 镜座服务(修订版)--采用 Dockerfile 创

    创建目录 首先,创建一个叫做 sshd_ubuntu 的目录,用于存放我们的 Dockerfile .脚本文件.以及其它文件. $ mkdir sshd_ubuntu $ ls sshd_ubuntu ...

  4. Delphi png、bmp、gif等图片格式转换成jpg

    unit Unit1; interface uses Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms ...

  5. 深入python3 (Dive Into Python 3) 在线阅读与下载

    在线阅读:http://book.doucube.com/diveintopython3/  中文版 下载地址:https://github.com/downloads/diveintomark/di ...

  6. javascript-DOM学习

    javascript-DOM学习 DOM document(html) object modle document对象(DOM核心对象) dom能用来干什么? 对html元素的样式(颜色.大小.位置等 ...

  7. Android自注-15-Activity生命周期

    很长一段时间没有写博客,懒,感慨一下. Activity的生命周期是一块以下附图: 通过代码下面简单的介绍一下.一些内容看代码的凝视: package com.mxy; import android. ...

  8. CentOS 6安装桌面

    安装图形界面 yum -y groupinstall "X Window System" "Chinese Support" "Desktop&quo ...

  9. 线程操纵UI问题

    WPF只允许UI线程修改UI,其他线程必须通过Invoke.委托(安全性)Winform可以开启/关闭“只允许UI线程修改UI” 在WPF中非UI线程修改UI的方法 非UI线程直接修改UI,会报错 S ...

  10. WPF 寻找数据模板中的元素

    <Window x:Class="Wpf180706.Window11"        xmlns="http://schemas.microsoft.com/wi ...