部件透明效果及阴影效果

#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QGraphicsOpacityEffect>
#include <QGraphicsDropShadowEffect>
#include <QPainter>

MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);

//    //1.设置样式
//    ui->pushButton->setStyleSheet("background:yellow");
//    ui->pushButton->setStyleSheet("color:red");
//    setStyleSheet("QPushButton{background:yellow;color:red}");

//    ui->horizontalSlider->setStyleSheet("background:blue");

    //2.透明效果
    //窗口透明属性,取值0~1,0表完全透明,1表完全不透明
    //setWindowOpacite(0.5);

    //部件不透明,窗体完全透明,Windows下配合无边款风格Qt::FramelessWindowHint
    setWindowFlags(Qt::FramelessWindowHint);
    setAttribute(Qt::WA_TranslucentBackground);

    //3.部件设置透明
    //透明度效果  要添加头文件 #include <QGraphicsOpacityEffect>
//    QGraphicsOpacityEffect *opacityEffect = new QGraphicsOpacityEffect;
//    opacityEffect->setOpacity(0.4);
//    ui->label->setGraphicsEffect(opacityEffect);

    //4.阴影效果 添加头文件
//    QGraphicsDropShadowEffect *shadowEffect = new QGraphicsDropShadowEffect;
//    //阴影色,透明度
//    shadowEffect->setColor(QColor(100,100,100));
//    shadowEffect->setBlurRadius(20);//阴影模糊半径
//    shadowEffect->setOffset(20);//阴影偏移值
//    ui->label->setGraphicsEffect(shadowEffect);

}

//5.窗体半透明,重写void paintEvent(QPaintEvent* event);绘图事件
//把2透明效果打开
void MainWindow::paintEvent(QPaintEvent *event)
{
    QPainter painter(this);
    //绘图区域填充
    painter.fillRect(rect(),QColor(255,255,255,100));

}

MainWindow::~MainWindow()
{
    delete ui;
}

代码效果:

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值