方案一: 重寫繪圖事件,配合
1 setAttribute(Qt::WA_TranslucentBackground);//設置窗體透明(這個時候主窗體是沒有顏色的(也不可以設置顏色),一般可設置一個比主窗體小一點的widget放在主窗體里,然后設置widget的顏色)
1 void Login::paintEvent(QPaintEvent *) 2 { 3 QPainter painter(this); 4 painter.setRenderHint(QPainter::Antialiasing, true); 5 6 QColor color(Qt::black); 7 for(int i=0; i<=5; i++) 8 { 9 QPainterPath path; 10 path.setFillRule(Qt::WindingFill); 11 path.addRect(5-i, 5-i, this->width()-(5-i)*2, this->height()-(5-i)*2); 12 color.setAlpha(150-qSqrt(i)*50);//通過改變顏色透明度達到漸變效果 13 painter.setPen(color); 14 painter.drawPath(path); 15 } 16 }
方案二: 使用QGraphicsDropShadowEffect,配合setAttribute(Qt::WA_TranslucentBackground)和setWindowFlags(Qt::FramelessWindowHint)使用
(一般可設置一個比主窗體小一點的widget放在主窗體里,然后設置widget的顏色和圓角等)
1 QGraphicsDropShadowEffect *shadow=new QGraphicsDropShadowEffect(this); 2 shadow->setOffset(0); 3 shadow->setBlurRadius(20); 4 shadow->setColor(Qt::red); 5 this->setGraphicsEffect(shadow);