代码如下:
#pragma once
#include <QLabel>
class comRotateLabel : public QLabel
{
Q_OBJECT
public:
comRotateLabel(QWidget *parent);
~comRotateLabel();
void SetImage(QImage * image);
void SetImage(QString &imagePath);
void SetRotate(int rotate);
protected:
void paintEvent(QPaintEvent *e);
private:
QImage m_image;
int m_rotate;
};
#include <QPainter>
#include "comRotateLabel.h"
comRotateLabel::comRotateLabel(QWidget *parent)
: QLabel(parent)
, m_rotate(0)
{
}
comRotateLabel::~comRotateLabel()
{
}
void comRotateLabel::SetImage(QImage * image)
{
m_image = *image;
}
void comRotateLabel::SetImage(QString &imagePath)
{
m_image.load(imagePath);
}
void comRotateLabel::SetRotate(int rotate)
{
m_rotate = rotate;
}
void comRotateLabel::paintEvent(QPaintEvent *e)
{
QPainter painter(this);
painter.setPen(Qt::NoPen);
painter.setBrush(Qt::NoBrush);
painter.setRenderHint(QPainter::SmoothPixmapTransform);
painter.translate(this->width() / 2, this->height() / 2); //设置旋转中心
painter.rotate(m_rotate); //旋转
painter.translate(-(this->width() / 2), -(this->height() / 2)); //将原点复位
painter.drawImage(this->rect(), m_image);
QWidget::paintEvent(e);
}
调用方法:
{
m_courseBgLabel = new comRotateLabel(this);
m_courseBgLabel->setFixedSize(458, 458);
m_courseBgLabel->move(200, 200);
m_RotatePicTimer = new QTimer(this);
connect(m_RotatePicTimer, SIGNAL(timeout()), this, SLOT(slotRotatePicTimeout()));
m_RotatePicTimer->start(50);
}
void VPlayer::slotRotatePicTimeout()
{
if (m_imageRotate++ == 360) m_imageRotate = 0;
m_courseBgLabel->SetRotate(m_imageRotate);
m_courseBgLabel->update();
}