Qt自定义控件7:圆圈加载进度条

Qt自定义控件7:圆圈加载进度条

先看效果图:
在这里插入图片描述

思路:画一个占270度的圆弧,然后定义一个定时器,定时旋转坐标系,实现旋转的效果。圆弧需要使用渐变色,实现颜色越来越浅的效果

关键代码:CMProcessBar1.cpp

CMProcessBar1::CMProcessBar1(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::CMProcessBar1)
{
    ui->setupUi(this);
    QTimer *timer = new QTimer;
    connect(timer,QTimer::timeout,this,updaterRotation);// 定时旋转坐标系
    timer->start(3);//定时3毫秒
}

CMProcessBar1::~CMProcessBar1()
{
    delete ui;
}

void CMProcessBar1::updaterRotation(){ //循环360度旋转坐标系
    rotation++;
    if(rotation == 360){
        rotation = 0;
    }
    update();
}

void CMProcessBar1::paintEvent(QPaintEvent *event){//根据QPaintPath画出渐变色的圆弧
    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);

    QConicalGradient gra(QPoint(0,0),0);
    gra.setColorAt(0,QColor("#3BB6FE"));
    gra.setColorAt(1,QColor("#FFFFFF"));
    QBrush brush(gra);

    int radis = 40;
    int sider = 5;
    QRect rect(-radis,-radis,radis*2,radis*2);
    QPainterPath path;
    path.arcTo(rect,0, 270);

    QPainterPath subPath;
    subPath.addEllipse(rect.adjusted(sider, sider, -sider, -sider));

    path = path-subPath;
    painter.setBrush(brush);//QColor("#66CFFF")
    painter.setPen(Qt::NoPen);
    painter.rotate(rotation);
    painter.drawPath(path);


}

  • 1
    点赞
  • 11
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

制造垃圾代码的垃圾coder一个

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值