头文件:
#ifndef CIRCLEWIDGET_H
#define CIRCLEWIDGET_H
#include <QWidget>
class QTimer;
class CircleWidget : public QWidget
{
Q_OBJECT
public:
explicit CircleWidget(QWidget *parent = 0);
void startCircle();
protected:
void paintEvent(QPaintEvent *e);
private slots:
void onTimeout();
private:
QImage m_bg;
QTimer *m_pCircleTimer = Q_NULLPTR;
int m_nCirleCount = 0;
bool m_bCircle = false;
int m_nAngle = 0;
};
#endif // CIRCLEWIDGET_H
cpp文件:
#include "circlewidget.h"
#include <QPainter>
#include <QTimer>
CircleWidget::CircleWidget(QWidget *parent):
QWidget(parent)
{
m_bg = QImage(":/skin/btn_Reconnecting.png");
m_pCircleTimer = new QTimer(this);
m_pCircleTimer->setInterval(50);
connect(m_pCircleTimer, &QTimer::timeout, this, &CircleWidget::onTimeout);
}
void CircleWidget::startCircle()
{
m_pCircleTimer->start();
m_bCircle = true;
}
void CircleWidget::paintEvent(QPaintEvent *e)
{
QPainter painter(this);
painter.setPen(Qt::NoPen);
painter.setBrush(Qt::NoBrush);
painter.setRenderHint(QPainter::SmoothPixmapTransform);
if(m_bCircle)
{
painter.translate(this->width() / 2, this->height() / 2); //设置旋转中心
painter.rotate(m_nAngle); //旋转
painter.translate(-(this->width() / 2), -(this->height() / 2)); //将原点复位
}
painter.drawImage(this->rect(), m_bg);
QWidget::paintEvent(e);
}
void CircleWidget::onTimeout()
{
++m_nCirleCount;
if(m_nAngle == 360)
{
m_nAngle = 0;
}
m_nAngle += 10;
if(m_nCirleCount == 600)
{
m_pCircleTimer->stop();
m_bCircle = false;
m_nCirleCount = 0;
}
update();
}