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);

留给自己以后复习使用

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