版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/w5688414/article/details/90072287
今天因为项目需要做了一个旋转的动画,下面是我的代码,有需要的可以参考:
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
#include "rotatewidget.h"
#include<QPropertyAnimation>
namespace Ui {
class MainWindow;
}
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
explicit MainWindow(QWidget *parent = nullptr);
~MainWindow();
private slots:
void on_pushButton_clicked();
private:
Ui::MainWindow *ui;
RotateWidget* mRotateWidget;
QPropertyAnimation* anim1;
QPropertyAnimation* anim2;
};
#endif // MAINWINDOW_H
mainwindow.cpp的代码为:
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include "rotatewidget.h"
#include<QPropertyAnimation>
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
mRotateWidget=new RotateWidget();
mRotateWidget->setPixmap1(QPixmap(":/Resources/trash.png"));
// mRotateWidget->setPixmap2(QPixmap(":/Resources/trash.png"));
mRotateWidget->resize(300,300);
anim1= new QPropertyAnimation (mRotateWidget, "rot1");
// anim2=new QPropertyAnimation(mRotateWidget, "rot2");
anim1->setStartValue(0);
// anim2->setStartValue(0);
anim1->setEndValue(359);
// anim2->setEndValue(-359);
anim1->setLoopCount(-1);
// anim2->setLoopCount(-1);
anim1->setDuration(5000);
// anim2->setDuration(3000);
}
MainWindow::~MainWindow()
{
delete ui;
}
void MainWindow::on_pushButton_clicked()
{
anim1->start();
// anim2->start();
mRotateWidget->show();
}
rotatewidget.h的文件代码为:
#ifndef ROTATEWIDGET_H
#define ROTATEWIDGET_H
#include <QWidget>
class RotateWidget : public QWidget
{
Q_OBJECT
Q_PROPERTY(int rot1 READ rot1 WRITE setRot1);
Q_PROPERTY(int rot2 READ rot2 WRITE setRot2);
public:
explicit RotateWidget(QWidget *parent = nullptr);
void setPixmap1(QPixmap px);
void setPixmap2(QPixmap px);
int rot1();
int rot2();
protected:
void paintEvent(QPaintEvent *);
public slots:
void setRot1(int r1);
void setRot2(int r2);
private:
int m_rot1;
int m_rot2;
QPixmap m_px1, m_px2;
};
#endif // ROTATEWIDGET_H
rotatewidget.cpp的代码为:
#include "rotatewidget.h"
#include<QPainter>
RotateWidget::RotateWidget(QWidget *parent) : QWidget(parent)
{
m_rot1 = 0;
m_rot2 = 0;
}
void RotateWidget::setPixmap1(QPixmap px) {
m_px1 = px;
update();
}
void RotateWidget::setPixmap2(QPixmap px)
{
m_px2 = px;
update();
}
int RotateWidget::rot1()
{
return m_rot1;
}
int RotateWidget::rot2()
{
return m_rot2;
}
void RotateWidget::setRot1(int r1)
{
m_rot1 = r1;
update();
}
void RotateWidget::setRot2(int r2)
{
m_rot2 = r2;
update();
}
void RotateWidget::paintEvent(QPaintEvent *) {
QPainter p(this);
p.setRenderHint(QPainter::Antialiasing);
p.translate(width()/2, height()/2);
if(!m_px1.isNull()) {
p.save();
p.rotate(m_rot1);
QRect r = m_px1.rect();
r.moveCenter(QPoint(0,0));
p.drawPixmap(r, m_px1);
p.restore();
}
if(!m_px2.isNull()) {
p.save();
p.rotate(m_rot2);
QRect r = m_px2.rect();
r.moveCenter(QPoint(0,0));
p.drawPixmap(r, m_px2);
p.restore();
}
}
下载链接:https://download.csdn.net/download/w5688414/11171223
参考文献
[1].rotate an image. https://www.qtcentre.org/threads/53765-rotate-an-image