【QT Graphics/View】自定义动态同心圆DyConcentricCircle

2023-12-18 23:40

本文主要是介绍【QT Graphics/View】自定义动态同心圆DyConcentricCircle,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

一、功能

1、任意平移、改变内圆、外圆大小

2、鼠标中键切换箭头方向

3、获取圆心坐标,获取大小半径

4、鼠标移动到圆的边缘上改变鼠标形状

二、效果图

 

三、实现原理

1、图元组成及对应接口

2个圆圈

    painter->drawEllipse(rect1);painter->drawEllipse(rect2);

2条直线

    painter->drawLine(m_center.x() - m_radius, m_center.y(), m_center.x() - m_another_radius, m_center.y());painter->drawLine(m_center.x() + m_radius, m_center.y(), m_center.x() + m_another_radius, m_center.y());

2个箭头(一个箭头由2条直线组成)

2、判断鼠标在圆上

diff = 鼠标点与圆心的距离 - 圆的半径 

如果diff接近于0则认为鼠标在圆上

bool DyConcentricCircle::judgeInCircle(QPointF pos)
{QLineF line(m_center, pos);if(abs(line.length() - m_radius) < this->pen().widthF()){return true;}return false;
}

3、获取半径

并没有规定哪个半径为外半径,哪个半径为内半径,而是通过最大值、最小值确认的

外圆半径 = max(r1, r2)

内圆半径 = min(r1,r2)

double DyConcentricCircle::radiusInner() const
{return qMin(m_radius, m_another_radius);
}double DyConcentricCircle::radiusOuter() const
{return qMax(m_radius, m_another_radius);
}

四、关键代码

.h

#ifndef DYCONCENTRICCIRCLE_H
#define DYCONCENTRICCIRCLE_H#include "BaseGraphicsItem.h"class DyConcentricCircle : public BaseGraphicsItem
{Q_OBJECT
public:DyConcentricCircle(QPointF center, qreal radius1, qreal radius2,E_ItemType type = BaseGraphicsItem::E_ItemType::Dy_ConcentricCircle);double radiusInner() const;double radiusOuter() const;public:enum E_STATE_FLAG{DEFAULT_FLAG = 0,MOV_RADIUS,            /**< 第一个圆半径 */MOV_ANOTHER_RADIUS,    /**< 第二个圆半径 */};protected:virtual QRectF boundingRect() const override;virtual void paint(QPainter *painter,const QStyleOptionGraphicsItem *option,QWidget *widget) override;virtual void hoverEnterEvent(QGraphicsSceneHoverEvent *event) override;virtual void hoverMoveEvent(QGraphicsSceneHoverEvent *event) override;virtual void hoverLeaveEvent(QGraphicsSceneHoverEvent *event) override;virtual void mousePressEvent(QGraphicsSceneMouseEvent *event) override;virtual void mouseMoveEvent(QGraphicsSceneMouseEvent *event) override;virtual void mouseReleaseEvent(QGraphicsSceneMouseEvent *event) override;virtual void mouseDoubleClickEvent(QGraphicsSceneMouseEvent *event) override;private:/*** @brief judgeInCircle 是否在第一个圆上* @param pos* @return*/bool judgeInCircle(QPointF pos);/*** @brief judgeInAnotherArc 是否在第二个圆上* @param pos* @return*/bool judgeInAnotherCircle(QPointF pos);protected:qreal m_radius = 0;            /**< 第一个半径 */qreal m_another_radius = 0;    /**< 第二个半径 */E_STATE_FLAG m_stateFlag = DEFAULT_FLAG;
};#endif // DYCONCENTRICCIRCLE_H

.cpp

