【QT Graphics/View】自定义动态同心弧DyConcentricArc

2024-02-06 19:10

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

一、功能

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

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

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

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

二、效果图

 

 

三、实现原理

数据结构表达:由2个半径 + 起始角度 + 终点角度组成

    qreal m_radius = 0;        /**< 第一个半径 */qreal m_another_radius = 0; /**< 第二个半径 */qreal m_startAngle = 0;    /**< 起始角度 */qreal m_endAngle = 0;      /**< 终点角度 */

整个图元组成:由3个圆弧+2个直线+箭头组成

绘图在paint()函数中实现

绘制圆弧,根据起始角度和角度范围确定

    painter->drawArc(rect1, m_startAngle * 16, spanAngle * 16);painter->drawArc(rect2, m_startAngle * 16, spanAngle * 16);

 绘制直线,需要根据原点坐标、半径大小、角度计算直线位置

    QLineF maxEndLine;maxEndLine.setP1(QPointF(0, 0.0001));maxEndLine.setLength(qMax(m_radius, m_another_radius));maxEndLine.setAngle(m_endAngle);
painter->drawLine(startLine);

判断鼠标是否再圆弧边上,通过鼠标位置到圆心的距离和半径的大小是否接近判断

bool DyConcentricArc::judgeInAnotherArc(QPointF pos)
{QLineF line(QPointF(0, 0), pos);if(abs(line.length() - m_another_radius) < this->pen().widthF()){return true;}return false;
}

判断鼠标是否在起始直线上,先判断是否在鼠标到圆心的距离是否在2个半径之间,再判断鼠标到圆心直线的角度是否接近开始角度

bool DyConcentricArc::judgeInStartLine(QPointF pos)
{QLineF line(QPointF(0, 0), pos);if(line.length() < qMax(m_radius, m_another_radius) &&line.length() > qMin(m_radius, m_another_radius)){if(qAbs(line.angle() - m_startAngle) < 2 ){return true;}}return false;
}

 再重写鼠标事件,完成对半径、角度的改变

四、关键代码

DyConcentricArc.h

#ifndef DYCONCENTRICARC_H
#define DYCONCENTRICARC_H#include "BaseGraphicsItem.h"
#include "GraphicsMemory.h"class GRAPHICSLIBSHARED_EXPORT DyConcentricArc : public BaseGraphicsItem
{Q_OBJECTpublic:enum E_STATE_FLAG{DEFAULT_FLAG = 0,MOV_ARC_RADIUS,            /**< 第一个弧半径 */MOV_ANOTHER_ARC_RADIUS,    /**< 第二个弧半径 */MOV_START_ANGLE,           /**< 开始角度 */MOV_END_ANGLE              /**< 结束角度 */};DyConcentricArc(qreal radius1, qreal radius2,qreal startAngle, qreal endAngle,E_ItemType type = BaseGraphicsItem::E_ItemType::Dy_ConcentricArc);double radiusInner() const;double radiusOuter() const;double startAngle() const;double endAngle() const;void setConfig(const T_DyArcConfig &config);T_DyArcConfig config() const;protected:virtual QRectF boundingRect() const override;virtual void paint(QPainter *painter,const QStyleOptionGraphicsItem *option,QWidget *widget) override;virtual void contextMenuEvent(QGraphicsSceneContextMenuEvent *event) 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 judgeInArc 是否在第一个圆弧上* @param pos* @return*/bool judgeInArc(QPointF pos);/*** @brief judgeInAnotherArc 是否在第二个圆弧上* @param pos* @return*/bool judgeInAnotherArc(QPointF pos);/*** @brief judgeInStartLine 是否在开始边上* @param pos* @return*/bool judgeInStartLine(QPointF pos);/*** @brief judgeInEndLine 是否在结束边上* @param pos* @return*/bool judgeInEndLine(QPointF pos);private:qreal m_radius = 0;        /**< 第一个半径 */qreal m_another_radius = 0; /**< 第二个半径 */qreal m_startAngle = 0;    /**< 起始角度 */qreal m_endAngle = 0;      /**< 终点角度 */E_STATE_FLAG m_stateFlag = DEFAULT_FLAG;T_DyArcConfig m_dyArcConfig;
};
#endif // DYCONCENTRICARC_H

 DyConcentricArc.cpp

