Qt自定义控件11:波浪进度条3(美化版)

Qt自定义控件11:波浪进度条3(美化版)

先看效果图:
Qt自定义控件11:波浪进度条3(美化版)_第1张图片

思路:与上一篇思路一摸一样,只是添加了一个外圈的占比圆弧

关键代码:WaterProgressBar2.cpp

void WaterProgressBar2::paintEvent(QPaintEvent *event){
    int width = this->width();
    int height = this->height();
    int side = qMin(width, height);

    QPainter painter(this);
    painter.setRenderHints(QPainter::Antialiasing | QPainter::TextAntialiasing);
    painter.translate(width / 2, height / 2);
    painter.scale(side / 200.0, side / 200.0);


    drawBg(&painter);
    drawWater(&painter);
    drawText(&painter);

}

void WaterProgressBar2::drawWater(QPainter* painter){
    QRect rect(-radius,-radius,2*radius,2*radius);
    int startX = -radius;
    int startY = -radius;
    int endX = radius;
    int endY = radius;
    float height = radius-((value*radius)/50);

    QPainterPath waterPath1;
    waterPath1.moveTo(startX,endY);
    QPainterPath waterPath2;
    waterPath2.moveTo(startX,endY);

    for(int i = startX;i<=endX;i++){
        float y1,y2;

        y1 = sinA*qSin(sinAlph*i+offset)+height;
        y2 = sinA*qSin(sinAlph*i+30+offset)+height;
        if(value == 0){
            y1 = y2 = endY;
        }
        if(value == 100){
            y1 = y2 = 0;
        }
        waterPath1.lineTo(i,y1);
        waterPath2.lineTo(i,y2);
    }
    waterPath1.lineTo(endX,endY);
    waterPath2.lineTo(endX,endY);

    QPainterPath bigPath;
    bigPath.addEllipse(rect);
    waterPath1 = bigPath.intersected(waterPath1);
    waterPath2 = bigPath.intersected(waterPath2);
    painter->save();
    painter->setPen(Qt::NoPen);
    waterColor.setAlpha(180);
    painter->setBrush(waterColor);
    painter->drawPath(waterPath2);
    waterColor.setAlpha(100);
    painter->setBrush(waterColor);
    painter->drawPath(waterPath1);


    painter->restore();
}

void WaterProgressBar2::drawText(QPainter* painter){
    painter->save();
    QString text = QString("%1%").arg(value);
    QFont font = painter->font();
    font.setPixelSize(40);
    painter->setFont(font);
    QPen pen = painter->pen();
    pen.setColor(Qt::white);
    pen.setWidth(4);
    painter->setPen(pen);
    QRect rect(-radius,-radius,radius*2,radius*2);
    painter->drawText(rect,Qt::AlignCenter,text);
    painter->restore();
}

void WaterProgressBar2::drawBg(QPainter* painter){
    painter->save();
    painter->setBrush(QColor(bgColor));
    QRect rect(-radius,-radius,radius*2,radius*2);
    painter->drawEllipse(rect);
    painter->restore();
}

你可能感兴趣的:(Qt画图(自定义控件))