一、控件阴影效果
为子部件添加阴影比较简单,使用如下方式:
QGraphicsDropShadowEffect *shadow_effect = new QGraphicsDropShadowEffect(this); shadow_effect->setOffset(-5, 5); shadow_effect->setColor(Qt::gray); shadow_effect->setBlurRadius(8); network_group_box->setGraphicsEffect(shadow_effect);
效果如下:
二、 窗口阴影效果(代码实现)
void DropShadowWidget::paintEvent(QPaintEvent *event) { QPainterPath path; path.setFillRule(Qt::WindingFill); path.addRect(10, 10, this->width()-20, this->height()-20); QPainter painter(this); painter.setRenderHint(QPainter::Antialiasing, true); painter.fillPath(path, QBrush(Qt::white)); QColor color(0, 0, 0, 50); for(int i=0; i<10; i++) { QPainterPath path; path.setFillRule(Qt::WindingFill); path.addRect(10-i, 10-i, this->width()-(10-i)*2, this->height()-(10-i)*2); color.setAlpha(150 - qSqrt(i)*50); painter.setPen(color); painter.drawPath(path); } }
三、窗口阴影效果(图片绘制)
阴影边框很常见,诸如360以及其他很多软件都有类似效果,了解CSS3的同学们应该都知道box-shadow,它就是来设定阴影效果的,那么Qt呢?看过一些资料,说是QSS是基于CSS2的,既然如此,box-shadow是基于CSS3的!那么Qt定然就用不了!
搜了一些资料,每张图片都做成阴影效果的固然不可能,直接舍弃(即使可以,也不采纳)。如果实时的去画图,效率太低,最后选择了拼图的方式!
效果如下:
左上角、左下角、右上角、右下角、上、下、左、右,这几个方向都绘制对应的图即可!
#include "shadow_widget.h" ShadowWidget::ShadowWidget(QWidget *parent) : QDialog(parent) { setWindowFlags(Qt::FramelessWindowHint | Qt::Dialog); setAttribute(Qt::WA_TranslucentBackground); } ShadowWidget::~ShadowWidget() { } void ShadowWidget::paintEvent(QPaintEvent *event) { QPainter painter(this); this->drawShadow(painter); painter.setPen(Qt::NoPen); painter.setBrush(Qt::white); painter.drawRect(QRect(SHADOW_WIDTH, SHADOW_WIDTH, this->width()-2*SHADOW_WIDTH, this->height()-2*SHADOW_WIDTH)); } void ShadowWidget::drawShadow(QPainter &painter) { //绘制左上角、左下角、右上角、右下角、上、下、左、右边框 QList pixmaps; pixmaps.append(QPixmap(":/shadow/shadow_left")); pixmaps.append(QPixmap(":/shadow/shadow_right")); pixmaps.append(QPixmap(":/shadow/shadow_top")); pixmaps.append(QPixmap(":/shadow/shadow_bottom")); pixmaps.append(QPixmap(":/shadow/shadow_left_top")); pixmaps.append(QPixmap(":/shadow/shadow_right_top")); pixmaps.append(QPixmap(":/shadow/shadow_left_bottom")); pixmaps.append(QPixmap(":/shadow/shadow_right_bottom")); painter.drawPixmap(0, 0, SHADOW_WIDTH, SHADOW_WIDTH, pixmaps[4]); painter.drawPixmap(this->width()-SHADOW_WIDTH, 0, SHADOW_WIDTH, SHADOW_WIDTH, pixmaps[5]); painter.drawPixmap(0,this->height()-SHADOW_WIDTH, SHADOW_WIDTH, SHADOW_WIDTH, pixmaps[6]); painter.drawPixmap(this->width()-SHADOW_WIDTH, this->height()-SHADOW_WIDTH, SHADOW_WIDTH, SHADOW_WIDTH, pixmaps[7]); painter.drawPixmap(0, SHADOW_WIDTH, SHADOW_WIDTH, this->height()-2*SHADOW_WIDTH, pixmaps[0].scaled(SHADOW_WIDTH, this->height()-2*SHADOW_WIDTH)); painter.drawPixmap(this->width()-SHADOW_WIDTH, SHADOW_WIDTH, SHADOW_WIDTH, this->height()-2*SHADOW_WIDTH, pixmaps[1].scaled(SHADOW_WIDTH, this->height()- 2*SHADOW_WIDTH)); painter.drawPixmap(SHADOW_WIDTH, 0, this->width()-2*SHADOW_WIDTH, SHADOW_WIDTH, pixmaps[2].scaled(this->width()-2*SHADOW_WIDTH, SHADOW_WIDTH)); painter.drawPixmap(SHADOW_WIDTH, this->height()-SHADOW_WIDTH, this->width()-2*SHADOW_WIDTH, SHADOW_WIDTH, pixmaps[3].scaled(this->width()-2*SHADOW_WIDTH, SHADOW_WIDTH)); }
作者:江南烟雨居
本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。