qt实现方框调整

效果

在四周调整
在这里插入图片描述

代码

#ifndef MAINWINDOW_H
#define MAINWINDOW_H#include <QWidget>class MainWindow : public QWidget
{Q_OBJECT
public:explicit MainWindow(QWidget *parent = 0);~MainWindow();void paintEvent(QPaintEvent *event);void updateRect();void resizeEvent(QResizeEvent *event);void mousePressEvent(QMouseEvent *event);void mouseReleaseEvent(QMouseEvent *event);void mouseMoveEvent(QMouseEvent *event);
private:bool isValid(QRect &rect);bool isOutOfRange(QRect &rect);void setSizeCursor(QMouseEvent *event);
private:bool m_bPress{false};bool m_bSetTop{false};bool m_bSetLeft{false};bool m_bSetRight{false};bool m_bSetBottom{false};bool m_bSetTopRight{false};bool m_bSetBottomLeft{false};bool m_bSetTopLeft{false};bool m_bSetBottomRight{false};bool m_bMinSize{true};int  m_minWidth{0};int  m_minHeight{0};QRect m_rect;QRect m_top;QRect m_bottom;QRect m_left;QRect m_right;QRect m_topLeft;QRect m_topRight;QRect m_bottomLeft;QRect m_bottomRight;QPoint m_pressPoint;
};#endif // MAINWINDOW_H
#include "mainwindow.h"
#include <QPainter>
#include <QMouseEvent>
#include <QDebug>MainWindow::MainWindow(QWidget *parent) :QWidget(parent)
{this->setMouseTracking(true);m_rect = QRect(90,80,100,100);updateRect();
}MainWindow::~MainWindow()
{}void MainWindow::paintEvent(QPaintEvent *)
{QPainter painter{this};QPen pen;pen.setStyle(Qt::DashLine);pen.setWidthF(0.5);//pen.setColor(Qt::black);pen.setColor(Qt::red);painter.setPen(pen);painter.drawRect(m_rect);pen.setStyle(Qt::SolidLine);pen.setWidthF(0.8);painter.setPen(pen);painter.setBrush(Qt::white);painter.setRenderHints(QPainter::SmoothPixmapTransform|QPainter::Antialiasing|QPainter::HighQualityAntialiasing);painter.drawEllipse(m_topLeft);painter.drawEllipse(m_topRight);painter.drawEllipse(m_bottomLeft);painter.drawEllipse(m_bottomRight);painter.drawEllipse(m_top);painter.drawEllipse(m_bottom);painter.drawEllipse(m_left);painter.drawEllipse(m_right);
}void MainWindow::updateRect()
{int width = 3;int offset = 4;m_topLeft= QRect(m_rect.topLeft(),QSize(width,width));m_topLeft = m_topLeft.adjusted(-offset,-offset,offset,offset);m_topRight = QRect(m_rect.topRight(),QSize(width,width));m_topRight = m_topRight.adjusted(-offset,-offset,offset,offset);m_bottomLeft = QRect(m_rect.bottomLeft(),QSize(width,width));m_bottomLeft = m_bottomLeft.adjusted(-offset,-offset,offset,offset);m_bottomRight= QRect(m_rect.bottomRight(),QSize(width,width));m_bottomRight = m_bottomRight.adjusted(-offset,-offset,offset,offset);m_top = QRect((m_topLeft.x()+m_topRight.x())/2,m_topLeft.y(),m_topLeft.width(),m_topLeft.height() );m_bottom = QRect((m_bottomLeft.x()+m_bottomRight.x())/2,m_bottomRight.y(),m_bottomRight.width(),m_bottomRight.height() );m_left = QRect(m_topLeft.x(),(m_topLeft.y()+m_bottomLeft.y())/2,m_bottomLeft.width(),m_bottomLeft.height() );m_right = QRect(m_topRight.x(),(m_topRight.y()+m_bottomRight.y())/2,m_topRight.width(),m_topRight.height() );m_minWidth = m_topLeft.width()+m_top.width()+m_topRight.width();m_minHeight = m_minWidth;
}void MainWindow::resizeEvent(QResizeEvent *)
{m_rect.moveCenter(this->rect().center());updateRect();
}void MainWindow::mousePressEvent(QMouseEvent *event)
{if(!event){return;}if(m_topLeft.contains(event->pos())){m_bSetTopLeft=true;}else if(m_topRight.contains(event->pos())){m_bSetTopRight=true;}else if(m_bottomLeft.contains(event->pos())){m_bSetBottomLeft=true;}else if(m_bottomRight.contains(event->pos())){m_bSetBottomRight=true;}else if(m_top.contains(event->pos())){m_bSetTop=true;}else if(m_bottom.contains(event->pos())){m_bSetBottom=true;}else if(m_left.contains(event->pos())){m_bSetLeft=true;}else if(m_right.contains(event->pos())){m_bSetRight=true;}else if(m_rect.contains(event->pos())){m_bPress=true;QPoint pressPoint = event->pos();QPoint center = m_rect.center();m_pressPoint = QPoint(pressPoint.x()-center.x(),pressPoint.y()-center.y());}
}void MainWindow::mouseReleaseEvent(QMouseEvent *)
{m_bPress=false;m_bSetTop  = false;m_bSetLeft = false;m_bSetRight=false;m_bSetBottom=false;m_bSetTopRight=false;m_bSetBottomLeft=false;m_bSetTopLeft=false;m_bSetBottomRight=false;qDebug()<< __LINE__ << __FUNCTION__ << __FILE__<<m_rect;
}void MainWindow::mouseMoveEvent(QMouseEvent *event)
{if(!event){return;}if(m_bSetTopRight){QRect rect = m_rect;rect.setTopRight(event->pos());if(!this->isValid(rect)){return;}if(rect.width()<0||rect.height()<0){m_bSetBottomLeft=true;m_bSetTopRight=false;m_rect.setBottomLeft(event->pos());}else{m_rect=rect;}updateRect();update();}else if(m_bSetBottomLeft){QRect rect = m_rect;rect.setBottomLeft(event->pos());if(!this->isValid(rect)){return;}if(rect.width()<0||rect.height()<0){m_bSetBottomLeft=false;m_bSetTopRight=true;m_rect.setTopRight(event->pos());}else{m_rect=rect;}updateRect();update();}else if(m_bSetTopLeft){QRect rect = m_rect;rect.setTopLeft(event->pos());if(!this->isValid(rect)){return;}if(rect.width()<0||rect.height()<0){m_bSetTopLeft=false;m_bSetBottomRight=true;m_rect.setBottomRight(event->pos());}else{m_rect=rect;}updateRect();update();}else if(m_bSetBottomRight){QRect rect = m_rect;rect.setBottomRight(event->pos());if(!this->isValid(rect)){return;}if(rect.width()<0||rect.height()<0){m_bSetTopLeft=true;m_bSetBottomRight=false;m_rect.setTopLeft(event->pos());}else{m_rect=rect;}updateRect();update();}else if(m_bSetTop){QRect rect = m_rect;rect.setTop(event->pos().y());if(!this->isValid(rect)){return;}if(rect.width()<0||rect.height()<0){m_bSetBottom=true;m_bSetTop=false;m_rect.setBottom(event->pos().y());}else{m_rect=rect;}updateRect();update();}else if(m_bSetBottom){QRect rect = m_rect;rect.setBottom(event->pos().y());if(!this->isValid(rect)){return;}if(rect.width()<0||rect.height()<0){m_bSetBottom=false;m_bSetTop=true;m_rect.setTop(event->pos().y());}else{m_rect=rect;}updateRect();update();}else if(m_bSetLeft){QRect rect = m_rect;rect.setLeft(event->pos().x());if(!this->isValid(rect)){return;}if(rect.width()<0||rect.height()<0){m_bSetRight=true;m_bSetLeft=false;m_rect.setRight(event->pos().x());}else{m_rect=rect;}updateRect();update();}else if(m_bSetRight){QRect rect = m_rect;rect.setRight(event->pos().x());if(!this->isValid(rect)){return;}if(rect.width()<0||rect.height()<0){m_bSetRight=false;m_bSetLeft=true;m_rect.setLeft(event->pos().x());}else{m_rect=rect;}updateRect();update();}else if(m_bPress){QPoint point = event->pos();point -= m_pressPoint;QRect rect = m_rect;rect.moveCenter(point);if(this->isOutOfRange(rect)){return;}m_rect=rect;updateRect();update();}else{this->setSizeCursor(event);}
}bool MainWindow::isValid(QRect& rect)
{if(m_bMinSize){if(0 == m_minHeight || 0 ==  m_minWidth){return true;}if(rect.width() < m_minWidth){return false;}if(rect.height() < m_minHeight){return false;}}if(this->isOutOfRange(rect)){return false;}return true;
}bool MainWindow::isOutOfRange(QRect &rect)
{if(rect.right() > this->rect().right()){return true;}if(rect.bottom() > this->rect().bottom()){return true;}if(rect.left() < 0){return true;}if(rect.top() < 0){return true;}return false;
}void MainWindow::setSizeCursor(QMouseEvent *event)
{if(m_topLeft.contains(event->pos())){this->setCursor(QCursor(Qt::SizeFDiagCursor));}else if(m_topRight.contains(event->pos())){this->setCursor(QCursor(Qt::SizeBDiagCursor));}else if(m_bottomLeft.contains(event->pos())){this->setCursor(QCursor(Qt::SizeBDiagCursor));}else if(m_bottomRight.contains(event->pos())){this->setCursor(QCursor(Qt::SizeFDiagCursor));}else if(m_top.contains(event->pos())){this->setCursor(QCursor(Qt::SizeVerCursor));}else if(m_bottom.contains(event->pos())){this->setCursor(QCursor(Qt::SizeVerCursor));}else if(m_left.contains(event->pos())){this->setCursor(QCursor(Qt::SizeHorCursor));}else if(m_right.contains(event->pos())){this->setCursor(QCursor(Qt::SizeHorCursor));}else if(m_rect.contains(event->pos())){this->setCursor(QCursor(Qt::SizeAllCursor));}else{this->setCursor(QCursor(Qt::ArrowCursor));}
}

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.hqwc.cn/news/652454.html

