当前位置: 首页 > news >正文

使用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;
}

在这里插入图片描述

http://www.lryc.cn/news/311460.html

相关文章:

  • javaWebssh教师荣誉库管理系统myeclipse开发mysql数据库MVC模式java编程计算机网页设计
  • Android minigbm框架普法
  • 01、MongoDB -- 下载、安装、配置文件等配置 及 副本集配置
  • uniapp中导入css和scss的区别
  • RabbitMQ-TTL/死信队列/延迟队列高级特性
  • docker安装php7.4安装(swoole)
  • 身份证识别系统(安卓)
  • Python教程——最后一波来喽
  • 学生管理系统(python实现)
  • Java读取文件
  • 曾桂华:车载座舱音频体验探究与思考| 演讲嘉宾公布
  • 面试题HTML+CSS+网络+浏览器篇
  • wordpress外贸独立站
  • [python] 构建数据流水线(pipeline)
  • 计算机网络-网络互连和互联网(五)
  • 【深度学习】Pytorch基础
  • C++模拟揭秘刘谦魔术,领略数学的魅力
  • JAVA语言编写一个方法,两个Long参数传入,使用BigDecimal类,计算相除四舍五入保留2位小数返回百分数。
  • SQL教学:掌握MySQL数据操作核心技能--DML语句基本操作之“增删改查“
  • 【性能测试】Jmeter性能压测-阶梯式/波浪式场景总结(详细)
  • 前端面试 跨域理解
  • JetBrains TeamCity 身份验证绕过漏洞复现(CVE-2024-27198)
  • 设计模式—单例模式
  • Android在后台读取UVC摄像头的帧数据流并推送
  • vue单向数据流介绍
  • OpenMMlab AI实战营第四期培训
  • React轻松开发平台:实现高效、多变的应用开发范本
  • 多域名SSL证书:保护多个网站的安全之选
  • HarmonyOS—HAP唯一性校验逻辑
  • 金三银四,程序员如何备战面试季