#include "DyConcentricArc.h"
#include <QCheckBox>
#include <QComboBox>
#include <QDebug>
#include <QMenu>
#include <QSpinBox>
#include <QWidgetAction>
#include <QtMath>DyConcentricArc::DyConcentricArc(qreal radius1, qreal radius2,qreal startAngle, qreal endAngle, BaseGraphicsItem::E_ItemType type): BaseGraphicsItem(type), m_radius(radius1), m_another_radius(radius2),m_startAngle(startAngle), m_endAngle(endAngle)
{setAcceptHoverEvents(true);this->setFlags(QGraphicsItem::ItemIsSelectable | QGraphicsItem::ItemIsMovable | QGraphicsItem::ItemIsFocusable);setConfig(GraphicsMemory::getInstance()->dyArcConfig());
}double DyConcentricArc::radiusInner() const
{return qMin(m_radius, m_another_radius);
}double DyConcentricArc::radiusOuter() const
{return qMax(m_radius, m_another_radius);
}double DyConcentricArc::startAngle() const
{return m_startAngle;
}double DyConcentricArc::endAngle() const
{return m_endAngle;
}void DyConcentricArc::setConfig(const T_DyArcConfig &config)
{m_dyArcConfig = config;
}T_DyArcConfig DyConcentricArc::config() const
{return m_dyArcConfig;
}QRectF DyConcentricArc::boundingRect() const
{qreal maxRadius = qMax(m_radius, m_another_radius);return QRectF(0 - maxRadius, 0 - maxRadius, maxRadius * 2, maxRadius * 2);
}void DyConcentricArc::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);  /* 线段保持原来的线宽 */QBrush brush(pen.color(), Qt::Dense1Pattern);painter->setPen(pen);painter->setBrush(brush);painter->setRenderHints(QPainter::Antialiasing | QPainter::TextAntialiasing);/* 画2个边界弧 */int spanAngle = m_endAngle - m_startAngle ;if(spanAngle < 0){spanAngle = 360 + spanAngle;}QRectF rect1(0 - m_radius, 0 - m_radius, m_radius * 2, m_radius * 2);QRectF rect2(0 - m_another_radius, 0 - m_another_radius, m_another_radius * 2, m_another_radius * 2);painter->drawArc(rect1, m_startAngle * 16, spanAngle * 16);painter->drawArc(rect2, m_startAngle * 16, spanAngle * 16);/* 画中间弧 */pen.setStyle(Qt::DashDotLine);painter->setPen(pen);double r3 = (m_another_radius + m_radius) / 2;QRectF rect3(0 - r3, 0 - r3, r3 * 2, r3 * 2);painter->drawArc(rect3, m_startAngle * 16, spanAngle * 16);/* 画2边界直线-虚线 */pen.setStyle(Qt::DashDotLine);painter->setPen(pen);QLineF minStartLine;minStartLine.setP1(QPointF(0, 0.0001));minStartLine.setLength(qMin(m_radius, m_another_radius));minStartLine.setAngle(m_startAngle);
//    painter->drawLine(minStartLine);QLineF minEndLine;minEndLine.setP1(QPointF(0, 0.0001));minEndLine.setLength(qMin(m_radius, m_another_radius));minEndLine.setAngle(m_endAngle);
//    painter->drawLine(minEndLine);/* 画2边界直线-实线*/pen.setStyle(Qt::SolidLine);painter->setPen(pen);QLineF maxStartLine;maxStartLine.setP1(QPointF(0, 0.0001));maxStartLine.setLength(qMax(m_radius, m_another_radius));maxStartLine.setAngle(m_startAngle);QLineF maxEndLine;maxEndLine.setP1(QPointF(0, 0.0001));maxEndLine.setLength(qMax(m_radius, m_another_radius));maxEndLine.setAngle(m_endAngle);QLineF startLine(minStartLine.p2(), maxStartLine.p2());painter->drawLine(startLine);QLineF endLine(minEndLine.p2(), maxEndLine.p2());painter->drawLine(endLine);/* 画方向-带箭头 */if(0 == m_dyArcConfig.direction){/* 向圆心方向 */double angle = std::atan2(-startLine.dy(), startLine.dx());qreal arrowSize = 10;QPointF arrowP1 = startLine.p1() + QPointF(sin(angle + M_PI / 3) * arrowSize,cos(angle + M_PI / 3) * arrowSize);QPointF arrowP2 = startLine.p1() + QPointF(sin(angle + M_PI - M_PI / 3) * arrowSize,cos(angle + M_PI - M_PI / 3) * arrowSize);painter->drawLine(startLine.p1(), arrowP1);painter->drawLine(startLine.p1(), arrowP2);}else{/* 远离圆心方向 */double angle = std::atan2(-startLine.dy(), startLine.dx());qreal arrowSize = 10;QPointF arrowP1 = startLine.p2() - QPointF(sin(angle + M_PI / 3) * arrowSize,cos(angle + M_PI / 3) * arrowSize);QPointF arrowP2 = startLine.p2() - QPointF(sin(angle + M_PI - M_PI / 3) * arrowSize,cos(angle + M_PI - M_PI / 3) * arrowSize);painter->drawLine(startLine.p2(), arrowP1);painter->drawLine(startLine.p2(), arrowP2);}/* 画点 */pen.setColor(QColor(0, 128, 0));painter->setPen(pen);painter->drawPoint(QPointF(0, 0));
}void DyConcentricArc::contextMenuEvent(QGraphicsSceneContextMenuEvent *event)
{
//    if ( !this->isSelected() )
//    {
//        return;
//    }QMenu *menu = new QMenu();menu->setStyleSheet("QMenu { background-color:rgb(255,255,255); border: 5px solid rgb(235,110,36); }");QCheckBox *modeSelectCheckBox =  new QCheckBox(menu);modeSelectCheckBox->setStyleSheet("QComboBox { background-color:rgb(255,255,255);}");modeSelectCheckBox->setText(tr("Fuzzy edge algorithm"));bool ischecked = (0 == m_dyArcConfig.modeSelect) ? false : true;modeSelectCheckBox->setChecked(ischecked);connect(modeSelectCheckBox, static_cast<void (QCheckBox::*)(int)>(&QCheckBox::stateChanged), [ = ](int index){Q_UNUSED(index)m_dyArcConfig.modeSelect = modeSelectCheckBox->isChecked() ? 1 : 0;emit stateChanged(this);});QComboBox *darkAndLightDirComboBox =  new QComboBox(menu);darkAndLightDirComboBox->setStyleSheet("QComboBox { background-color:rgb(255,255,255);}");darkAndLightDirComboBox->addItem(tr("Auto"));darkAndLightDirComboBox->addItem(tr("From dark to light"));darkAndLightDirComboBox->addItem(tr("From light to dark"));darkAndLightDirComboBox->setCurrentIndex(m_dyArcConfig.darkAndLightDir);connect(darkAndLightDirComboBox, static_cast<void (QComboBox::*)(int)>(&QComboBox::currentIndexChanged), [ = ](int index){m_dyArcConfig.darkAndLightDir = index;emit stateChanged(this);});QWidgetAction *widgetAction1 = new QWidgetAction(menu);widgetAction1->setDefaultWidget(modeSelectCheckBox);menu->addAction(widgetAction1);QWidgetAction *widgetAction2 = new QWidgetAction(menu);widgetAction2->setDefaultWidget(darkAndLightDirComboBox);menu->addAction(widgetAction2);menu->exec(QCursor::pos() + QPoint(radiusOuter(), 0));delete menu;QGraphicsItem::contextMenuEvent(event);
}void DyConcentricArc::hoverEnterEvent(QGraphicsSceneHoverEvent *event)
{if(judgeInArc(event->pos())){setCursor(Qt::CrossCursor);}else if (judgeInAnotherArc(event->pos())){setCursor(Qt::CrossCursor);}else if (judgeInStartLine(event->pos())){setCursor(Qt::CrossCursor);}else if (judgeInEndLine(event->pos())){setCursor(Qt::CrossCursor);}else{setCursor(Qt::ArrowCursor);}event->accept();
}void DyConcentricArc::hoverMoveEvent(QGraphicsSceneHoverEvent *event)
{if(judgeInArc(event->pos())){setCursor(Qt::CrossCursor);}else if (judgeInAnotherArc(event->pos())){setCursor(Qt::CrossCursor);}else if (judgeInStartLine(event->pos())){setCursor(Qt::CrossCursor);}else if (judgeInEndLine(event->pos())){setCursor(Qt::CrossCursor);}else{setCursor(Qt::ArrowCursor);}event->accept();
}void DyConcentricArc::hoverLeaveEvent(QGraphicsSceneHoverEvent *event)
{Q_UNUSED(event)setCursor(Qt::ArrowCursor);event->accept();
}void DyConcentricArc::mousePressEvent(QGraphicsSceneMouseEvent *event)
{if(event->button() == Qt::LeftButton){if(judgeInArc(event->pos())){m_stateFlag = MOV_ARC_RADIUS;}else if(judgeInAnotherArc(event->pos())){m_stateFlag = MOV_ANOTHER_ARC_RADIUS;}else if(judgeInStartLine(event->pos())){m_stateFlag = MOV_START_ANGLE;}else if(judgeInEndLine(event->pos())){m_stateFlag = MOV_END_ANGLE;}}else if(event->button() == Qt::RightButton){}else if(event->button() == Qt::MidButton){/* 中键切换方向 */if(0 == m_dyArcConfig.direction){m_dyArcConfig.direction = 1;}else{m_dyArcConfig.direction = 0;}emit stateChanged(this);}QGraphicsItem::mousePressEvent(event);
}void DyConcentricArc::mouseMoveEvent(QGraphicsSceneMouseEvent *event)
{switch (static_cast<int>(m_stateFlag)){case MOV_ARC_RADIUS:{/* 必须为pos,非scenePos*/QLineF line(QPointF(0, 0), event->pos());m_radius = line.length();break;}case MOV_ANOTHER_ARC_RADIUS:{QLineF line(QPointF(0, 0), event->pos());m_another_radius = line.length();break;}case MOV_START_ANGLE:{QLineF line(QPointF(0, 0), event->pos());m_startAngle = line.angle();break;}case MOV_END_ANGLE:{QLineF line(QPointF(0, 0), event->pos());m_endAngle = line.angle();break;}default:{QGraphicsItem::mouseMoveEvent(event);break;}}
}void DyConcentricArc::mouseReleaseEvent(QGraphicsSceneMouseEvent *event)
{m_stateFlag = DEFAULT_FLAG;if(event->button() == Qt::LeftButton){emit stateChanged(this);}QGraphicsItem::mouseReleaseEvent(event);
}void DyConcentricArc::mouseDoubleClickEvent(QGraphicsSceneMouseEvent *event)
{if(event->button() == Qt::LeftButton){GraphicsMemory::getInstance()->setDyArcConfig(config());emit selectCompleted(this);}else{QGraphicsItem::mouseDoubleClickEvent(event);}
}bool DyConcentricArc::judgeInArc(QPointF pos)
{QLineF line(QPointF(0, 0), pos);if(abs(line.length() - m_radius) < this->pen().widthF()){return true;}return false;
}bool DyConcentricArc::judgeInAnotherArc(QPointF pos)
{QLineF line(QPointF(0, 0), pos);if(abs(line.length() - m_another_radius) < this->pen().widthF()){return true;}return false;
}bool DyConcentricArc::judgeInStartLine(QPointF pos)
{QLineF line(QPointF(0, 0), pos);if(line.length() < qMax(m_radius, m_another_radius) &&line.length() > qMin(m_radius, m_another_radius)){if(qAbs(line.angle() - m_startAngle) < 2 ){return true;}}return false;
}bool DyConcentricArc::judgeInEndLine(QPointF pos)
{QLineF line(QPointF(0, 0), pos);if(line.length() < qMax(m_radius, m_another_radius) &&line.length() > qMin(m_radius, m_another_radius)){if(qAbs(line.angle() - m_endAngle) < 2 ){return true;}}return false;
}

