Qt 3D图片旋转预览

(一)、控件介绍

  1. 自定义图片预览
  2. 水平,垂直翻转效果
  3. 自适应屏幕

(二)、效果图

在这里插入图片描述

主要代码

SquareImageViewer::SquareImageViewer(QWidget *parent)
	: QWidget(parent)
{
	m_rotation = 0;
	m_liner = 0;
	m_curIndex = 1;
	m_nextIndex = m_curIndex + 1;
	m_eRotatetype = VERTICAL;

	this->initForm();
}

SquareImageViewer::~SquareImageViewer()
{

}

void SquareImageViewer::initForm()
{
	m_animation = new QPropertyAnimation(this, "");
	m_animation->setDuration(5000);
	m_animation->setStartValue(0.0);
	m_animation->setEndValue(90.0);
	m_animation->setEasingCurve(QEasingCurve::InOutQuint);
	connect(m_animation, &QPropertyAnimation::valueChanged, this, &SquareImageViewer::onAnimateValueChanged);
	connect(m_animation, &QPropertyAnimation::finished, this, &SquareImageViewer::onAnimateFinished);
	m_animation->start();
}

void SquareImageViewer::onAnimateValueChanged(const QVariant &value)
{
	m_rotation = value.toDouble();
	m_liner = m_rotation / 90.0;
	update();
}

void SquareImageViewer::onAnimateFinished()
{
	m_rotation = 90.0;
	m_liner = m_rotation / 90.0;
	update();

	m_eRotatetype = (RotateType_e)(qrand() % 2);
	QTimer::singleShot(1000, this, SLOT(onUpdatePixmap()));
}

void SquareImageViewer::onUpdatePixmap()
{
	m_curIndex++;
	if (m_curIndex > maxIndex){
		m_curIndex = 1;
	}
	m_nextIndex = m_curIndex + 1;
	if (m_nextIndex > maxIndex){
		m_nextIndex = 1;
	}
	m_animation->start();
}

void SquareImageViewer::paintEvent(QPaintEvent *paintEvent)
{
	QSize pixSize(600, 300);
	QSize wSize(this->width() - 60, this->height() - 60);
	QPainter painter(this);
	painter.setRenderHints(QPainter::Antialiasing | QPainter::SmoothPixmapTransform);
	painter.translate(this->rect().center());
	painter.scale(wSize.width() * 1.0 / pixSize.width(), wSize.height() * 1.0 / pixSize.height());

	QPixmap curPixmap(QString(":/FrmSquareImageViewer/Resources/%1.jpg").arg(m_curIndex));
	QPixmap nextPixmap(QString(":/FrmSquareImageViewer/Resources/%1.jpg").arg(m_nextIndex));
	QRectF rectf = this->rect();

	if (m_eRotatetype == HORIZONTAL){
		painter.translate(pixSize.width() / 2.0 - m_liner * pixSize.width(), 0);
		painter.save(); {
			QTransform transform = painter.transform();
			transform.rotate(m_rotation, Qt::YAxis);
			painter.setTransform(transform);
			QRect cliprect(-pixSize.width(), -pixSize.height() / 2.0, pixSize.width(), pixSize.height());
			painter.drawPixmap(cliprect, curPixmap);
		}
		painter.restore();

		painter.save(); {
			QTransform transform = painter.transform();
			transform.rotate(-90 + m_rotation, Qt::YAxis);
			painter.setTransform(transform);
			QRect cliprect(0, -pixSize.height() / 2.0, pixSize.width(), pixSize.height());
			painter.drawPixmap(cliprect, nextPixmap);
		}
		painter.restore();
	}
	else{
		painter.translate(0, -pixSize.height() / 2.0 + m_liner * pixSize.height());
		painter.save(); {
			QTransform transform = painter.transform();
			transform.rotate(-m_rotation, Qt::XAxis);
			painter.setTransform(transform);
			QRect cliprect(-pixSize.width() / 2.0, 0, pixSize.width(), pixSize.height());
			painter.drawPixmap(cliprect, curPixmap);
		}
		painter.restore();

		painter.save(); {
			QTransform transform = painter.transform();
			transform.rotate(90 - m_rotation, Qt::XAxis);
			painter.setTransform(transform);
			QRect cliprect(-pixSize.width() / 2.0, -pixSize.height(), pixSize.width(), pixSize.height());
			painter.drawPixmap(cliprect, nextPixmap);
		}
		painter.restore();
	}
}

交流群

Qt交流大会 853086607
在这里插入图片描述

结尾

不定期上传新作品,解答群中作品相关问题。相关外,能解答则解答。欢迎大家一起探索Qt世界

发布了102 篇原创文章 · 获赞 165 · 访问量 20万+

猜你喜欢

转载自blog.csdn.net/ly305750665/article/details/100024972