Qt自定义控件10:波浪进度条2

Qt自定义控件10:波浪进度条2

先看效果图:
Qt自定义控件10:波浪进度条2_第1张图片

思路:大体遇上一个相同,不过需要实现圆形,所以需要有个大路径减去画出的波浪封闭的路径会得到当前的效果

关键代码:

void WaterProgressBar1::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);
    drawE(&painter);
    drawWater(&painter);
    drawText(&painter);

}

void WaterProgressBar1::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 WaterProgressBar1::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 WaterProgressBar1::drawBg(QPainter* painter){
    painter->save();
    painter->setPen(Qt::NoPen);
    painter->setBrush(QColor(bgColor));
    QRect rect(-outRadius,-outRadius,outRadius*2,outRadius*2);
    painter->drawEllipse(rect);
    painter->restore();
}

 void WaterProgressBar1::drawE(QPainter* painter){
     painter->save();

     QPainterPath path;
     QPainterPath subpath;

     QRect out(-outERadius,-outERadius,2*outERadius,2*outERadius);
     QRect inn(-innERadius,-innERadius,2*innERadius,2*innERadius);
     float range = (value*1.0/100)*360;

     path.arcTo(out,-range,range);
     subpath.addEllipse(inn);
     path -= subpath;
     painter->setPen(Qt::NoPen);
     painter->setBrush(waterColor);

     painter->drawPath(path);

     painter->restore();
 }

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