更多文章

【QT】自定义旋转矩形DyRotatedRectangle_俊俊的博客-CSDN博客

【QT】自定义动态同心圆DyConcentricCircle_俊俊的博客-CSDN博客

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



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

相关文章

VUE动态绑定class类的三种常用方式及适用场景详解

《VUE动态绑定class类的三种常用方式及适用场景详解》文章介绍了在实际开发中动态绑定class的三种常见情况及其解决方案,包括根据不同的返回值渲染不同的class样式、给模块添加基础样式以及根据设... 目录前言1.动态选择class样式(对象添加:情景一)2.动态添加一个class样式(字符串添加:情

基于Qt Qml实现时间轴组件

《基于QtQml实现时间轴组件》时间轴组件是现代用户界面中常见的元素,用于按时间顺序展示事件,本文主要为大家详细介绍了如何使用Qml实现一个简单的时间轴组件,需要的可以参考下... 目录写在前面效果图组件概述实现细节1. 组件结构2. 属性定义3. 数据模型4. 事件项的添加和排序5. 事件项的渲染如何使用

SpringCloud配置动态更新原理解析

《SpringCloud配置动态更新原理解析》在微服务架构的浩瀚星海中,服务配置的动态更新如同魔法一般,能够让应用在不重启的情况下,实时响应配置的变更,SpringCloud作为微服务架构中的佼佼者,... 目录一、SpringBoot、Cloud配置的读取二、SpringCloud配置动态刷新三、更新@R

基于Qt开发一个简单的OFD阅读器

《基于Qt开发一个简单的OFD阅读器》这篇文章主要为大家详细介绍了如何使用Qt框架开发一个功能强大且性能优异的OFD阅读器,文中的示例代码讲解详细,有需要的小伙伴可以参考一下... 目录摘要引言一、OFD文件格式解析二、文档结构解析三、页面渲染四、用户交互五、性能优化六、示例代码七、未来发展方向八、结论摘要

如何用Python绘制简易动态圣诞树

《如何用Python绘制简易动态圣诞树》这篇文章主要给大家介绍了关于如何用Python绘制简易动态圣诞树,文中讲解了如何通过编写代码来实现特定的效果,包括代码的编写技巧和效果的展示,需要的朋友可以参考... 目录代码:效果:总结 代码:import randomimport timefrom math

SpringBoot 自定义消息转换器使用详解

《SpringBoot自定义消息转换器使用详解》本文详细介绍了SpringBoot消息转换器的知识,并通过案例操作演示了如何进行自定义消息转换器的定制开发和使用,感兴趣的朋友一起看看吧... 目录一、前言二、SpringBoot 内容协商介绍2.1 什么是内容协商2.2 内容协商机制深入理解2.2.1 内容

python与QT联合的详细步骤记录

《python与QT联合的详细步骤记录》:本文主要介绍python与QT联合的详细步骤,文章还展示了如何在Python中调用QT的.ui文件来实现GUI界面,并介绍了多窗口的应用,文中通过代码介绍... 目录一、文章简介二、安装pyqt5三、GUI页面设计四、python的使用python文件创建pytho

Java中JSON字符串反序列化(动态泛型)

《Java中JSON字符串反序列化(动态泛型)》文章讨论了在定时任务中使用反射调用目标对象时处理动态参数的问题,通过将方法参数存储为JSON字符串并进行反序列化,可以实现动态调用,然而,这种方式容易导... 需求:定时任务扫描,反射调用目标对象,但是,方法的传参不是固定的。方案一:将方法参数存成jsON字

QT实现TCP客户端自动连接

《QT实现TCP客户端自动连接》这篇文章主要为大家详细介绍了QT中一个TCP客户端自动连接的测试模型,文中的示例代码讲解详细,感兴趣的小伙伴可以跟随小编一起学习一下... 目录版本 1:没有取消按钮 测试效果测试代码版本 2:有取消按钮测试效果测试代码版本 1:没有取消按钮 测试效果缺陷:无法手动停

基于Qt实现系统主题感知功能

《基于Qt实现系统主题感知功能》在现代桌面应用程序开发中,系统主题感知是一项重要的功能,它使得应用程序能够根据用户的系统主题设置(如深色模式或浅色模式)自动调整其外观,Qt作为一个跨平台的C++图形用... 目录【正文开始】一、使用效果二、系统主题感知助手类(SystemThemeHelper)三、实现细节