使用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语句对于数据库数…...
龙虎榜——20250610
上证指数放量收阴线,个股多数下跌,盘中受消息影响大幅波动。 深证指数放量收阴线形成顶分型,指数短线有调整的需求,大概需要一两天。 2025年6月10日龙虎榜行业方向分析 1. 金融科技 代表标的:御银股份、雄帝科技 驱动…...
JavaSec-RCE
简介 RCE(Remote Code Execution),可以分为:命令注入(Command Injection)、代码注入(Code Injection) 代码注入 1.漏洞场景:Groovy代码注入 Groovy是一种基于JVM的动态语言,语法简洁,支持闭包、动态类型和Java互操作性,…...
AI Agent与Agentic AI:原理、应用、挑战与未来展望
文章目录 一、引言二、AI Agent与Agentic AI的兴起2.1 技术契机与生态成熟2.2 Agent的定义与特征2.3 Agent的发展历程 三、AI Agent的核心技术栈解密3.1 感知模块代码示例:使用Python和OpenCV进行图像识别 3.2 认知与决策模块代码示例:使用OpenAI GPT-3进…...
Admin.Net中的消息通信SignalR解释
定义集线器接口 IOnlineUserHub public interface IOnlineUserHub {/// 在线用户列表Task OnlineUserList(OnlineUserList context);/// 强制下线Task ForceOffline(object context);/// 发布站内消息Task PublicNotice(SysNotice context);/// 接收消息Task ReceiveMessage(…...
python/java环境配置
环境变量放一起 python: 1.首先下载Python Python下载地址:Download Python | Python.org downloads ---windows -- 64 2.安装Python 下面两个,然后自定义,全选 可以把前4个选上 3.环境配置 1)搜高级系统设置 2…...
工程地质软件市场:发展现状、趋势与策略建议
一、引言 在工程建设领域,准确把握地质条件是确保项目顺利推进和安全运营的关键。工程地质软件作为处理、分析、模拟和展示工程地质数据的重要工具,正发挥着日益重要的作用。它凭借强大的数据处理能力、三维建模功能、空间分析工具和可视化展示手段&…...
USB Over IP专用硬件的5个特点
USB over IP技术通过将USB协议数据封装在标准TCP/IP网络数据包中,从根本上改变了USB连接。这允许客户端通过局域网或广域网远程访问和控制物理连接到服务器的USB设备(如专用硬件设备),从而消除了直接物理连接的需要。USB over IP的…...
Python Ovito统计金刚石结构数量
大家好,我是小马老师。 本文介绍python ovito方法统计金刚石结构的方法。 Ovito Identify diamond structure命令可以识别和统计金刚石结构,但是无法直接输出结构的变化情况。 本文使用python调用ovito包的方法,可以持续统计各步的金刚石结构,具体代码如下: from ovito…...
(一)单例模式
一、前言 单例模式属于六大创建型模式,即在软件设计过程中,主要关注创建对象的结果,并不关心创建对象的过程及细节。创建型设计模式将类对象的实例化过程进行抽象化接口设计,从而隐藏了类对象的实例是如何被创建的,封装了软件系统使用的具体对象类型。 六大创建型模式包括…...
怎么让Comfyui导出的图像不包含工作流信息,
为了数据安全,让Comfyui导出的图像不包含工作流信息,导出的图像就不会拖到comfyui中加载出来工作流。 ComfyUI的目录下node.py 直接移除 pnginfo(推荐) 在 save_images 方法中,删除或注释掉所有与 metadata …...
