Qt 通过绘画设置边框阴影

首先widget设置
    setWindowFlags(Qt::FramelessWindowHint);
    setAttribute(Qt::WA_TranslucentBackground, true);

然后重载paintevent
void ShadowMethod::paintEvent(QPaintEvent *event)
{
    QPainterPath path;
     path.setFillRule(Qt::WindingFill);
     path.addRect(10, 10, this->width()-20, this->height()-20);

     QPainter painter(this);
     painter.setRenderHint(QPainter::Antialiasing, true);
     painter.fillPath(path, QBrush(Qt::white));

     QColor color(92,93,95,50);
     int arr[10] = {150,120,80,50,40,30,20,10,5,5};
     for(int i=0; i<10; i++)
     {
         QPainterPath path;
         path.setFillRule(Qt::WindingFill);
         if(i == 5)
             path.addRect(10-i-1, 10-i-1, this->width()-(10-i)*2, this->height()-(10-i)*2);
         else
            path.addRoundedRect(10-i-1, 10-i-1, this->width()-(10-i)*2, this->height()-(10-i)*2,2,2);

         color.setAlpha(arr[i]);
         painter.setPen(color);
         painter.drawPath(path);

     }


}

效果图:

相关推荐
©️2020 CSDN 皮肤主题: 编程工作室 设计师:CSDN官方博客 返回首页