QT custom control realizes circle loading progress bar

Time:2022-5-7

This example shares the specific code of QT circle loading progress bar for your reference. The specific contents are as follows

Look at the renderings first:

Idea: draw a 270 degree arc, and then define a timer to rotate the coordinate system regularly to achieve the effect of rotation. The arc needs to use gradient color to realize the effect of lighter and lighter color

Key code: 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);//  Timed rotation coordinate system
  timer->start(3);// Timing 3 ms
}

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

Void cmprocessbar1:: updaterrotation() {// circulates the 360 degree rotation coordinate system
  rotation++;
  if(rotation == 360){
    rotation = 0;
  }
  update();
}

Void cmprocessbar1:: paintevent (qpaintevent * event) {// draw a gradient arc according to 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);


}

The above is the whole content of this article. I hope it will be helpful to your study, and I hope you can support developpaer.

Recommended Today

JS generate guid method

JS generate guid method https://blog.csdn.net/Alive_tree/article/details/87942348 Globally unique identification(GUID) is an algorithm generatedBinaryCount Reg128 bitsNumber ofidentifier , GUID is mainly used in networks or systems with multiple nodes and computers. Ideally, any computational geometry computer cluster will not generate two identical guids, and the total number of guids is2^128In theory, it is difficult to make two […]