#include "DyConcentricCircle.h"
#include <QMenu>
#include <QSpinBox>
#include <QWidgetAction>
#include <QDebug>
#include <QComboBox>
#include <QCheckBox>
#include <QtMath>DyConcentricCircle::DyConcentricCircle(QPointF center, qreal radius1, qreal radius2, E_ItemType type): BaseGraphicsItem(center, type), m_radius(radius1),  m_another_radius(radius2)
{m_pointList.append(new DyPointItem(this, m_center, DyPointItem::Center));m_pointList.setRandColor();setAcceptHoverEvents(true);this->setFlags(QGraphicsItem::ItemIsSelectable | QGraphicsItem::ItemIsMovable | QGraphicsItem::ItemIsFocusable);
}double DyConcentricCircle::radiusInner() const
{return qMin(m_radius, m_another_radius);
}double DyConcentricCircle::radiusOuter() const
{return qMax(m_radius, m_another_radius);
}QRectF DyConcentricCircle::boundingRect() const
{qreal maxRadius = qMax(m_radius, m_another_radius);return QRectF(m_center.x() - maxRadius, m_center.y() - maxRadius, maxRadius * 2, maxRadius * 2);
}void DyConcentricCircle::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
{Q_UNUSED(option);Q_UNUSED(widget);QPen pen = this->pen();double scaleFactor = painter->matrix().m11();pen.setWidthF(pen.widthF() / scaleFactor + 1);  /* 线段保持原来的线宽 */painter->setPen(pen);
//    QBrush brush(pen.color(), Qt::Dense1Pattern);
//    painter->setBrush(brush);painter->setRenderHints(QPainter::Antialiasing | QPainter::TextAntialiasing);/* 画圆 */QRectF rect1(m_center.x() - m_radius, m_center.y() - m_radius,m_radius * 2, m_radius * 2);QRectF rect2(m_center.x() - m_another_radius, m_center.y() - m_another_radius,m_another_radius * 2, m_another_radius * 2);painter->drawEllipse(rect1);painter->drawEllipse(rect2);/* 直线 */pen.setStyle(Qt::SolidLine);painter->setPen(pen);painter->drawLine(m_center.x() - m_radius, m_center.y(), m_center.x() - m_another_radius, m_center.y());painter->drawLine(m_center.x() + m_radius, m_center.y(), m_center.x() + m_another_radius, m_center.y());/* 箭头 */double arrowSize = 10;double arrowAngle = M_PI / 6;if(0 == m_direction){qreal radiusIn = radiusInner();/* 左箭头 */QPointF leftP(m_center.x() - radiusIn,  m_center.y());QPointF leftArrowP1;leftArrowP1.setX(m_center.x() - radiusIn - arrowSize * cos(arrowAngle));leftArrowP1.setY(m_center.y() - arrowSize * sin(arrowAngle));QPointF leftArrowP2;leftArrowP2.setX(m_center.x() - radiusIn - arrowSize * cos(arrowAngle));leftArrowP2.setY(m_center.y() + arrowSize * sin(arrowAngle));painter->drawLine(leftArrowP1, leftP);painter->drawLine(leftArrowP2, leftP);/* 右箭头 */QPointF rightP(m_center.x() + radiusIn,  m_center.y());QPointF rightArrowP1;rightArrowP1.setX(m_center.x() + radiusIn + arrowSize * cos(arrowAngle));rightArrowP1.setY(m_center.y() - arrowSize * sin(arrowAngle));QPointF rightArrowP2;rightArrowP2.setX(m_center.x() + radiusIn + arrowSize * cos(arrowAngle));rightArrowP2.setY(m_center.y() + arrowSize * sin(arrowAngle));painter->drawLine(rightArrowP1, rightP);painter->drawLine(rightArrowP2, rightP);}else{qreal radiusOut = radiusOuter();/* 左箭头 */QPointF leftP(m_center.x() - radiusOut,  m_center.y());QPointF leftArrowP1;leftArrowP1.setX(m_center.x() - radiusOut + arrowSize * cos(arrowAngle));leftArrowP1.setY(m_center.y() - arrowSize * sin(arrowAngle));QPointF leftArrowP2;leftArrowP2.setX(m_center.x() - radiusOut + arrowSize * cos(arrowAngle));leftArrowP2.setY(m_center.y() + arrowSize * sin(arrowAngle));painter->drawLine(leftArrowP1, leftP);painter->drawLine(leftArrowP2, leftP);/* 右箭头 */QPointF rightP(m_center.x() + radiusOut,  m_center.y());QPointF rightArrowP1;rightArrowP1.setX(m_center.x() + radiusOut - arrowSize * cos(arrowAngle));rightArrowP1.setY(m_center.y() - arrowSize * sin(arrowAngle));QPointF rightArrowP2;rightArrowP2.setX(m_center.x() + radiusOut - arrowSize * cos(arrowAngle));rightArrowP2.setY(m_center.y() + arrowSize * sin(arrowAngle));painter->drawLine(rightArrowP1, rightP);painter->drawLine(rightArrowP2, rightP);}
}void DyConcentricCircle::hoverEnterEvent(QGraphicsSceneHoverEvent *event)
{if(judgeInCircle(event->pos())){setCursor(Qt::CrossCursor);}else if (judgeInAnotherCircle(event->pos())){setCursor(Qt::CrossCursor);}else{setCursor(Qt::ArrowCursor);}event->accept();
}void DyConcentricCircle::hoverMoveEvent(QGraphicsSceneHoverEvent *event)
{if(judgeInCircle(event->pos())){setCursor(Qt::CrossCursor);}else if (judgeInAnotherCircle(event->pos())){setCursor(Qt::CrossCursor);}else{setCursor(Qt::ArrowCursor);}event->accept();
}void DyConcentricCircle::hoverLeaveEvent(QGraphicsSceneHoverEvent *event)
{setCursor(Qt::ArrowCursor);event->accept();
}void DyConcentricCircle::mousePressEvent(QGraphicsSceneMouseEvent *event)
{if(event->button() == Qt::LeftButton){if(judgeInCircle(event->pos())){m_stateFlag = MOV_RADIUS;}else if(judgeInAnotherCircle(event->pos())){m_stateFlag = MOV_ANOTHER_RADIUS;}}else if(event->button() == Qt::RightButton){}else if(event->button() == Qt::MiddleButton){/* 中键切换方向 */if(0 == m_direction){m_direction = 1;}else{m_direction = 0;}emit stateChanged(this);}QGraphicsItem::mousePressEvent(event);
}void DyConcentricCircle::mouseMoveEvent(QGraphicsSceneMouseEvent *event)
{switch (static_cast<int>(m_stateFlag)){case MOV_RADIUS:{/* 必须为pos,非scenePos*/QLineF line(m_center, event->pos());m_radius = line.length();break;}case MOV_ANOTHER_RADIUS:{QLineF line(m_center, event->pos());m_another_radius = line.length();break;}default:{QGraphicsItem::mouseMoveEvent(event);break;}}
}void DyConcentricCircle::mouseReleaseEvent(QGraphicsSceneMouseEvent *event)
{m_stateFlag = DEFAULT_FLAG;if(event->button() == Qt::LeftButton){emit stateChanged(this);}QGraphicsItem::mouseReleaseEvent(event);
}void DyConcentricCircle::mouseDoubleClickEvent(QGraphicsSceneMouseEvent *event)
{if(event->button() == Qt::LeftButton){emit selectCompleted(this);}QGraphicsItem::mouseDoubleClickEvent(event);
}bool DyConcentricCircle::judgeInCircle(QPointF pos)
{QLineF line(m_center, pos);if(abs(line.length() - m_radius) < this->pen().widthF()){return true;}return false;
}bool DyConcentricCircle::judgeInAnotherCircle(QPointF pos)
{QLineF line(m_center, pos);if(abs(line.length() - m_another_radius) < this->pen().widthF()){return true;}return false;
}

