setWindowFlags(Qt::FramelessWindowHint | Qt::WindowSystemMenuHint);
setAttribute(Qt::WA_TranslucentBackground);
setMouseTracking(true);
在绘图事件中绘制边框阴影
virtual void paintEvent(QPaintEvent *event) override;
void drawShadow(QPainter *p);
#include
#include
#include
#include
#define FrameWidth (8)
void flwidget::paintEvent(QPaintEvent *event)
{
QWidget::paintEvent(event);
QPainter p(this);
p.setRenderHint(QPainter::Antialiasing, true);
/*--------------------------------------------------
* draw client background
*--------------------------------------------------*/
p.save();
QRect clientRect{FrameWidth,
FrameWidth,
width() - 2 * FrameWidth,
height() - 2 * FrameWidth};
p.fillRect(clientRect,
QBrush(Qt::white));
p.restore();
/*--------------------------------------------------
* draw window shadow
*--------------------------------------------------*/
if(!isMaximized())
drawShadow(&p);
}
void flwidget::drawShadow(QPainter *p)
{
if(FrameWidth == 0){
return;
}
p->save();
QColor color(100, 100, 100, 30);
for (int i = 0; i < FrameWidth; i++)
{
color.setAlpha(static_cast<int>(120 - qSqrt(i) * 40));
p->setPen(color);
p->drawRoundedRect(FrameWidth - i,
FrameWidth - i,
width() - (FrameWidth - i) * 2,
height() - (FrameWidth - i) * 2,
i,
i);
}
p->restore();
}
效果如下图: