《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中构建终端应用界面利器Blessed模块的使用

《Python中构建终端应用界面利器Blessed模块的使用》Blessed库作为一个轻量级且功能强大的解决方案,开始在开发者中赢得口碑,今天,我们就一起来探索一下它是如何让终端UI开发变得轻松而高... 目录一、安装与配置:简单、快速、无障碍二、基本功能:从彩色文本到动态交互1. 显示基本内容2. 创建链

基于Qt开发一个简单的OFD阅读器

《基于Qt开发一个简单的OFD阅读器》这篇文章主要为大家详细介绍了如何使用Qt框架开发一个功能强大且性能优异的OFD阅读器,文中的示例代码讲解详细,有需要的小伙伴可以参考一下... 目录摘要引言一、OFD文件格式解析二、文档结构解析三、页面渲染四、用户交互五、性能优化六、示例代码七、未来发展方向八、结论摘要

Python pyinstaller实现图形化打包工具

《Pythonpyinstaller实现图形化打包工具》:本文主要介绍一个使用PythonPYQT5制作的关于pyinstaller打包工具,代替传统的cmd黑窗口模式打包页面,实现更快捷方便的... 目录1.简介2.运行效果3.相关源码1.简介一个使用python PYQT5制作的关于pyinstall

使用Python制作一个PDF批量加密工具

《使用Python制作一个PDF批量加密工具》PDF批量加密‌是一种保护PDF文件安全性的方法,通过为多个PDF文件设置相同的密码,防止未经授权的用户访问这些文件,下面我们来看看如何使用Python制... 目录1.简介2.运行效果3.相关源码1.简介一个python写的PDF批量加密工具。PDF批量加密

python与QT联合的详细步骤记录

《python与QT联合的详细步骤记录》:本文主要介绍python与QT联合的详细步骤,文章还展示了如何在Python中调用QT的.ui文件来实现GUI界面,并介绍了多窗口的应用,文中通过代码介绍... 目录一、文章简介二、安装pyqt5三、GUI页面设计四、python的使用python文件创建pytho

QT实现TCP客户端自动连接

《QT实现TCP客户端自动连接》这篇文章主要为大家详细介绍了QT中一个TCP客户端自动连接的测试模型,文中的示例代码讲解详细,感兴趣的小伙伴可以跟随小编一起学习一下... 目录版本 1:没有取消按钮 测试效果测试代码版本 2:有取消按钮测试效果测试代码版本 1:没有取消按钮 测试效果缺陷:无法手动停

使用Java编写一个文件批量重命名工具

《使用Java编写一个文件批量重命名工具》这篇文章主要为大家详细介绍了如何使用Java编写一个文件批量重命名工具,文中的示例代码讲解详细,感兴趣的小伙伴可以跟随小编一起学习一下... 目录背景处理1. 文件夹检查与遍历2. 批量重命名3. 输出配置代码片段完整代码背景在开发移动应用时,UI设计通常会提供不

基于Qt实现系统主题感知功能

《基于Qt实现系统主题感知功能》在现代桌面应用程序开发中,系统主题感知是一项重要的功能,它使得应用程序能够根据用户的系统主题设置(如深色模式或浅色模式)自动调整其外观,Qt作为一个跨平台的C++图形用... 目录【正文开始】一、使用效果二、系统主题感知助手类(SystemThemeHelper)三、实现细节

Qt实现文件的压缩和解压缩操作

《Qt实现文件的压缩和解压缩操作》这篇文章主要为大家详细介绍了如何使用Qt库中的QZipReader和QZipWriter实现文件的压缩和解压缩功能,文中的示例代码简洁易懂,需要的可以参考一下... 目录一、实现方式二、具体步骤1、在.pro文件中添加模块gui-private2、通过QObject方式创建

Python按条件批量删除TXT文件行工具

《Python按条件批量删除TXT文件行工具》这篇文章主要为大家详细介绍了Python如何实现按条件批量删除TXT文件中行的工具,文中的示例代码讲解详细,感兴趣的小伙伴可以跟随小编一起学习一下... 目录1.简介2.运行效果3.相关源码1.简介一个由python编写android的可根据TXT文件按条件批