qt实现方框调整

2024-04-27 11:12
文章标签 实现 qt 方框 调整

本文主要是介绍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));}
}

这篇关于qt实现方框调整的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/940347

相关文章

python生成随机唯一id的几种实现方法

《python生成随机唯一id的几种实现方法》在Python中生成随机唯一ID有多种方法,根据不同的需求场景可以选择最适合的方案,文中通过示例代码介绍的非常详细,需要的朋友们下面随着小编来一起学习学习... 目录方法 1:使用 UUID 模块(推荐)方法 2:使用 Secrets 模块(安全敏感场景)方法

Spring StateMachine实现状态机使用示例详解

《SpringStateMachine实现状态机使用示例详解》本文介绍SpringStateMachine实现状态机的步骤,包括依赖导入、枚举定义、状态转移规则配置、上下文管理及服务调用示例,重点解... 目录什么是状态机使用示例什么是状态机状态机是计算机科学中的​​核心建模工具​​,用于描述对象在其生命

Spring Boot 结合 WxJava 实现文章上传微信公众号草稿箱与群发

《SpringBoot结合WxJava实现文章上传微信公众号草稿箱与群发》本文将详细介绍如何使用SpringBoot框架结合WxJava开发工具包,实现文章上传到微信公众号草稿箱以及群发功能,... 目录一、项目环境准备1.1 开发环境1.2 微信公众号准备二、Spring Boot 项目搭建2.1 创建

IntelliJ IDEA2025创建SpringBoot项目的实现步骤

《IntelliJIDEA2025创建SpringBoot项目的实现步骤》本文主要介绍了IntelliJIDEA2025创建SpringBoot项目的实现步骤,文中通过示例代码介绍的非常详细,对大家... 目录一、创建 Spring Boot 项目1. 新建项目2. 基础配置3. 选择依赖4. 生成项目5.

Linux下删除乱码文件和目录的实现方式

《Linux下删除乱码文件和目录的实现方式》:本文主要介绍Linux下删除乱码文件和目录的实现方式,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录linux下删除乱码文件和目录方法1方法2总结Linux下删除乱码文件和目录方法1使用ls -i命令找到文件或目录

SpringBoot+EasyExcel实现自定义复杂样式导入导出

《SpringBoot+EasyExcel实现自定义复杂样式导入导出》这篇文章主要为大家详细介绍了SpringBoot如何结果EasyExcel实现自定义复杂样式导入导出功能,文中的示例代码讲解详细,... 目录安装处理自定义导出复杂场景1、列不固定,动态列2、动态下拉3、自定义锁定行/列,添加密码4、合并

mybatis执行insert返回id实现详解

《mybatis执行insert返回id实现详解》MyBatis插入操作默认返回受影响行数,需通过useGeneratedKeys+keyProperty或selectKey获取主键ID,确保主键为自... 目录 两种方式获取自增 ID:1. ​​useGeneratedKeys+keyProperty(推

Spring Boot集成Druid实现数据源管理与监控的详细步骤

《SpringBoot集成Druid实现数据源管理与监控的详细步骤》本文介绍如何在SpringBoot项目中集成Druid数据库连接池,包括环境搭建、Maven依赖配置、SpringBoot配置文件... 目录1. 引言1.1 环境准备1.2 Druid介绍2. 配置Druid连接池3. 查看Druid监控

Linux在线解压jar包的实现方式

《Linux在线解压jar包的实现方式》:本文主要介绍Linux在线解压jar包的实现方式,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录linux在线解压jar包解压 jar包的步骤总结Linux在线解压jar包在 Centos 中解压 jar 包可以使用 u

c++ 类成员变量默认初始值的实现

《c++类成员变量默认初始值的实现》本文主要介绍了c++类成员变量默认初始值,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧... 目录C++类成员变量初始化c++类的变量的初始化在C++中,如果使用类成员变量时未给定其初始值,那么它将被