实现方式,通过重写paintEvent 进行旋转绘制图片,实现让不同层次的图像进行旋转。
头文件:
#pragma once
#include <QLabel>
#include <QWidget>
class LogoGif : public QLabel
{
Q_OBJECT
public:
LogoGif(QWidget *parent = Q_NULLPTR);
~LogoGif();
protected:
void paintEvent(QPaintEvent *);
private:
QStringList m_filelist;
int m_angle[4];
};
cpp文件
#include "LogoGif.h"
#include "Global.h"
LogoGif::LogoGif(QWidget *parent)
: QLabel(parent)
{
m_filelist.append(":/res/img/QAQ1.png");
m_filelist.append(":/res/img/QAQ2.png");
m_filelist.append(":/res/img/QAQ3.png");
m_filelist.append(":/res/img/QAQ4.png");
m_angle[0] = 0;
m_angle[1] = 0;
m_angle[2] = 0;
m_angle[3] = 0;
QTimer* pTimer = new QTimer;
connect(pTimer, &QTimer::timeout, [=] (){
m_angle[0] += 2;
m_angle[1] -= 1;
m_angle[2] += 3;
m_angle[3] -= 1;
update();
});
pTimer->start(40);
}
LogoGif::~LogoGif()
{
}
void LogoGif::paintEvent(QPaintEvent *)
{
QPainter p(this);
p.setRenderHint(QPainter::Antialiasing, true); //反走样
/* 设定旋转中心点 */
p.translate(width() / 2, height() / 2);
QRect rt = this->geometry();
//多层旋转
int i = 0;
for (auto & v : m_filelist)
{
QPixmap disc(v);
QPixmap dst = disc.scaled(QSize(rt.width(), rt.height()));
/* 旋转的角度 */
p.rotate(m_angle[i]);
/* 画图操作 */
p.drawPixmap(-dst.width()/2, -dst.height() / 2, dst);
p.rotate(-m_angle[i]);
++i;
}
QPixmap disc(":/res/img/QAQ5.png");
QPixmap dst = disc.scaled(QSize(rt.width(), rt.height()));
p.rotate(0);
/* 画图操作 */
p.drawPixmap(-dst.width() / 2, -dst.height() / 2, dst);
}