《QT实用小工具·四十五》可以在界面上游泳的小鱼

2024-04-29 12:12

本文主要是介绍《QT实用小工具·四十五》可以在界面上游泳的小鱼,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

1、概述
源码放在文章末尾

该项目实现了灵动的小鱼,可以在界面上跟随鼠标点击自由的游泳,项目demo演示如下所示:
在这里插入图片描述

项目部分代码如下所示:

#include "magicfish.h"
#include <QtMath>
#include <QPainter>
#include <QPainterPath>
#include <QVariantAnimation>MagicFish::MagicFish(QQuickPaintedItem *parent): QQuickPaintedItem(parent),m_fishRadius(30),m_finLen(30 * 1.3),m_bodyHeight(30 * 3.2),m_headAlpha(200),m_bodyAlpha(225),M_finAlpha(120),m_mainAngle(0.0),m_curValue(0),m_wave(1.0),m_startFin(false),m_paintPoint(false)
{m_animation = new QVariantAnimation(this);m_animation->setDuration(180 * 1000);m_animation->setStartValue(0);m_animation->setEndValue(54000);m_animation->setLoopCount(-1);connect(m_animation, &QVariantAnimation::valueChanged, this, [this](const QVariant &value){m_curValue = value.toInt();update();});connect(this, &QQuickItem::widthChanged, this, &MagicFish::resize);connect(this, &QQuickItem::heightChanged, this, &MagicFish::resize);m_animation->start();
}void MagicFish::paint(QPainter *painter)
{painter->setRenderHint(QPainter::Antialiasing);QPointF middle_pos = QPointF(width() / 2, height() / 2);m_headPos = calcPoint(middle_pos, m_bodyHeight / 2.0, m_mainAngle);paintMyPoint(painter, m_headPos);paintMyPoint(painter, middle_pos);painter->setPen(Qt::NoPen);painter->setBrush(QBrush(QColor(20, 203, 232, 50)));painter->setBrush(QBrush(QColor(244, 92, 71, m_headAlpha)));painter->drawEllipse(m_headPos, m_fishRadius, m_fishRadius);qreal angle = m_mainAngle + qSin(qDegreesToRadians(m_curValue * 1.2 * m_wave)) * 2;QPointF end_pos = calcPoint(m_headPos, m_bodyHeight, angle - 180);QPointF pos1 = calcPoint(m_headPos, m_fishRadius, angle - 80);QPointF pos2 = calcPoint(end_pos, m_fishRadius * 0.7, angle - 90);QPointF pos3 = calcPoint(end_pos, m_fishRadius * 0.7, angle + 90);QPointF pos4 = calcPoint(m_headPos, m_fishRadius, angle + 80);QPointF central_left = calcPoint(m_headPos, m_bodyHeight * 0.56, angle - 130);QPointF central_right = calcPoint(m_headPos, m_bodyHeight * 0.56, angle + 130);QPainterPath path;path.moveTo(pos1);path.quadTo(central_left, pos2);path.lineTo(pos3);path.quadTo(central_right, pos4);path.lineTo(pos1);painter->setBrush(QBrush(QColor(244, 92, 71, m_bodyAlpha)));painter->drawPath(path);paintMyBody(painter, end_pos, m_fishRadius * 0.7, 0.6, angle);QPointF left_fin_pos = calcPoint(m_headPos, m_fishRadius * 0.9, angle + 110);paintMyFishFins(painter, left_fin_pos, true, angle);QPointF right_fin_pos = calcPoint(m_headPos, m_fishRadius * 0.9, angle - 110);paintMyFishFins(painter, right_fin_pos, false, angle);
}void MagicFish::resize()
{m_fishRadius = qMin(width(), height()) / 10.0;m_finLen = m_fishRadius * 1.3;m_bodyHeight = m_fishRadius * 3.2;
}QPointF MagicFish::calcPoint(const QPointF &pos, qreal length, qreal angle)
{qreal delta_x = qCos(qDegreesToRadians(angle)) * length;qreal delta_y = qSin(qDegreesToRadians(angle - 180)) * length;return QPointF(pos + QPointF(delta_x, delta_y));
}void MagicFish::paintMyPoint(QPainter *painter, const QPointF pos)
{if(m_paintPoint){painter->save();painter->setPen(QPen(Qt::black, 3));painter->setBrush(QBrush(Qt::black));painter->drawPoint(pos);painter->restore();}
}void MagicFish::paintMyFishFins(QPainter *painter, const QPointF &pos, bool is_left, qreal father_angle)
{qreal contral_angle = 115;qreal fin_angle = m_startFin ? qSin(qDegreesToRadians(m_curValue * 16.1 * m_wave)) * 12.0 : 2;QPainterPath path;path.moveTo(pos);QPointF end_pos = calcPoint(pos, m_finLen, is_left ? father_angle + fin_angle + 180 :father_angle - fin_angle - 180);QPointF control_pos = calcPoint(pos, m_finLen * 1.8, is_left ?father_angle + contral_angle + fin_angle :father_angle - contral_angle - fin_angle);path.quadTo(control_pos, end_pos);path.lineTo(pos);painter->save();painter->setBrush(QBrush(QColor(244, 92, 71, M_finAlpha)));painter->drawPath(path);painter->restore();
}void MagicFish::paintMyBody(QPainter *painter, const QPointF &pos, qreal seg_r, qreal MP, qreal father_angle)
{qreal angle = father_angle + qCos(qDegreesToRadians(m_curValue * 1.5 * m_wave)) * 15;qreal length = seg_r * (MP + 1);QPointF end_pos = calcPoint(pos, length, angle - 180);QPointF pos1 = calcPoint(pos, seg_r, angle - 90);QPointF pos2 = calcPoint(end_pos, seg_r * MP, angle - 90);QPointF pos3 = calcPoint(end_pos, seg_r * MP, angle + 90);QPointF pos4 = calcPoint(pos, seg_r, angle + 90);painter->save();painter->setBrush(QBrush(QColor(244, 92, 71, m_headAlpha)));painter->drawEllipse(pos, seg_r, seg_r);painter->drawEllipse(end_pos, seg_r * MP, seg_r * MP);QPainterPath path;path.moveTo(pos1);path.lineTo(pos2);path.lineTo(pos3);path.lineTo(pos4);painter->drawPath(path);painter->restore();paintMyBody2(painter, end_pos, seg_r * 0.6, 0.4, angle);
}void MagicFish::paintMyBody2(QPainter *painter, const QPointF &pos, qreal seg_r, qreal MP, qreal father_angle)
{qreal angle = father_angle + qSin(qDegreesToRadians(m_curValue * 1.5 * m_wave)) * 35;qreal length = seg_r * (MP + 2.7);QPointF end_pos = calcPoint(pos, length, angle - 180);QPointF pos1 = calcPoint(pos, seg_r, angle - 90);QPointF pos2 = calcPoint(end_pos, seg_r * MP, angle - 90);QPointF pos3 = calcPoint(end_pos, seg_r * MP, angle + 90);QPointF pos4 = calcPoint(pos, seg_r, angle + 90);paintMyTail(painter, pos, length, seg_r, angle);painter->save();painter->setBrush(QBrush(QColor(244, 92, 71, m_headAlpha)));painter->drawEllipse(end_pos, seg_r * MP, seg_r * MP);QPainterPath path;path.moveTo(pos1);path.lineTo(pos2);path.lineTo(pos3);path.lineTo(pos4);painter->drawPath(path);painter->restore();
}void MagicFish::paintMyTail(QPainter *painter, const QPointF &pos, qreal length, qreal max_w, qreal angle)
{qreal w = qAbs(qSin(qDegreesToRadians(m_curValue * 1.7 * m_wave)) * max_w + m_fishRadius / 5.0 * 3.0);QPointF end_point1 = calcPoint(pos, length, angle - 180);QPointF end_point2 = calcPoint(pos, length - 10, angle - 180);QPointF pos1 = calcPoint(end_point1, w, angle - 90);QPointF pos2 = calcPoint(end_point1, w, angle + 90);QPointF pos3 = calcPoint(end_point2, w - m_fishRadius / 1.5, angle - 90);QPointF pos4 = calcPoint(end_point2, w - m_fishRadius / 1.5, angle + 90);QPainterPath path;path.moveTo(pos);path.lineTo(pos3);path.lineTo(pos4);path.lineTo(pos);painter->save();painter->setBrush(QBrush(QColor(244, 92, 71,     m_headAlpha)));painter->drawPath(path);path.closeSubpath();path.moveTo(pos);path.lineTo(pos1);path.lineTo(pos2);path.lineTo(pos);painter->drawPath(path);painter->restore();
}void MagicFish::setWave(qreal value)
{m_wave = value;
}qreal MagicFish::getFishR() const
{return m_fishRadius;
}qreal MagicFish::getAngle()
{return m_mainAngle;
}QRectF MagicFish::geometry() const
{return QRectF(x(), y(), width(), height());
}QPointF MagicFish::getHeadPos() const
{return m_headPos;
}void MagicFish::setCurrentAngle(qreal angle)
{m_mainAngle = angle;update();
}void MagicFish::setFinAnimation(bool start)
{m_startFin = start;
}void MagicFish::setFishR(int value)
{m_fishRadius = value;m_finLen = value * 1.3;m_bodyHeight = value * 3.2;update();
}

