本文主要是介绍【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的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!