- 首先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);
- }
- }
- 效果图:
- <img src="http://img.blog.csdn.net/20171011165958714?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQveHpwYmxvZw==/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center" alt="">
https://blog.csdn.net/xzpblog/article/details/78205710