1 QFrame + QGraphicsDropShadowEffect
QFrame *frame = new QFrame(this);
frame->setStyleSheet("QFrame{background-color: rgb(255, 255, 255);border-radius:10px}"); //设置圆角与背景透明
frame->setGeometry(5, 5, this->width() - 5, this->height() - 5);//设置有效范围框
QGraphicsDropShadowEffect *shadow_effect = new QGraphicsDropShadowEffect(this);
shadow_effect->setOffset(0, 0);
shadow_effect->setColor(Qt::black);
shadow_effect->setBlurRadius(10);
frame->setGraphicsEffect(shadow_effect);
//...
this->setAttribute(Qt::WA_TranslucentBackground);//特别注意这句
如果发现没有效果,那可能你设置了底层布局的问题。因为你可能设置了底层布局setContentsMargins的关系,如是,调整这个函数的参数即可
2 paintEvent
def paintEvent(self, event: QPaintEvent):
painter = QPainter(self)
path = QPainterPath()
path.setFillRule(Qt.WindingFill)
path.addRect(10, 10, self.width()-20, self.height()-20)
painter.setRenderHint(QPainter.Antialiasing)
painter.fillPath(path, QBrush(Qt.white))
color = QColor(0, 0, 0, 50)
for i in range(10):
x = 10-i
path = QPainterPath()
path.setFillRule(Qt.WindingFill)
x1, y1, x2, y2 = x, x, self.width()-x*2, self.height()-x*2
print(x1, y1, x2, y2)
path.addRect(x1, y1, x2, y2)
color.setAlpha(150 - (i**0.5)*50)
painter.setPen(color)
painter.drawPath(path)