使用GraphView实现简单的绘图工具
ShapeItem代码:
ShapeItem::ShapeItem(ShapeType type)
{m_type = type;m_lt = QPointF(0, 0);m_rb = QPointF(0, 0);m_deleteEnable = false;m_bll = BllData::getInstance();connect(m_bll, &BllData::deleteShapeEnableSignal, this, &ShapeItem::deleteShapeEnableSlot);this->setFlags(QGraphicsItem::ItemIsSelectable |QGraphicsItem::ItemIsMovable |QGraphicsItem::ItemIsFocusable);
}ShapeItem::~ShapeItem()
{}
QRectF ShapeItem::boundingRect() const {int w = abs(m_lt.x() - m_rb.x());int h = abs(m_lt.y() - m_rb.y());QRectF rect(m_lt.x(), m_lt.y(), w + 2, h + 2);return rect;
}
void ShapeItem::paint(QPainter* painter, const QStyleOptionGraphicsItem* option, QWidget* widget) {Q_UNUSED(option);Q_UNUSED(widget);painter->setPen(QPen(Qt::darkBlue, 1, Qt::SolidLine));int width = abs(m_lt.x() - m_rb.x());int height = abs(m_lt.y() - m_rb.y());QPointF center((m_lt.x() + m_rb.x()) / 2, (m_lt.y() + m_rb.y()) / 2);if (m_type == RectShape) {QRectF rect(m_lt.x(), m_lt.y(), width, height);painter->drawRect(rect);}else if (m_type == SquareShape) {QRectF rect(m_lt.x(), m_lt.y(), width, width);painter->drawRect(rect);}else if (m_type == CircleShape) {QRectF rect(m_lt.x(), m_lt.y(), width, width);painter->drawEllipse(rect);}else if (m_type == EllipseShape) {QRectF rect(m_lt.x(), m_lt.y(), width, height);painter->drawEllipse(rect);}
}
void ShapeItem::setTopLeft(QPointF lt) {m_lt = lt;
}
QPointF ShapeItem::getTopLeft() {return m_lt;
}
void ShapeItem::setBottomRight(QPointF rb) {m_rb = rb;
}
QPointF ShapeItem::getBottomRight() {return m_rb;
}
void ShapeItem::mouseDoubleClickEvent(QGraphicsSceneMouseEvent* event) {qDebug() << "===================mousePressEvent pos: " << event->scenePos();if (m_deleteEnable) {m_deleteEnable = false;m_bll->returnDeletePos(m_lt);}
}
void ShapeItem::mousePressEvent(QGraphicsSceneMouseEvent* event) {qDebug() << "================ShapeItem::mousePressEvent---------pos--" << event->pos();
}
void ShapeItem::mouseMoveEvent(QGraphicsSceneMouseEvent* event) {qDebug() << "=================ShapeItem::mouseMoveEvent-------------";qreal dx = event->scenePos().x() - event->lastScenePos().x();qreal dy = event->scenePos().y() - event->lastScenePos().y();this->moveBy(dx, dy);
}
void ShapeItem::deleteShapeEnableSlot() {m_deleteEnable = true;
}
void ShapeItem::wheelEvent(QGraphicsSceneWheelEvent* event) {static int cnt = 0;cnt++;if (cnt > 1000) {cnt = 0;}if (cnt % 5 == 0) {int d = event->delta();QPointF center((m_lt.x() + m_rb.x()) / 2, (m_lt.y() + m_rb.y()) / 2);int w = abs(m_lt.x() - m_rb.x());int h = abs(m_lt.y() - m_rb.y());if (d > 0) {w = w + 5;h = h + 5;m_lt.setX(center.x() - w / 2);m_lt.setY(center.y() - h / 2);m_rb.setX(center.x() + w / 2);m_rb.setY(center.y() + h / 2);}else {w = w - 5;h = h - 5;w = w > 5 ? w : 5;h = h > 5 ? h : 5;m_lt.setX(center.x() - w / 2);m_lt.setY(center.y() - h / 2);m_rb.setX(center.x() + w / 2);m_rb.setY(center.y() + h / 2);}m_bll->wheelUpdateData();}
}
主UI代码:
GraphViewDemo2::GraphViewDemo2(QWidget *parent): QWidget(parent)
{ui.setupUi(this);m_type = NoShape;m_bll = BllData::getInstance();m_scene = new GraphicsScene;/*connect(m_bll, &BllData::updateRectPosSignal, this, &GraphViewDemo2::updateRectPosSlot);connect(m_bll, &BllData::releaseRectPosSignal, this, &GraphViewDemo2::releaseRectPosSlot);*/connect(m_bll, &BllData::returnDeletePosSignal, this, &GraphViewDemo2::returnDeletePosSlot);connect(m_bll, &BllData::wheelUpdateDataSignal, this, &GraphViewDemo2::wheelUpdateDataSlot);connect(m_bll, &BllData::pressRectPosSignal, this, &GraphViewDemo2::pressRectPosSlot);//ui.graphicsView->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);//ui.graphicsView->setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff); 设置场景范围ui.graphicsView->resize(1440, 1120);//ui.graphicsView->setSceneRect(INT_MIN / 2, INT_MIN / 2, INT_MAX, INT_MAX); 反锯齿//ui.graphicsView->setRenderHints(QPainter::Antialiasing);m_scene->setBackgroundBrush(Qt::gray);ui.graphicsView->setScene(m_scene);
}GraphViewDemo2::~GraphViewDemo2()
{
}
void GraphViewDemo2::pressRectPosSlot(QPointF pos) {int w = 100;int h = 80;ShapeItem* item;if (m_type == RectShape) {int ltx = pos.x() - w / 2;int lty = pos.y() - h / 2;int rbx = pos.x() + w / 2;int rby = pos.y() + h / 2;item = new ShapeItem(RectShape);item->setTopLeft(QPointF(ltx, lty));item->setBottomRight(QPointF(rbx, rby));m_scene->addItem(item);}else if (m_type == SquareShape) {h = 100;int ltx = pos.x() - w / 2;int lty = pos.y() - h / 2;int rbx = pos.x() + w / 2;int rby = pos.y() + h / 2;item = new ShapeItem(SquareShape);item->setTopLeft(QPointF(ltx, lty));item->setBottomRight(QPointF(rbx, rby));m_scene->addItem(item);}else if (m_type == CircleShape) {h = 100;int ltx = pos.x() - w / 2;int lty = pos.y() - h / 2;int rbx = pos.x() + w / 2;int rby = pos.y() + h / 2;item = new ShapeItem(CircleShape);item->setTopLeft(QPointF(ltx, lty));item->setBottomRight(QPointF(rbx, rby));m_scene->addItem(item);}else if (m_type == EllipseShape) {h = 80;int ltx = pos.x() - w / 2;int lty = pos.y() - h / 2;int rbx = pos.x() + w / 2;int rby = pos.y() + h / 2;item = new ShapeItem(EllipseShape);item->setTopLeft(QPointF(ltx, lty));item->setBottomRight(QPointF(rbx, rby));m_scene->addItem(item);}m_type = NoShape;
}
void GraphViewDemo2::returnDeletePosSlot(QPointF pos) {qDebug() << "returnPressPosSlot pos: " << pos;for (int i = 0; i < m_scene->items().size(); i++) {ShapeItem* item = (ShapeItem*)m_scene->items()[i];QPointF lt = item->getTopLeft();if (pos == lt) {qDebug() << "index: " << i;m_scene->removeItem(item);break;}}
}
void GraphViewDemo2::on_deleteBtn_clicked() {m_bll->deleteShapeEnable();
}
void GraphViewDemo2::on_clearBtn_clicked() {m_scene->clear();//ui.graphicsView->setScene(m_scene);
}
void GraphViewDemo2::wheelUpdateDataSlot() {m_scene->update();
}
void GraphViewDemo2::on_rectBtn_clicked() {m_bll->setShapeEnable();m_type = RectShape;
}
void GraphViewDemo2::on_squareBtn_clicked() {m_bll->setShapeEnable();m_type = SquareShape;
}
void GraphViewDemo2::on_circleBtn_clicked() {m_bll->setShapeEnable();m_type = CircleShape;
}
void GraphViewDemo2::on_ellipseBtn_clicked() {m_bll->setShapeEnable();m_type = EllipseShape;
}
Scene代码:
GraphicsScene::GraphicsScene(QObject *parent): QGraphicsScene(parent)
{m_startP.setX(0);m_startP.setY(0);m_endP.setX(0);m_endP.setY(0);m_enable = false;m_bll = BllData::getInstance();connect(m_bll, &BllData::setShapeEnableSignal, this, &GraphicsScene::setShapeEnableSlot);
}GraphicsScene::~GraphicsScene()
{}
void GraphicsScene::mousePressEvent(QGraphicsSceneMouseEvent* event) {if (m_enable) {m_enable = false;m_startP = event->scenePos();m_endP = m_startP;m_bll->pressRectPos(m_startP);}else {//qDebug() << "GraphicsScene::mousePressEvent---------------";//event->ignore();QGraphicsScene::mousePressEvent(event);}}
void GraphicsScene::mouseMoveEvent(QGraphicsSceneMouseEvent* event) {QGraphicsScene::mouseMoveEvent(event);//qDebug() << "m_startp: " << m_startP << " m_endp: " << m_endP;
}
void GraphicsScene::mouseReleaseEvent(QGraphicsSceneMouseEvent* event) {QGraphicsScene::mouseReleaseEvent(event);
}
void GraphicsScene::setShapeEnableSlot() {qDebug() << "setRectEnableSlot---------------";m_enable = true;
}

相关文章:
使用GraphView实现简单的绘图工具
ShapeItem代码: ShapeItem::ShapeItem(ShapeType type) {m_type type;m_lt QPointF(0, 0);m_rb QPointF(0, 0);m_deleteEnable false;m_bll BllData::getInstance();connect(m_bll, &BllData::deleteShapeEnableSignal, this, &ShapeItem::deleteShap…...
javaWebssh教师荣誉库管理系统myeclipse开发mysql数据库MVC模式java编程计算机网页设计
一、源码特点 java ssh在线授课辅导系统是一套完善的web设计系统(系统采用ssh框架进行设计开发),对理解JSP java编程开发语言有帮助,系统具有完整的源代码和数据库,系统主要采用B/S模式开发。开发环境为TOMCAT7.0…...
Android minigbm框架普法
Android minigbm框架普法 引言 假设存在这么一个场景,我的GPU的上层实现走的不是标准的Mesa接口,且GPU也没有提专门配套的gralloc和hwcompoer实现。那么我们的Android要怎么使用到EGL和GLES库呢,并且此GPU驱动是支持drm实现的,也有…...
01、MongoDB -- 下载、安装、配置文件等配置 及 副本集配置
目录 MongoDB -- 下载、安装、配置 及 副本集配置启动命令启动 mongodb 的服务器(单机和副本集)启动单机模式的 mongodb 服务器启动副本集的 3 个副本节点(mongodb 服务器) 启动 mongodb 的客户端 MongoDB 下载MongoDB 安装1、解压…...
uniapp中导入css和scss的区别
在项目中编写了一个基础的公共样式 common.scss文件 想要将其 导入到app.vue文件中 第一次使用的是import url(static/common.scss); 编译直接报错,无法识别这个文件 原因是 使用import url()是CSS中用于导入外部样式表的语法,但它不适用于导入SCS…...
RabbitMQ-TTL/死信队列/延迟队列高级特性
文章目录 TTL死信队列消息成为死信的三种情况队列如何绑定死信交换机 延迟队列RabbitMQ如何实现延迟队列 总结来源B站黑马程序员 TTL TTLTTL(Time To Live):存活时间/过期时间当信息到达存活时间后,还没有被消费,会被自动清除。RabbitMQ可以对消息设置过…...
docker安装php7.4安装(swoole)
容器 docker pull centos:centos7 docker run -dit -p9100:9100 --name“dade” --privilegedtrue centos:centos7 /usr/sbin/init 一、安装前库文件和工具准备 1、首先安装 EPEL 源 yum -y install epel-release2.安装 REMI 源 yum -y install http://rpms.remirepo.net/en…...
身份证识别系统(安卓)
设计内容与要求: 通过手机摄像头捕获身份证信息,将身份证上的姓名、性别、出生年月、身份证号码保存在数据库中。1)所开发Apps软件至少需由3-5个以上功能性界面组成。要求:界面美观整洁、方便应用;可以使用Android原生…...
Python教程——最后一波来喽
提示:文章写完后,目录可以自动生成,如何生成可参考右边的帮助文档 文章目录 1.使用__slots__2. property3.多重继承 4.定制类5.枚举类6.错误处理7.调试8. 文档测试9.单元测试10. 文件读写11. StringIO和BytesIO12. 操作文件和目录13.序列化14…...
学生管理系统(python实现)
新增学生显示学生查找学生删除学生存档到文件 约定好数据的存储格式: 约定把数据保存在和py文件同级目录中,文件名为record.txt 文件内容按照行文本的方式来表示 首先这是一个文本文件,里面包含了很多行,每一行代表一个学生 …...
Java读取文件
读取文件为String 、访问链接直接跳转html 环境:SpringMVC 、前端jsp InputStreamReader FileInputStream fileInputStream new FileInputStream(formatFile.getHtmlpath());InputStreamReader reader new InputStreamReader(fileInputStream, StandardCharsets…...
曾桂华:车载座舱音频体验探究与思考| 演讲嘉宾公布
智能车载音频 I 分论坛将于3月27日同期举办! 我们正站在一个前所未有的科技革新的交汇点上,重塑我们出行体验的变革正在悄然发生。当人工智能的磅礴力量与车载音频相交融,智慧、便捷与未来的探索之旅正式扬帆起航。 在驾驶的旅途中࿰…...
面试题HTML+CSS+网络+浏览器篇
文章目录 Css预处理sass less是什么?为什么使用他们怎么转换 less 为 css?重绘和回流是什么http 是什么?有什么特点HTTP 协议和 HTTPS 区别什么是 CSRF 攻击HTML5 新增的内容有哪些Css3 新增的特性flex VS grid清除浮动的方式有哪些ÿ…...
wordpress外贸独立站
WordPress外贸电商主题 简洁实用的wordpress外贸电商主题,适合做外贸跨境的电商公司官网使用。 https://www.jianzhanpress.com/?p5025 华强北面3C数码WordPress外贸模板 电脑周边、3C数码产品行业的官方网站使用,用WordPress外贸模板快速搭建外贸网…...
[python] 构建数据流水线(pipeline)
Plum 是一个用于构建数据流水线(pipeline)的 Python 库,它旨在简化和优化数据处理流程,使得数据流转和处理变得更加清晰、高效和可维护。下面我将更详细地介绍 Plum 的特点、功能和使用方法。 Plum 的主要特点和功能:…...
计算机网络-网络互连和互联网(五)
1.路由器技术NAT: 网络地址翻译,解决IP短缺,路由器内部和外部地址进行转换。静态地址转换:静态NAT(一对一) 静态NAT,内外一对一转换,用于web服务器,ftp服务器等固定IP的…...
【深度学习】Pytorch基础
张量 运算与操作 加减乘除 pytorch中tensor运算逐元素进行,或者一一对应计算 常用操作 典型维度为N X C X H X W,N为图像张数,C为图像通道数,HW为图高宽。 sum() 一般,指定维度,且keepdimTrue该维度上元…...
C++模拟揭秘刘谦魔术,领略数学的魅力
新的一年又开始了,大家新年好呀~。在这我想问大家一个问题,有没有同学看了联欢晚会上刘谦的魔术呢? 这个节目还挺有意思的,它最出彩的不是魔术本身,而是小尼老师“念错咒语”而导致他手里的排没有拼在一起,…...
JAVA语言编写一个方法,两个Long参数传入,使用BigDecimal类,计算相除四舍五入保留2位小数返回百分数。
在Java中,你可以使用BigDecimal类来执行精确的浮点数计算,并且可以指定结果的小数位数。以下是一个方法,它接受两个Long类型的参数,并使用BigDecimal来计算它们的商,然后将结果四舍五入到两位小数,并返回一…...
SQL教学:掌握MySQL数据操作核心技能--DML语句基本操作之“增删改查“
大家好,今天我要给大家分享的是SQL-DML语句教学。DML,即Data Manipulation Language,也就是我们常说的"增 删 改 查",是SQL语言中用于操作数据库中数据的一部分。作为MySQL新手小白,掌握DML语句对于数据库数…...
内网服务器福音:手把手教你搞定Supervisor 4.0.4离线安装(附CentOS 7.6 + Python 2.7.5环境避坑指南)
内网环境下的Supervisor 4.0.4离线部署全攻略:从依赖解析到避坑实践 在金融、政务等安全敏感领域,生产服务器往往部署在严格隔离的内网环境中。这种架构虽然保障了系统安全性,却给运维工具链的部署带来了独特挑战——无法直接通过pip install…...
链游3.0时代:GameFi+NFT+SocialFi如何引爆万亿级“数字乌托邦“?
——区块链游戏开发的全栈解密与商业落地指南引言:当游戏世界开始"造富" 当Axie Infinity的玩家在菲律宾靠打怪月入过万,当Decentraland的虚拟土地拍出243万美元天价,当StepN的运动鞋NFT创造45天回本神话——链游已不再是加密圈的小…...
如何快速掌握Prism-Samples-Wpf交互性编程:InvokeCommandAction事件驱动开发终极指南
如何快速掌握Prism-Samples-Wpf交互性编程:InvokeCommandAction事件驱动开发终极指南 【免费下载链接】Prism-Samples-Wpf Samples that demonstrate how to use various Prism features with WPF 项目地址: https://gitcode.com/gh_mirrors/pr/Prism-Samples-Wpf…...
论文AI率爆表怕延毕?5招实测降AI率,3分钟知网AIGC过审上岸
2025 年 12 月 25 日知网 AIGC 检测系统升级,2026 年 4 月 27 日维普 AI 率检测平台升级…2026 毕业季,各大主流 AIGC 检测软件陆续升级系统,识别 AI 痕迹更加精准。 临近毕业,同学们看者飘红的 AIGC 检测报告、纷繁复杂的降 AI …...
Unity ASE全屏风沙Shader实战:从光学建模到跨平台优化
1. 这不是“加个粒子就完事”的风沙——为什么全屏风沙在Unity里是个硬骨头“Unity之ASE实现全屏风沙效果”——看到这个标题,很多刚接触Shader Graph或Amplify Shader Editor(ASE)的美术向程序员第一反应是:“不就是叠个噪波UV动…...
Unity Crest海洋系统跨渲染管线适配指南:BIRP/URP/HDRP深度解析
1. 这不是“换个Shader就能跑”的事:Crest海洋系统在现代Unity管线中的真实适配困境Crest海洋系统——这个在Unity生态里被反复提及、被无数海景Demo反复验证的高质量水体解决方案,从诞生之初就带着一个隐性前提:它原生构建于Built-in Render…...
合同系统功能详解:相对方管理
上一期,我们讲解了合同系统的业务功用。本期开始,我们将逐一对合同系统的核心功能进行拆解,结合实际业务场景展开详细讲解。今天,我们重点介绍合同系统中的相对方管理功能。 在实际业务落地过程中,不同企业的经营业态…...
终极指南:免费开源的AMD Ryzen调试神器SMUDebugTool完整使用教程
终极指南:免费开源的AMD Ryzen调试神器SMUDebugTool完整使用教程 【免费下载链接】SMUDebugTool A dedicated tool to help write/read various parameters of Ryzen-based systems, such as manual overclock, SMU, PCI, CPUID, MSR and Power Table. 项目地址: …...
OBS智能背景移除插件:零绿幕实现专业直播效果的完整指南
OBS智能背景移除插件:零绿幕实现专业直播效果的完整指南 【免费下载链接】obs-backgroundremoval An OBS plugin for removing background in portrait images (video), making it easy to replace the background when recording or streaming. 项目地址: https:…...
“--tile”失效了?深度逆向Midjourney纹理无缝拼接底层逻辑(含Python自动化Tile校验脚本)
更多请点击: https://codechina.net 第一章:Midjourney纹理无缝拼接的核心价值与失效现象洞察 在游戏开发、建筑可视化与数字孪生等高频复用表面材质的场景中,Midjourney生成的纹理若能实现像素级无缝拼接(tiling)&am…...
