16,203
社区成员
发帖
与我相关
我的任务
分享
#ifndef CYCLOPROGRESS_H
#define CYCLOPROGRESS_H
#include <QLabel>
#include <QPixmap>
class CycloProgress : public QLabel
{
Q_OBJECT
public:
explicit CycloProgress(QWidget *parent = 0);
void setMinLoops(int loops = 3);
/**
* @brief startAnimation start the Animation
* @param interval unit is milliseconds
*/
void startAnimation(int interval = 25);
void stopAnimation();
signals:
void runMinLoopsEnough();
protected:
void paintEvent(QPaintEvent *);
void timerEvent(QTimerEvent *event);
private:
int timerID;
int minLoops;
QPixmap m_pixmap;
qreal m_rotation;
};
#endif // CYCLOPROGRESS_H
#include "cycloprogress.h"
#include <QPainter>
#include <QTimerEvent>
#include <QDebug>
CycloProgress::CycloProgress(QWidget *parent) :
QLabel(parent), minLoops(3)
{
m_rotation = 0.0;
timerID = 0;
m_pixmap = QPixmap("://qrc/cyclo.png");
}
void CycloProgress::setMinLoops(int loops)
{
minLoops = loops;
}
void CycloProgress::startAnimation(int interval)
{
if(timerID == 0){
timerID = startTimer(interval);
}
}
void CycloProgress::stopAnimation()
{
killTimer(timerID);
timerID = 0;
}
void CycloProgress::timerEvent(QTimerEvent * event)
{
if(event->timerId() == timerID){
m_rotation += 5;
update();
if(m_rotation > minLoops*360){
emit runMinLoopsEnough();
}
}
}
void CycloProgress::paintEvent(QPaintEvent * /*event*/)
{
QPainter painter(this);
painter.setRenderHint(QPainter::SmoothPixmapTransform);
painter.setRenderHint(QPainter::Antialiasing);
QPixmap leadon_text("://qrc/leadon_e_char.png");
QPointF center(m_pixmap.width() / qreal(2), m_pixmap.height() / qreal(2));
painter.drawPixmap(m_pixmap.width() / qreal(2) -leadon_text.width() / 2 , m_pixmap.height() / qreal(2), leadon_text);
painter.translate(center);
painter.rotate(m_rotation);
painter.translate(-center);
painter.drawPixmap(QPointF(0, 0), m_pixmap);
}