QGraphicsView实现简易地图16『爆炸效果』

前文链接:QGraphicsView实现简易地图15『测量面积』
一种简单的爆炸波扩散效果
动态演示效果:

静态展示图片:

核心代码:

cpp 复制代码
#pragma once
#include "../AbstractGeoItem.h"
#include "DataStruct/GeoData.h"

/*
 * 爆炸扩散效果-静态
 */

class ExplosiveDiffusionItem : public AbstractGeoItem
{
public:
	ExplosiveDiffusionItem(const GeoCoord &geoCoord, int radius, QGraphicsItem *parent = nullptr);
	~ExplosiveDiffusionItem();

	// 更新地理位置
	virtual void updateGeoPos();

protected:
	virtual QRectF boundingRect() const override;
	virtual void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget) override;

	// 设置半径(单位:米)
	void setRadius(int radius);
	// 获取半径(单位:米)
	int radius();

protected:
	int m_radius;	// 半径(单位:米)
	double m_rw;	// 基于指定经纬度处的椭圆横轴半径
	double m_rh;	// 基于指定经纬度处的椭圆竖轴半径
};
cpp 复制代码
#include "ExplosiveDiffusionItem.h"
#include <QRadialGradient>
#include <QPainter>
#include "Utility/MapUtility.h"

ExplosiveDiffusionItem::ExplosiveDiffusionItem(const GeoCoord &geoCoord, int radius, QGraphicsItem *parent /*= nullptr*/) : AbstractGeoItem(parent)
{
	setZValue(300);
	setGeoPos(geoCoord.lon, geoCoord.lat);
	m_radius = radius;
}

ExplosiveDiffusionItem::~ExplosiveDiffusionItem()
{

}

void ExplosiveDiffusionItem::updateGeoPos()
{
	AbstractGeoItem::updateGeoPos();
	prepareGeometryChange();
}

QRectF ExplosiveDiffusionItem::boundingRect() const
{
	return QRectF(-m_rw, -m_rh, 2 * m_rw, 2 * m_rh);
}

void ExplosiveDiffusionItem::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
{
	QRadialGradient gr(0, 0, m_rw, 0, 0);
	gr.setColorAt(0.0, QColor(255, 255, 255, 0));
	gr.setColorAt(0.90, QColor(255, 255, 255, 10));
	gr.setColorAt(0.94, QColor(255, 255, 255, 40));
	gr.setColorAt(0.99, QColor(185, 225, 240, 255));
	gr.setColorAt(1, QColor(185, 225, 240, 128));

	painter->setRenderHint(QPainter::Antialiasing);
	painter->setBrush(gr);
	painter->setPen(Qt::NoPen);
	painter->drawEllipse(boundingRect());
}

void ExplosiveDiffusionItem::setRadius(int radius)
{
	m_radius = radius;

	GeoCoord right = MapUtility::geoCoordByDisAndBearing(m_lon, m_lat, 90, m_radius);
	GeoCoord top = MapUtility::geoCoordByDisAndBearing(m_lon, m_lat, 0, m_radius);

	QPointF rightPos = sceneCoordFromGeoCoord(right.lon, right.lat);
	QPointF topPos = sceneCoordFromGeoCoord(top.lon, top.lat);
	QPointF centerPos = sceneCoordFromGeoCoord(m_lon, m_lat);
	m_rw = rightPos.x() - centerPos.x();
	m_rh = centerPos.y() - topPos.y();

	prepareGeometryChange();
}

int ExplosiveDiffusionItem::radius()
{
	return m_radius;
}

此处提供的动态效果,是利用QPropertyAnimation制作而成,后面文章的动态效果也是采用这种方法,后续将不再展示代码,参考此处即可。

cpp 复制代码
#pragma once
#include "ExplosiveDiffusionItem.h"

/*
 * 爆炸扩散效果-动态扩散
 */

class QPropertyAnimation;

class ExplosiveDiffusionAnimateItem : public QObject, public ExplosiveDiffusionItem
{
	Q_OBJECT
	Q_PROPERTY(int m_radius WRITE setRadius READ radius)

public:
	ExplosiveDiffusionAnimateItem(const GeoCoord &geoCoord, int radius, QGraphicsItem *parent = nullptr);
	~ExplosiveDiffusionAnimateItem();

	void start(int msecs = 1000);

private:
	QPropertyAnimation *m_animation;
};
cpp 复制代码
#include "ExplosiveDiffusionAnimateItem.h"
#include <QPropertyAnimation>

ExplosiveDiffusionAnimateItem::ExplosiveDiffusionAnimateItem(const GeoCoord &geoCoord, int radius, QGraphicsItem *parent /*= nullptr*/)
	: ExplosiveDiffusionItem(geoCoord, radius, parent)
{
	m_animation = new QPropertyAnimation(this, "m_radius", this);
	m_animation->setEasingCurve(QEasingCurve::InOutQuad);
	connect(m_animation, &QAbstractAnimation::finished, this, &ExplosiveDiffusionAnimateItem::finished);
}

ExplosiveDiffusionAnimateItem::~ExplosiveDiffusionAnimateItem()
{

}

void ExplosiveDiffusionAnimateItem::start(int msecs/* = 1000*/)
{
	if (m_animation->state() != QAbstractAnimation::Stopped)
		m_animation->stop();

	m_animation->setDuration(msecs);
	m_animation->setStartValue(0);
	m_animation->setEndValue(m_radius);
	m_animation->start();
}
相关推荐
兆。4 分钟前
掌握 PyQt5:从零开始的桌面应用开发
开发语言·爬虫·python·qt
徒步僧11 小时前
ThingsBoard规则链节点:RPC Call Reply节点详解
qt·microsoft·rpc
可峰科技12 小时前
斗破QT编程入门系列之一:认识Qt:初步使用(四星斗师)
开发语言·qt
我喜欢就喜欢12 小时前
基于qt vs下的视频播放
开发语言·qt·音视频
CP-DD13 小时前
Qt的架构设计
qt
阿_旭13 小时前
基于YOLO11/v10/v8/v5深度学习的维修工具检测识别系统设计与实现【python源码+Pyqt5界面+数据集+训练代码】
人工智能·python·深度学习·qt·ai
Bruce小鬼18 小时前
QT创建按钮篇
开发语言·qt
martian66519 小时前
QT开发:掌握现代UI动画技术:深入解析QML和Qt Quick中的动画效果
开发语言·c++·qt·ui
墨染新瑞21 小时前
两个有趣的小东西(qt和类型转换)
开发语言·网络·qt
Bruce小鬼1 天前
解决MAC安装QT启动项目不显示窗口问题
开发语言·qt·macos