这篇关于【QT Graphics/View】自定义动态同心圆DyConcentricCircle的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



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

相关文章

慢sql提前分析预警和动态sql替换-Mybatis-SQL

《慢sql提前分析预警和动态sql替换-Mybatis-SQL》为防止慢SQL问题而开发的MyBatis组件,该组件能够在开发、测试阶段自动分析SQL语句,并在出现慢SQL问题时通过Ducc配置实现动... 目录背景解决思路开源方案调研设计方案详细设计使用方法1、引入依赖jar包2、配置组件XML3、核心配

Qt实现网络数据解析的方法总结

《Qt实现网络数据解析的方法总结》在Qt中解析网络数据通常涉及接收原始字节流,并将其转换为有意义的应用层数据,这篇文章为大家介绍了详细步骤和示例,感兴趣的小伙伴可以了解下... 目录1. 网络数据接收2. 缓冲区管理(处理粘包/拆包)3. 常见数据格式解析3.1 jsON解析3.2 XML解析3.3 自定义

Spring Security自定义身份认证的实现方法

《SpringSecurity自定义身份认证的实现方法》:本文主要介绍SpringSecurity自定义身份认证的实现方法,下面对SpringSecurity的这三种自定义身份认证进行详细讲解,... 目录1.内存身份认证(1)创建配置类(2)验证内存身份认证2.JDBC身份认证(1)数据准备 (2)配置依

