Qt自定义控件15:圆形加载条

Qt自定义控件15:圆形加载条

先看效果图;
Qt自定义控件15:圆形加载条_第1张图片

思路:以中心为原点画小圆圈,旋转坐标系,在这些小圆圈里有一个的颜色比较重(我的代码中这里的颜色不太明显,可自己设置)

关键代码:

CMProcessBar4::CMProcessBar4(QWidget *parent) : QWidget(parent)
{
     
    timer = new QTimer;
    connect(timer,QTimer::timeout,this,[=](){
     
        rRange+=40;
        if(rRange>=360){
     
            rRange = 0;
        }
        update();
    });
}

void CMProcessBar4::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);

    paintE(&painter);
}

void CMProcessBar4::paintE(QPainter* painter){
     
    int range = 360.0/eCount;
    painter->save();
    painter->rotate(rRange);
    painter->setPen(Qt::NoPen);
    painter->setBrush(Qt::white);
    painter->drawEllipse(QPoint(oERange,0),eRange,eRange);
    painter->setBrush(QColor("#E4E4E4"));
    for(int i = 0;i<eCount-1;i++){
     
        painter->rotate(range);
        painter->drawEllipse(QPoint(oERange,0),eRange,eRange);
    }
    painter->restore();
}

void CMProcessBar4::startR(){
     
    timer->start(100);
}

你可能感兴趣的:(Qt画图(自定义控件),Qt,QPainter,自定义控件,加载条)