栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > C/C++/C#

Qt自定义控件实现圆圈加载进度条

C/C++/C# 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

Qt自定义控件实现圆圈加载进度条

本文实例为大家分享了Qt实现圆圈加载进度条的具体代码,供大家参考,具体内容如下

先看效果图:

思路:画一个占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);


}

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持考高分网。

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/62167.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号