源码下载

这篇关于《QT实用小工具·四十五》可以在界面上游泳的小鱼的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/946054

相关文章

Python基于wxPython和FFmpeg开发一个视频标签工具

《Python基于wxPython和FFmpeg开发一个视频标签工具》在当今数字媒体时代,视频内容的管理和标记变得越来越重要,无论是研究人员需要对实验视频进行时间点标记,还是个人用户希望对家庭视频进行... 目录引言1. 应用概述2. 技术栈分析2.1 核心库和模块2.2 wxpython作为GUI选择的优

macOS无效Launchpad图标轻松删除的4 种实用方法

《macOS无效Launchpad图标轻松删除的4种实用方法》mac中不在appstore上下载的应用经常在删除后它的图标还残留在launchpad中,并且长按图标也不会出现删除符号,下面解决这个问... 在 MACOS 上,Launchpad(也就是「启动台」)是一个便捷的 App 启动工具。但有时候,应

使用Java实现通用树形结构构建工具类

《使用Java实现通用树形结构构建工具类》这篇文章主要为大家详细介绍了如何使用Java实现通用树形结构构建工具类,文中的示例代码讲解详细,感兴趣的小伙伴可以跟随小编一起学习一下... 目录完整代码一、设计思想与核心功能二、核心实现原理1. 数据结构准备阶段2. 循环依赖检测算法3. 树形结构构建4. 搜索子

