使用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语句对于数据库数…...
Python爬虫实战:研究MechanicalSoup库相关技术
一、MechanicalSoup 库概述 1.1 库简介 MechanicalSoup 是一个 Python 库,专为自动化交互网站而设计。它结合了 requests 的 HTTP 请求能力和 BeautifulSoup 的 HTML 解析能力,提供了直观的 API,让我们可以像人类用户一样浏览网页、填写表单和提交请求。 1.2 主要功能特点…...
接口测试中缓存处理策略
在接口测试中,缓存处理策略是一个关键环节,直接影响测试结果的准确性和可靠性。合理的缓存处理策略能够确保测试环境的一致性,避免因缓存数据导致的测试偏差。以下是接口测试中常见的缓存处理策略及其详细说明: 一、缓存处理的核…...
Nuxt.js 中的路由配置详解
Nuxt.js 通过其内置的路由系统简化了应用的路由配置,使得开发者可以轻松地管理页面导航和 URL 结构。路由配置主要涉及页面组件的组织、动态路由的设置以及路由元信息的配置。 自动路由生成 Nuxt.js 会根据 pages 目录下的文件结构自动生成路由配置。每个文件都会对…...
dify打造数据可视化图表
一、概述 在日常工作和学习中,我们经常需要和数据打交道。无论是分析报告、项目展示,还是简单的数据洞察,一个清晰直观的图表,往往能胜过千言万语。 一款能让数据可视化变得超级简单的 MCP Server,由蚂蚁集团 AntV 团队…...
根目录0xa0属性对应的Ntfs!_SCB中的FileObject是什么时候被建立的----NTFS源代码分析--重要
根目录0xa0属性对应的Ntfs!_SCB中的FileObject是什么时候被建立的 第一部分: 0: kd> g Breakpoint 9 hit Ntfs!ReadIndexBuffer: f7173886 55 push ebp 0: kd> kc # 00 Ntfs!ReadIndexBuffer 01 Ntfs!FindFirstIndexEntry 02 Ntfs!NtfsUpda…...
uniapp 集成腾讯云 IM 富媒体消息(地理位置/文件)
UniApp 集成腾讯云 IM 富媒体消息全攻略(地理位置/文件) 一、功能实现原理 腾讯云 IM 通过 消息扩展机制 支持富媒体类型,核心实现方式: 标准消息类型:直接使用 SDK 内置类型(文件、图片等)自…...
如何配置一个sql server使得其它用户可以通过excel odbc获取数据
要让其他用户通过 Excel 使用 ODBC 连接到 SQL Server 获取数据,你需要完成以下配置步骤: ✅ 一、在 SQL Server 端配置(服务器设置) 1. 启用 TCP/IP 协议 打开 “SQL Server 配置管理器”。导航到:SQL Server 网络配…...
字符串哈希+KMP
P10468 兔子与兔子 #include<bits/stdc.h> using namespace std; typedef unsigned long long ull; const int N 1000010; ull a[N], pw[N]; int n; ull gethash(int l, int r){return a[r] - a[l - 1] * pw[r - l 1]; } signed main(){ios::sync_with_stdio(false), …...
qt+vs Generated File下的moc_和ui_文件丢失导致 error LNK2001
qt 5.9.7 vs2013 qt add-in 2.3.2 起因是添加一个新的控件类,直接把源文件拖进VS的项目里,然后VS卡住十秒,然后编译就报一堆 error LNK2001 一看项目的Generated Files下的moc_和ui_文件丢失了一部分,导致编译的时候找不到了。因…...
CSS 工具对比:UnoCSS vs Tailwind CSS,谁是你的菜?
在现代前端开发中,Utility-First (功能优先) CSS 框架已经成为主流。其中,Tailwind CSS 无疑是市场的领导者和标杆。然而,一个名为 UnoCSS 的新星正以其惊人的性能和极致的灵活性迅速崛起。 这篇文章将深入探讨这两款工具的核心理念、技术差…...
