QT-旋转地球

该博客介绍了一个使用Qt库创建的地球旋转演示程序。通过加载地图和网格图片,利用QLabel和QTimer来实现图片的连续旋转。代码中定义了QtRotateMap类,包含了必要的成员变量和方法,如旋转图片的函数以及定时器槽函数,使得地球图像能够以一定的速度持续转动。此外,当窗口大小改变时,图片会相应地缩放以适应窗口尺寸。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >


演示效果

请添加图片描述


一、实例代码如下

代码如下(示例):

#include "QtRotateMap.h"
#include <QLabel>
#include <QTimer>
#include <QPixmap>
struct sRotateMapData
{
	QTimer *pTimer = nullptr;
	QPixmap pixWorld;
	QPixmap pixGrid;

	QLabel *pLabelWorld = nullptr;
	QLabel *pLabelGrid = nullptr;
};

QtRotateMap::QtRotateMap(QWidget *parent)
    : QMainWindow(parent)
	, d_ptr(new sRotateMapData)
{
    ui.setupUi(this);

	// 加载图片
	d_ptr->pixWorld = QPixmap(":/Resource/map.png");
	d_ptr->pixGrid = QPixmap(":/Resource/lbx.png");

	// 创建label对象
	d_ptr->pLabelWorld = new QLabel(this->centralWidget());
	d_ptr->pLabelGrid = new QLabel(this->centralWidget());

	// 设置最小值显示
	this->setMinimumSize(300, 300);
	d_ptr->pLabelWorld->setFixedSize(this->size());
	d_ptr->pLabelGrid->setFixedSize(this->size());

	// 将图片设置到label上
	d_ptr->pixWorld.scaled(d_ptr->pLabelWorld->size(), Qt::KeepAspectRatio);
	d_ptr->pLabelWorld->setScaledContents(true);
	d_ptr->pLabelWorld->setPixmap(d_ptr->pixWorld);

	d_ptr->pixGrid.scaled(d_ptr->pLabelGrid->size(), Qt::KeepAspectRatio);
	d_ptr->pLabelGrid->setScaledContents(true);
	d_ptr->pLabelGrid->setPixmap(d_ptr->pixGrid);

	// 开启定时器
	d_ptr->pTimer = new QTimer(this);
	d_ptr->pTimer->start(70);
	connect(d_ptr->pTimer, SIGNAL(timeout()), this, SLOT(slotOnTimer()));
}

// 进行图片旋转
QPixmap QtRotateMap::rotateImageWithTransform(const QPixmap &pix, int nAngle)
{
	int c = pix.height() / 2;
	QMatrix mt1;
	mt1.translate(c, c);       //先将矩阵移到图片中心
	mt1.rotate(nAngle);        //旋转矩阵
	mt1.translate(-c, -c);     //将矩阵移回

	QPixmap p = pix.transformed(mt1, Qt::SmoothTransformation);
	int w = qMin(pix.width(), pix.height());
	QRect cube(0, 0, w, w);
	cube.moveCenter(QPoint(p.rect().center()));

	p = p.copy(cube);

	return p;
}

// 定时器槽函数
void QtRotateMap::slotOnTimer()
{
	static int nAngle = 0;
	nAngle += 1;
	nAngle %= 360;

	d_ptr->pLabelWorld->setPixmap(rotateImageWithTransform(d_ptr->pixWorld, nAngle));
	d_ptr->pLabelGrid->setPixmap(rotateImageWithTransform(d_ptr->pixGrid, -nAngle));
}

// 窗体改变,图片也跟着缩放
void QtRotateMap::paintEvent(QPaintEvent *event)
{
	d_ptr->pLabelWorld->setFixedSize(this->size());
	d_ptr->pLabelGrid->setFixedSize(this->size());

}t

程序连接下载

连接地址:https://download.youkuaiyun.com/download/u013083044/20621797

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

进击的大海贼

联系博主,为您提供有价值的资源

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值