springboot使用Scheduling实现动态增删启停定时任务教程

《springboot使用Scheduling实现动态增删启停定时任务教程》:本文主要介绍springboot使用Scheduling实现动态增删启停定时任务教程,具有很好的参考价值,希望对大家有... 目录1、配置定时任务需要的线程池2、创建ScheduledFuture的包装类3、注册定时任务,增加、删

SpringBoot基于配置实现短信服务策略的动态切换

《SpringBoot基于配置实现短信服务策略的动态切换》这篇文章主要为大家详细介绍了SpringBoot在接入多个短信服务商(如阿里云、腾讯云、华为云)后,如何根据配置或环境切换使用不同的服务商,需... 目录目标功能示例配置(application.yml)配置类绑定短信发送策略接口示例:阿里云 & 腾

C++如何通过Qt反射机制实现数据类序列化

《C++如何通过Qt反射机制实现数据类序列化》在C++工程中经常需要使用数据类,并对数据类进行存储、打印、调试等操作,所以本文就来聊聊C++如何通过Qt反射机制实现数据类序列化吧... 目录设计预期设计思路代码实现使用方法在 C++ 工程中经常需要使用数据类,并对数据类进行存储、打印、调试等操作。由于数据类

Qt中QGroupBox控件的实现

《Qt中QGroupBox控件的实现》QGroupBox是Qt框架中一个非常有用的控件,它主要用于组织和管理一组相关的控件,本文主要介绍了Qt中QGroupBox控件的实现,具有一定的参考价值,感兴趣... 目录引言一、基本属性二、常用方法2.1 构造函数 2.2 设置标题2.3 设置复选框模式2.4 是否

QT进行CSV文件初始化与读写操作

《QT进行CSV文件初始化与读写操作》这篇文章主要为大家详细介绍了在QT环境中如何进行CSV文件的初始化、写入和读取操作,本文为大家整理了相关的操作的多种方法,希望对大家有所帮助... 目录前言一、CSV文件初始化二、CSV写入三、CSV读取四、QT 逐行读取csv文件五、Qt如何将数据保存成CSV文件前言

Qt中QUndoView控件的具体使用

《Qt中QUndoView控件的具体使用》QUndoView是Qt框架中用于可视化显示QUndoStack内容的控件,本文主要介绍了Qt中QUndoView控件的具体使用,具有一定的参考价值,感兴趣的... 目录引言一、QUndoView 的用途二、工作原理三、 如何与 QUnDOStack 配合使用四、自

MySQL中动态生成SQL语句去掉所有字段的空格的操作方法

《MySQL中动态生成SQL语句去掉所有字段的空格的操作方法》在数据库管理过程中,我们常常会遇到需要对表中字段进行清洗和整理的情况,本文将详细介绍如何在MySQL中动态生成SQL语句来去掉所有字段的空... 目录在mysql中动态生成SQL语句去掉所有字段的空格准备工作原理分析动态生成SQL语句在MySQL