利用Python开发Markdown表格结构转换为Excel工具

《利用Python开发Markdown表格结构转换为Excel工具》在数据管理和文档编写过程中,我们经常使用Markdown来记录表格数据,但它没有Excel使用方便,所以本文将使用Python编写一... 目录1.完整代码2. 项目概述3. 代码解析3.1 依赖库3.2 GUI 设计3.3 解析 Mark

利用Go语言开发文件操作工具轻松处理所有文件

《利用Go语言开发文件操作工具轻松处理所有文件》在后端开发中,文件操作是一个非常常见但又容易出错的场景,本文小编要向大家介绍一个强大的Go语言文件操作工具库,它能帮你轻松处理各种文件操作场景... 目录为什么需要这个工具?核心功能详解1. 文件/目录存javascript在性检查2. 批量创建目录3. 文件

jvm调优常用命令行工具详解

《jvm调优常用命令行工具详解》:本文主要介绍jvm调优常用命令行工具的用法,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录一 jinfo命令查看参数1.1 查看jvm参数二 jstack命令2.1 查看现场堆栈信息三 jstat 实时查看堆内存,gc情况3.1

MySQL使用binlog2sql工具实现在线恢复数据功能

《MySQL使用binlog2sql工具实现在线恢复数据功能》binlog2sql是大众点评开源的一款用于解析MySQLbinlog的工具,根据不同选项,可以得到原始SQL、回滚SQL等,下面我们就来... 目录背景目标步骤准备工作恢复数据结果验证结论背景生产数据库执行 SQL 脚本,一般会经过正规的审批

基于Python开发批量提取Excel图片的小工具

《基于Python开发批量提取Excel图片的小工具》这篇文章主要为大家详细介绍了如何使用Python中的openpyxl库开发一个小工具,可以实现批量提取Excel图片,有需要的小伙伴可以参考一下... 目前有一个需求,就是批量读取当前目录下所有文件夹里的Excel文件,去获取出Excel文件中的图片,并

Qt 中 isHidden 和 isVisible 的区别与使用小结

《Qt中isHidden和isVisible的区别与使用小结》Qt中的isHidden()和isVisible()方法都用于查询组件显示或隐藏状态,然而,它们有很大的区别,了解它们对于正确操... 目录1. 基础概念2. 区别清见3. 实际案例4. 注意事项5. 总结1. 基础概念Qt 中的 isHidd

Java导入、导出excel用法步骤保姆级教程(附封装好的工具类)

《Java导入、导出excel用法步骤保姆级教程(附封装好的工具类)》:本文主要介绍Java导入、导出excel的相关资料,讲解了使用Java和ApachePOI库将数据导出为Excel文件,包括... 目录前言一、引入Apache POI依赖二、用法&步骤2.1 创建Excel的元素2.3 样式和字体2.