Qt之自定义控件阴影
来源:互联网 发布:mac相册导入iphone 编辑:程序博客网 时间:2024/05/16 11:08
1.默认阴影
QGraphicsDropShadowEffect *defaultShadow = new QGraphicsDropShadowEffect();defaultShadow->setBlurRadius(9.0);defaultShadow->setColor(QColor(0, 0, 0, 160));defaultShadow->setOffset(4.0);ui->pushButtonDefault->setGraphicsEffect(defaultShadow);2.自定义阴影
customshadoweffect.h
#ifndef CUSTOMSHADOWEFFECT_H#define CUSTOMSHADOWEFFECT_H#include <QGraphicsDropShadowEffect>#include <QGraphicsEffect>class CustomShadowEffect : public QGraphicsEffect{ Q_OBJECTpublic: 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_Hcustomshadoweffect.cpp
#include "customshadoweffect.h"#include <QPainter>// #include <QGraphicsEffect>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_NAMESPACEvoid 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));}用于Graphics Item
// ...CustomShadowEffect *bodyShadow = new CustomShadowEffect();bodyShadow->setBlurRadius(20.0);bodyShadow->setDistance(6.0);bodyShadow->setColor(QColor(0, 0, 0, 80));item->setGraphicsEffect(bodyShadow);// ...用于控件
//...CustomShadowEffect *bodyShadow = new CustomShadowEffect();bodyShadow->setBlurRadius(20.0);bodyShadow->setDistance(6.0);bodyShadow->setColor(QColor(0, 0, 0, 80));ui->widget->setGraphicsEffect(bodyShadow);// ...3.下图是默认和自定义两种情况下作用于按钮的效果
参考链接:https://stackoverflow.com/questions/23718827/qt-shadow-around-window
阅读全文
1 0
- Qt之自定义控件阴影
- QT-控件阴影效果
- Qt自定义控件之QSwitchButton
- [Qt]自定义控件之开关控件
- Qt自定义控件之------QProgressBar的自定义
- Qt之阴影边框
- Qt之阴影边框
- Qt之阴影边框
- Qt之阴影边框
- 自定义控件之ImageView实现点击之后有阴影效果
- Qt 控件添加阴影效果
- 自定义控件(阴影,渐变)
- Qt之自定义控件(开关按钮)
- Qt之自定义控件(开关按钮)
- Qt控件之自定义ComboBox样式
- Qt自定义委托之按钮控件
- 【QT】列表控件之自定义显示
- QT 自定义控件之速度表盘
- Liunx中分区过程
- 想使用Docker容器?先看看这些注意事项
- .net core2.0下使用Identity改用dapper存储数据
- ASP.NET Core 认证与授权[6]:授权策略是怎么执行的?
- springboot中使用Mybatis注解配置详解
- Qt之自定义控件阴影
- mongoDB
- 跟着老桂学ASP.NET Core 2.0
- Windows下配置Git服务器和客户端
- js 之IIFE 立即执行函数表达式
- 主成分分析(PCA)
- Maven疑难JAR 下载 JMS JMXTOOLS zoomdu
- 在ROS中使用Hokuyo激光雷达UST-10LX和UST-30LX
- python 多个字符串 "a",'b',"c"=('a', 'b', 'c')