如若内容造成侵权/违法违规/事实不符,请联系编程知识网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

MySQL B+索引的工作原理及应用

引言 在数据库系统中&#xff0c;索引是优化查询、提高性能的关键技术之一。特别是在MySQL数据库中&#xff0c;B树索引作为最常用的索引类型&#xff0c;对数据库性能有着至关重要的影响。本文旨简单解析MySQL中B树索引的工作原理&#xff0c;帮助学生朋友们更好地理解和利用…

ruoyi-nbcio-plus基于vue3的flowable为了适配文件上传改造VForm3的代码记录

更多ruoyi-nbcio功能请看演示系统 gitee源代码地址 前后端代码&#xff1a; https://gitee.com/nbacheng/ruoyi-nbcio 演示地址&#xff1a;RuoYi-Nbcio后台管理系统 http://218.75.87.38:9666/ 更多nbcio-boot功能请看演示系统 gitee源代码地址 后端代码&#xff1a; h…

国产3D自研技术如何突围?眸瑞科技给3D建设、管理带来全新模式

眸瑞科技是全球领先的数字孪生引擎技术及服务提供商&#xff0c;它专注于让一切3D模型在全网多端轻量化处理与展示&#xff0c;为行业数字化转型升级与数字孪生应用提供成套的国产自研3D可视化技术、产品与服务。 引言 眸瑞科技是全球领先的数字孪生引擎技术及服务提供商&…

