1 QFrame + QGraphicsDropShadowEffect

  1. QFrame *frame = new QFrame(this);
  2. frame->setStyleSheet("QFrame{background-color: rgb(255, 255, 255);border-radius:10px}"); //设置圆角与背景透明
  3. frame->setGeometry(5, 5, this->width() - 5, this->height() - 5);//设置有效范围框
  4. QGraphicsDropShadowEffect *shadow_effect = new QGraphicsDropShadowEffect(this);
  5. shadow_effect->setOffset(0, 0);
  6. shadow_effect->setColor(Qt::black);
  7. shadow_effect->setBlurRadius(10);
  8. frame->setGraphicsEffect(shadow_effect);
  9. //...
  10. this->setAttribute(Qt::WA_TranslucentBackground);//特别注意这句

如果发现没有效果,那可能你设置了底层布局的问题。因为你可能设置了底层布局setContentsMargins的关系,如是,调整这个函数的参数即可

2 paintEvent

  1. def paintEvent(self, event: QPaintEvent):
  2. painter = QPainter(self)
  3. path = QPainterPath()
  4. path.setFillRule(Qt.WindingFill)
  5. path.addRect(10, 10, self.width()-20, self.height()-20)
  6. painter.setRenderHint(QPainter.Antialiasing)
  7. painter.fillPath(path, QBrush(Qt.white))
  8. color = QColor(0, 0, 0, 50)
  9. for i in range(10):
  10. x = 10-i
  11. path = QPainterPath()
  12. path.setFillRule(Qt.WindingFill)
  13. x1, y1, x2, y2 = x, x, self.width()-x*2, self.height()-x*2
  14. print(x1, y1, x2, y2)
  15. path.addRect(x1, y1, x2, y2)
  16. color.setAlpha(150 - (i**0.5)*50)
  17. painter.setPen(color)
  18. painter.drawPath(path)

image.png