【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

相关文章

Java调用C++动态库超详细步骤讲解(附源码)

《Java调用C++动态库超详细步骤讲解(附源码)》C语言因其高效和接近硬件的特性,时常会被用在性能要求较高或者需要直接操作硬件的场合,:本文主要介绍Java调用C++动态库的相关资料,文中通过代... 目录一、直接调用C++库第一步:动态库生成(vs2017+qt5.12.10)第二步:Java调用C++

C#如何动态创建Label,及动态label事件

《C#如何动态创建Label,及动态label事件》:本文主要介绍C#如何动态创建Label,及动态label事件,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录C#如何动态创建Label,及动态label事件第一点:switch中的生成我们的label事件接着,

SpringCloud动态配置注解@RefreshScope与@Component的深度解析

《SpringCloud动态配置注解@RefreshScope与@Component的深度解析》在现代微服务架构中,动态配置管理是一个关键需求,本文将为大家介绍SpringCloud中相关的注解@Re... 目录引言1. @RefreshScope 的作用与原理1.1 什么是 @RefreshScope1.

MyBatis 动态 SQL 优化之标签的实战与技巧(常见用法)

《MyBatis动态SQL优化之标签的实战与技巧(常见用法)》本文通过详细的示例和实际应用场景,介绍了如何有效利用这些标签来优化MyBatis配置,提升开发效率,确保SQL的高效执行和安全性,感... 目录动态SQL详解一、动态SQL的核心概念1.1 什么是动态SQL?1.2 动态SQL的优点1.3 动态S

使用Sentinel自定义返回和实现区分来源方式

《使用Sentinel自定义返回和实现区分来源方式》:本文主要介绍使用Sentinel自定义返回和实现区分来源方式,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录Sentinel自定义返回和实现区分来源1. 自定义错误返回2. 实现区分来源总结Sentinel自定

如何自定义Nginx JSON日志格式配置

《如何自定义NginxJSON日志格式配置》Nginx作为最流行的Web服务器之一,其灵活的日志配置能力允许我们根据需求定制日志格式,本文将详细介绍如何配置Nginx以JSON格式记录访问日志,这种... 目录前言为什么选择jsON格式日志?配置步骤详解1. 安装Nginx服务2. 自定义JSON日志格式各

Android自定义Scrollbar的两种实现方式

《Android自定义Scrollbar的两种实现方式》本文介绍两种实现自定义滚动条的方法,分别通过ItemDecoration方案和独立View方案实现滚动条定制化,文章通过代码示例讲解的非常详细,... 目录方案一:ItemDecoration实现(推荐用于RecyclerView)实现原理完整代码实现

基于Spring实现自定义错误信息返回详解

《基于Spring实现自定义错误信息返回详解》这篇文章主要为大家详细介绍了如何基于Spring实现自定义错误信息返回效果,文中的示例代码讲解详细,感兴趣的小伙伴可以跟随小编一起学习一下... 目录背景目标实现产出背景Spring 提供了 @RestConChina编程trollerAdvice 用来实现 HTT

Qt 中 isHidden 和 isVisible 的区别与使用小结

《Qt中isHidden和isVisible的区别与使用小结》Qt中的isHidden()和isVisible()方法都用于查询组件显示或隐藏状态,然而,它们有很大的区别,了解它们对于正确操... 目录1. 基础概念2. 区别清见3. 实际案例4. 注意事项5. 总结1. 基础概念Qt 中的 isHidd

SpringSecurity 认证、注销、权限控制功能(注销、记住密码、自定义登入页)

《SpringSecurity认证、注销、权限控制功能(注销、记住密码、自定义登入页)》SpringSecurity是一个强大的Java框架,用于保护应用程序的安全性,它提供了一套全面的安全解决方案... 目录简介认识Spring Security“认证”(Authentication)“授权” (Auth