QT自定義旋轉控件

代碼如下:

#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();
}

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章