SSRF漏洞是什么,如何进行有效防护

SSRF全称&#xff1a;Server-Side Request Forgery&#xff0c;即&#xff0c;服务器端请求伪造。是一个由攻击者构造请求&#xff0c;在目标服务端执行的一个安全漏洞。攻击者可以利用该漏洞使服务器端向攻击者构造的任意域发出请求&#xff0c;目标通常是从外网无法访问的内部…

Ubuntu2004 CMake 使用基础

一、环境安装 win10安装wsl ubuntu2004 #windows c盘工程目录建立软链 ln -s /mnt/c /home/vrviu/ 安装cmake、c编译工具 apt install -y cmake g 二、CMakeLists.txt讲解 准备工作 首先&#xff0c;在/home/vrviu 目录建立一个 cmake 目录 以后我们所有的 cmake 练习都会放…

一文掌握Vue3:深度解读Vue3新特性、Vue2与Vue3核心差异以及Vue2到Vue3转型迭代迁移重点梳理与实战

每次技术革新均推动着应用性能与开发体验的提升。Vue3 的迭代进步体现在性能优化、API重构与增强型TypeScript支持等方面&#xff0c;从而实现更高效开发、更优运行表现&#xff0c;促使升级成为保持竞争力与跟进现代前端趋势的必然选择。本文深度解读Vue3 响应式数据data、生命…

