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参考文献
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
结尾
只为记录,只为分享! 愿所写能对你有所帮助。不忘记点个赞,谢谢~
参考地址
http://blog.csdn.net/ly305750665/article/details/79209774
Qt之QGraphicsEffect阴影、模糊效果的更多相关文章
- Qt之圆角阴影边框
Qt的主窗体要做出类似WIN7那种圆角阴影边框,这一直是美工的需求. 这里是有一些门道的,尤其是,这里藏着一个很大的秘密. 这个秘密是一个QT的至少横跨3个版本,存在了2年多的BUG... https ...
- QT笔记之实现阴影窗口
方法一: 代码实现 在窗口构造函数中加入:setAttribute(Qt::WA_TranslucentBackground),保证不被绘制上的部分透明 重写void paintEvent(QPain ...
- Qt之自定义QLineEdit右键菜单
一.QLineEdit说明 QLineEdit是单行文本框,不同于QTextEdit,他只能显示一行文本,通常可以用作用户名.密码和搜索框等.它还提供了一些列的信号和槽,方便我们使用,有兴趣的小伙伴可 ...
- HTML5 Canvas阴影用法演示
HTML5 Canvas阴影用法演示 HTML5 Canvas中提供了设置阴影的四个属性值分别为: context.shadowColor = “red” 表示设置阴影颜色为红色 context.sh ...
- canvas设置阴影
canvas设置阴影 属性 shadowOffsetX = float 阴影向右偏移量 shadowOffsetY = float 阴影向下偏移量 shadowBlur = float 阴影模糊效果 ...
- CSS知识总结(五)
CSS常用样式 3.边框样式 1)边框线 border-style : none | hidden | dotted | dashed | solid | double | groove | ridg ...
- ppmoney 总结一
1.JQ $.get() <!DOCTYPE html> <html lang="en"> <head> <meta charset=&q ...
- #8.11.16总结#CSS常用样式总结(二)
border 边框 简写:border:1px solid #000; 等效于:border-width:1px;border-style:solid;border-color:#000; 顺序:b ...
- h5 canvas 画图
h5 canvas 画图 <!DOCTYPE html> <html lang="en"> <head> <meta charset=&q ...
随机推荐
- 【BZOJ 2534】Uva10829L-gap字符串
[链接]h在这里写链接 [题意] 让你找出uvu形式的字符串个数. v非空且长度为L,且u也非空 [题解] 之前做过一道相同的题. 枚举u的长度. 然后从u,2 ...
- dom4j解析xml获取所有的子节点并放入map中
dom4j递归解析所有子节点 //解析返回的xml字符串,生成document对象 Document document = DocumentHelper.parseText(resultXml); / ...
- 最简单的基于JavaEE和FFmpeg的视频网站
最简单的视频网站 Simplest Video Website 雷霄骅 Lei Xiaohua leixiaohua1020@126.com 中国传媒大学/数字电视技术 Communication U ...
- Python logging模块无法正常输出日志
废话少说,先上代码 File:logger.conf [formatters] keys=default [formatter_default] format=%(asctime)s - %(name ...
- java项目采用exe4j打包成exe档
java项目采用exe4j打包成exe档 前言:我们都知道java是平台无关性.能够打包成jar文件,到不论什么操作系统有jre环境的电脑都能够同意!可是我们打包成exe文件就相当于舍弃了这一大优势, ...
- 还在使用vc6.0吗??vs2010吧
每个人在面对新的事物的时候,总是充满了排斥感,当vista和win7来临的时候,我们还在留恋xp,说xp是如何的好,win7是如何的不给力,当然,我们必须承认,xp是一款优秀的操作系统,这个我们无法否 ...
- 最新用WPF为触摸屏写了一个手写程序,双格输入的
原文:最新用WPF为触摸屏写了一个手写程序,双格输入的 双格输入可以提高手写速度,当前字写完以后可以自动识别提交,写下一个字.这样比单格手写速度提高一倍.特别适合触摸屏程序使用 界面如下: 程序如下: ...
- windows添加本地文件托管到新增github库
新增repositoy.登录gitHub,并点击“New Reposoitory” 写入名字 之后点击“create resposity” \ 按照上图中的步骤可以完成.以下为完成步骤. 2. 在本 ...
- Spring MVC专题
Spring从3.1版本开始增加了ConfigurableEnvironment和PropertySource: ConfigurableEnvironment Spring的ApplicationC ...
- WebAPI Delete方法报错405 Method Not Allowed
.net framework 在Web.config文件中添加如下配置: <system.webServer> <modules runAllManagedModulesForAll ...