透明效果
情况一
//窗口整个透明属性,取值为0-1,0为全透明
setWindowOpacity(0.5);

情况二
//部件不透明,窗体背景完全透明,以下两个函数必须配合使用
setWindowFlags(Qt::FramelessWindowHint);//窗口无边框
setAttribute(Qt::WA_TranslucentBackground);//背景透明

情况三
//单个部件设置透明
//需要添加头文件#include<QGraphicsOpacityEffect>
QGraphicsOpacityEffect*opacityEffect=new QGraphicsOpacityEffect;
opacityEffect->setOpacity(0.1); //0为完全透明,1为不透明
ui->label->setGraphicsEffect(opacityEffect);

情况四
窗口半透明,部件不透明:需要重写paintEvent
setWindowFlags(Qt::FramelessWindowHint); //窗口无边框
setAttribute(Qt::WA_TranslucentBackground);//背景透明
//重写绘图事件
void MainWindow::paintEvent(QPaintEvent *event)
{
QPainter painter(this);
painter.fillRect(rect(),QColor(255,255,255,200));//rect:填充矩形区域+rgb值+透明度为100
}

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

本文详细介绍如何在Qt中实现窗口及部件的透明效果,并添加阴影效果,包括不同透明度设置、背景透明处理、单个部件透明设置以及重写绘图事件的方法。

498

被折叠的 条评论
为什么被折叠?