稳态视觉诱发电位 (SSVEP) 分类学习系列 (4) :Temporal-Spatial Transformer

稳态视觉诱发电位分类学习系列:Temporal-Spatial Transformer 0. 引言1. 主要贡献2. 提出的方法2.1 解码的主要步骤2.2 网络的主要结构 3. 结果和讨论3.1 在两个数据集下的分类效果3.2 与基线模型的比较3.3 消融实验3.4 t-SNE 可视化 4. 总结欢迎来稿 论文地址&#xff1a;http…

【Elasticsearch<二>✈️✈️】基本属性概念与MySQL数据库的不同之处

目录 &#x1f378;前言 &#x1f37b;一、Elasticsearch 基本属性 1.1 ES VS MySQL 1.2 ES 属性概念 1.3 ES 的增删改查 &#x1f37a;二、自动补全场景 2.1 场景举例 2.2 使用数据分词器 2.3 查询的流程 2.4 整个查询流程图 &#x1f379;章末 &#x1f378;前言 上次初步…

[C++ QT项目实战]----C++ QT系统实现多线程通信

前言 在C QT中&#xff0c;多线程通信原理主要涉及到信号与槽机制和事件循环机制。 1、信号与槽机制&#xff1a; 在QT中&#xff0c;信号与槽是一种用于对象间通信的机制。对象可以通过发送信号来通知其他对象&#xff0c;其他对象通过连接槽来接收信号并进行相应的处…

微信小程序:12.页面导航

什么是页面导航 页面导航指的是页面之间的相互跳转。例如&#xff0c;浏览器中实现的页面导航的方式有两种&#xff1a; 连接location.href 小程序中实现页面导航的两种方式 声明式导航 在页面上声明一个导航组件 通过点击组件实现页面跳转 导航TabBar页面 是指配置TabB…

mac 教程 终端如何拆墙

一直觉得自己写的不是技术&#xff0c;而是情怀&#xff0c;一个个的教程是自己这一路走来的痕迹。靠专业技能的成功是最具可复制性的&#xff0c;希望我的这条路能让你们少走弯路&#xff0c;希望我能帮你们抹去知识的蒙尘&#xff0c;希望我能帮你们理清知识的脉络&#xff0…

C语言:一维数组、二维数组、字符数组介绍

数组 介绍一维数组定义应用方法初始化 举例示例结果 二维数组定义应用方法初始化 举例示例结果 字符数组定义应用方法初始化 举例示例结果分析 介绍 在C语言中&#xff0c;数组是一种基本的数据结构&#xff0c;用于存储一系列相同类型的数据。数组可以是多维的&#xff0c;最…