QGraphicsDropShadowEffect设置阴影无法生效问题
1、简介
窗口阴影效果是界面开发中经常需要使用的一个效果,另外也可以通过绘制事件设置界面阴影,此文章只是总结使用QGraphicsDropShadowEffect设置窗口阴影时遇到的坑
(1)主窗口设置窗口无边框,setWindowFlags(this->windowFlags() | Qt::FramelessWindowHint);
(2)主窗口设置窗口半透明显示,使用qss设备背景颜色透明无法生效,需要使用setAttribute(Qt::WA_TranslucentBackground);
(3)frame 窗口设置阴影setGraphicsEffect(shadowEffect);否则所有的子窗口都会附带阴影效果
附代码:
this->setWindowFlags(this->windowFlags() | Qt::FramelessWindowHint); //无边框
this->setAttribute(Qt::WA_TranslucentBackground); //背景半透明
this->resize(WidgetWidth, WidgetHeight);
QGraphicsDropShadowEffect *shadowEffect = new QGraphicsDropShadowEffect(this);
shadowEffect->setColor(QColor(127, 127, 127, 127)); // 设置阴影颜色,透明度
shadowEffect->setBlurRadius(20); //设置阴影范围
shadowEffect->setXOffset(0); //设置阴影开始位置
shadowEffect->setYOffset(0);
frame = new QFrame(this);
frame->setGeometry(10, 10, WidgetWidth - 20, WidgetHeight - 20);
frame->setGraphicsEffect(shadowEffect);
frame->setStyleSheet("QFrame{background: #ffffff;}");
效果图: