使用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语句对于数据库数…...
浅谈 React Hooks
React Hooks 是 React 16.8 引入的一组 API,用于在函数组件中使用 state 和其他 React 特性(例如生命周期方法、context 等)。Hooks 通过简洁的函数接口,解决了状态与 UI 的高度解耦,通过函数式编程范式实现更灵活 Rea…...
CentOS下的分布式内存计算Spark环境部署
一、Spark 核心架构与应用场景 1.1 分布式计算引擎的核心优势 Spark 是基于内存的分布式计算框架,相比 MapReduce 具有以下核心优势: 内存计算:数据可常驻内存,迭代计算性能提升 10-100 倍(文档段落:3-79…...
sqlserver 根据指定字符 解析拼接字符串
DECLARE LotNo NVARCHAR(50)A,B,C DECLARE xml XML ( SELECT <x> REPLACE(LotNo, ,, </x><x>) </x> ) DECLARE ErrorCode NVARCHAR(50) -- 提取 XML 中的值 SELECT value x.value(., VARCHAR(MAX))…...
OpenPrompt 和直接对提示词的嵌入向量进行训练有什么区别
OpenPrompt 和直接对提示词的嵌入向量进行训练有什么区别 直接训练提示词嵌入向量的核心区别 您提到的代码: prompt_embedding = initial_embedding.clone().requires_grad_(True) optimizer = torch.optim.Adam([prompt_embedding...
VM虚拟机网络配置(ubuntu24桥接模式):配置静态IP
编辑-虚拟网络编辑器-更改设置 选择桥接模式,然后找到相应的网卡(可以查看自己本机的网络连接) windows连接的网络点击查看属性 编辑虚拟机设置更改网络配置,选择刚才配置的桥接模式 静态ip设置: 我用的ubuntu24桌…...
TMC2226超静音步进电机驱动控制模块
目前已经使用TMC2226量产超过20K,发现在静音方面做的还是很不错。 一、TMC2226管脚定义说明 二、原理图及下载地址 一、TMC2226管脚定义说明 引脚编号类型功能OB11电机线圈 B 输出 1BRB2线圈 B 的检测电阻连接端。将检测电阻靠近该引脚连接到地。使用内部检测电阻时,将此引…...
安宝特案例丨寻医不再长途跋涉?Vuzix再次以AR技术智能驱动远程医疗
加拿大领先科技公司TeleVU基于Vuzix智能眼镜打造远程医疗生态系统,彻底革新患者护理模式。 安宝特合作伙伴TeleVU成立30余年,沉淀医疗技术、计算机科学与人工智能经验,聚焦医疗保健领域,提供AR、AI、IoT解决方案。 该方案使医疗…...
数据库优化实战指南:提升性能的黄金法则
在现代软件系统中,数据库性能直接影响应用的响应速度和用户体验。面对数据量激增、访问压力增大,数据库性能瓶颈经常成为项目痛点。如何科学有效地优化数据库,提升查询效率和系统稳定性,是每位开发与运维人员必备的技能。 本文结…...
智能问数Text2SQL Vanna windows场景验证
架构 Vanna 是一个开源 Python RAG(检索增强生成)框架,用于 SQL 生成和相关功能。 机制 Vanna 的工作过程分为两个简单步骤 - 在您的数据上训练 RAG“模型”,然后提出问题,这些问题将返回 SQL 查询,这些查…...
HarmonyOS Next 弹窗系列教程(3)
HarmonyOS Next 弹窗系列教程(3) 选择器弹窗 (PickerDialog) 介绍 选择器弹窗通常用于在用户进行某些操作(如点击按钮)时显示特定的信息或选项。让用户可以进行选择提供的固定的内容。 以下内容都属于选择器弹窗: …...
