Qt 自定義窗口實現帶陰影的圓角窗口

Qt 自定義窗口實現帶陰影的圓角窗口

  • 第一步需要在構造函數設置以下幾個屬性:
    this->setWindowFlags(Qt::FramelessWindowHint);/這個window標誌是去掉窗口默認的標題欄。更多標誌可以參考qt自帶的例子 Window Flags Example/
    this>setAttribute(Qt::WA_TranslucentBackground);/設置窗口背景爲透明;setWindowOpacity(qreal level)這是QWidget提供設置窗口透明的一個方法,level爲0.0~1.0之間/
  • 第二步重寫paintEvent(QPaintEvent *event)事件,然後將背景硬生生的畫出來(效果不太理想,但勉強可以使用)代碼如下:
   /* 通過繪畫路徑來畫陰影*/
    QPainterPath path;  
    path.setFillRule(Qt::WindingFill);  
    path.addRoundedRect(10,10, this->width()-20, this->height()-20,6);  

    QPainter painter(this);  
    painter.setRenderHint(QPainter::Antialiasing, true);  
    painter.fillPath(path, QBrush(Qt::white));  
    QColor color(0, 0, 0, 50);  
    for(int i=0; i<10; i++)  
    {  
        QPainterPath path;  
        path.setFillRule(Qt::WindingFill);  
        path.addRoundedRect(10-i, 10-i, this->width()-     (10-i)*2, this->height()-(10-i)*2,6);  
        color.setAlpha(150  - qSqrt(i)*50);  /*顏色逐漸透明*/
        painter.setPen(color);  
        painter.drawPath(path);  
    }
上面已經完成自定義窗口背景顏色和實現圓角,當然還有其他方法,比如通過在窗口各個邊填圖也是可以的,也可以使用qt自帶的QGraphicsDropShadowEffect這個類來實現,下面分別給代碼:
    填圖:
       ![這裏寫圖片描述](http://img.blog.csdn.net/20161128220555369)
   void paintEvent(QPaintEvent *event)

{
QPainter painter(this);
painter.setRenderHint(QPainter::Antialiasing, true);
QPixmap background(“:/background”);

int nLeftWidth = 144;
int nBottomHeight = 24;
int nTopHeight = 67;

QRect left(0, 100, nLeftWidth, 100);
QRect right(background.width() - nLeftWidth, 100, nLeftWidth, 100);
QRect leftTop(0, 0, nLeftWidth, nTopHeight);
QRect rightTop(background.width() - nLeftWidth, 0, nLeftWidth, nTopHeight);
QRect top(150, 0, 150, nTopHeight);
QRect leftBottom(0, background.height() - nBottomHeight, nLeftWidth, nBottomHeight);
QRect rightBottom(background.width() - nLeftWidth, background.height() - nBottomHeight, nLeftWidth, nBottomHeight);
QRect bottom(150, background.height() - nBottomHeight, 100, nBottomHeight);
QRect center(300, 300, 100, 100);

QRect leftRect(0, nTopHeight, nLeftWidth, this->height() - nTopHeight - nBottomHeight);
QRect rightRect(this->width() - nLeftWidth, nTopHeight, nLeftWidth, this->height() - nTopHeight - nBottomHeight);
QRect leftTopRect(0, 0, nLeftWidth, nTopHeight);
QRect rightTopRect(this->width() - nLeftWidth, 0, nLeftWidth, nTopHeight);
QRect topRect(nLeftWidth, 0, this->width() - nLeftWidth*2, nTopHeight);
QRect leftBottomRect(0, this->height() - nBottomHeight, nLeftWidth, nBottomHeight);
QRect righttBottomRect(this->width() - nLeftWidth, this->height() - nBottomHeight, nLeftWidth, nBottomHeight);
QRect bottomRect(nLeftWidth, this->height() - nBottomHeight, this->width() - nLeftWidth*2, nBottomHeight);
QRect centerRect(nLeftWidth, nTopHeight, this->width() - nLeftWidth*2, this->height() - nTopHeight - nBottomHeight);

painter.drawPixmap(topRect, background, top);
painter.drawPixmap(leftRect, background, left);
painter.drawPixmap(rightRect, background, right);
painter.drawPixmap(rightTopRect, background, rightTop);
painter.drawPixmap(leftTopRect, background, leftTop);
painter.drawPixmap(leftBottomRect, background, leftBottom);
painter.drawPixmap(righttBottomRect, background, rightBottom);
painter.drawPixmap(bottomRect, background, bottom);
painter.drawPixmap(centerRect, background, center);

}

QGraphicsDropShadowEffect實現:
這個類主要是來給部件添加陰影效果的,如果使用這個方法需要注意不能直接頂級控件,需要在頂級部件一個佈局(QLayout),然後在佈局裏放一個QFrame或者QWidget,外佈局需要給QFrame或者QWidget留出陰影空間,可以通過調用QLayout的void setContentsMargins(int left, int top, int right, int bottom)方法來設置與頂級控件邊緣的間隔距離。
代碼如下:

     QGraphicsDropShadowEffect *effect = new      QGraphicsDropShadowEffect;
    effect->setBlurRadius(16);
    effect->setXOffset(0);
    effect->setYOffset(6);
    effect->setColor(Qt::white);

   frame = new QFrame(this);/*其他控件往frame放就可以了*/
   QGridLayout *layout = new QGridLayout(this);
   layout->setContentsMargins(16,16,16,16);
   layout->addWidget(frame);
   frame->setGraphicsEffect(effect);

留給自己以後複習使用

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章