Qt之圓角陰影邊框

Qt的主窗體要作出相似WIN7那種圓角陰影邊框,這一直是美工的需求。this

圓角陰影邊框

這裏是有一些門道的,尤爲是,這裏藏着一個很大的祕密。3d

這個祕密是一個QT的至少橫跨3個版本,存在了2年多的BUG。。。blog

https://bugreports.qt-project.org/browse/QTBUG-17548get

 

ok,看完這個BUG的應急解決辦法,而後你再需加上以下代碼例子,就可見效了qt

void MainWindow::paintEvent(QPaintEvent *event)
{
    QPainterPath _qppath;
    _qppath.setFillRule(Qt::WindingFill);
    _qppath.addRect(10, 10, this->width()-20, this->height()-20);

    QPainter _qpainter(this);
    _qpainter.setRenderHint(QPainter::Antialiasing, true);
    _qpainter.fillPath(_qppath, QBrush(Qt::white));

    QColor color(0, 0, 0, 55);
    for(int i=0; i<10; i++)
    {
        QPainterPath _qppath_shadow;
        _qppath_shadow.setFillRule(Qt::WindingFill);
        _qppath_shadow.addRoundRect(10-i, 10-i, this->width()-(10-i)*2, this->height()-(10-i)*2,5);
        color.setAlpha(155 - qSqrt(i)*55);
        _qpainter.setPen(color);
        _qpainter.drawPath(_qppath_shadow);
    }
}it

相關文章
相關標籤/搜索