在很多时候,我们不需要Qt自带的窗口边框, 我们都会去掉它实现自己想要的窗口边框,但是,去掉边框之后,边缘阴影效果随之不见,总感觉光秃秃的不舒服,所以下面是几个去边框加边缘阴影效果的方法。
第一种 使用Qt的QGraphicsDropShadowEffect类来实现。
#include "widget.h"
#include "ui_widget.h"
#include <QHBoxLayout>
#include <QGraphicsDropShadowEffect>
Widget::Widget(QWidget *parent)
: QWidget(parent)
, ui(new Ui::Widget)
{
ui->setupUi(this);
resize(400, 300);
setWindowFlags(windowFlags() | Qt::FramelessWindowHint); // 其中windowFlags()函数返回当前窗口的标志
setAttribute(Qt::WA_TranslucentBackground); // 设置窗口背景透明,其中Qt::WA_TranslucentBackground 是表示窗口背景透明的一个属性。
QWidget *widget = new QWidget(this);
widget->setStyleSheet("background-color: white");
QHBoxLayout *pLayout = new QHBoxLayout(this);
pLayout->addWidget(widget);
pLayout->setContentsMargins(20, 20, 20, 20);
QGraphicsDropShadowEffect *shadow = new QGraphicsDropShadowEffect(widget);
shadow->setOffset(0, 0);
shadow->setColor(QColor(QStringLiteral("black")));
shadow->setBlurRadius(30);
widget->setGraphicsEffect(shadow);
}
第二种 使用Qt的QGraphicsDropShadowEffect类来实现。
还有几种方式,现在不想写了,以后再写。