qt跟随鼠标动态绘制_基于QCustomPlot绘图,鼠标跟随动态显

本文介绍如何使用QCustomPlot库创建一个跟随鼠标的动态绘制工具。该工具包括一个跟踪点,显示数值的标签以及指向数据点的箭头。根据不同的追踪类型(X轴、Y轴或数据追踪),动态调整跟踪点和箭头的位置,以实时显示坐标值。
摘要由CSDN通过智能技术生成

#include "MyTracer.h"XxwTracer::XxwTracer(QCustomPlot*_plot, TracerType _type, QObject *parent)

: QObject(parent),

m_plot(_plot),

m_type(_type)

{

m_visible= true;

m_tracer= Q_NULLPTR;//跟踪的点

m_label = Q_NULLPTR;//显示的数值

m_arrow = Q_NULLPTR;//箭头

if(m_plot)

{

QColor clrDefault(Qt::red);

QBrush brushDefault(Qt::NoBrush);

QPen penDefault(clrDefault);//penDefault.setBrush(brushDefault);

penDefault.setWidthF(0.5);

m_tracer= newQCPItemTracer(m_plot);

m_tracer->setStyle(QCPItemTracer::tsCircle);

m_tracer->setPen(penDefault);

m_tracer->setBrush(brushDefault);

m_label= newQCPItemText(m_plot);

m_label->setLayer("overlay");

m_label->setClipToAxisRect(false);

m_label->setPadding(QMargins(5, 5, 5, 5));

m_label->setBrush(brushDefault);

m_label->setPen(penDefault);

m_label->position->setParentAnchor(m_tracer->position);//m_label->setFont(QFont("宋体", 8));

m_label->setFont(QFont("Arial", 8));

m_label->setColor(clrDefault);

m_label->setText("");

m_arrow= newQCPItemLine(m_plot);

QPen arrowPen(clrDefault,1);

m_arrow->setPen(penDefault);

m_arrow->setLayer("overlay");

m_arrow->setClipToAxisRect(false);

m_arrow->setHead(QCPLineEnding::esSpikeArrow);//设置头部为箭头形状

switch(m_type)

{caseXAxisTracer:

{

m_tracer->position->setTypeX(QCPItemPosition::ptPlotCoords);

m_tracer->position->setTypeY(QCPItemPosition::ptAxisRectRatio);

m_tracer->setSize(7);

m_label->setPositionAlignment(Qt::AlignTop |Qt::AlignHCenter);

m_arrow->end->setParentAnchor(m_tracer->position);

m_arrow->start->setParentAnchor(m_arrow->end);

m_arrow->start->setCoords(0, 20);//偏移量

break;

}caseYAxisTracer:

{

m_tracer->position->setTypeX(QCPItemPosition::ptAxisRectRatio);

m_tracer->position->setTypeY(QCPItemPosition::ptPlotCoords);

m_tracer->setSize(7);

m_label->setPositionAlignment(Qt::AlignRight |Qt::AlignHCenter);

m_arrow->end->setParentAnchor(m_tracer->position);

m_arrow->start->setParentAnchor(m_label->position);

m_arrow->start->setCoords(-20, 0);//偏移量

break;

}caseDataTracer:

{

m_tracer->position->setTypeX(QCPItemPosition::ptPlotCoords);

m_tracer->position->setTypeY(QCPItemPosition::ptPlotCoords);

m_tracer->setSize(5);

m_label->setPositionAlignment(Qt::AlignLeft |Qt::AlignVCenter);

m_arrow->end->setParentAnchor(m_tracer->position);

m_arrow->start->setParentAnchor(m_arrow->end);

m_arrow->start->setCoords(20, 0);break;

}default:break;

}

setVisible(false);

}

}

XxwTracer::~XxwTracer()

{if(m_plot)

{if(m_tracer)

m_plot->removeItem(m_tracer);if(m_label)

m_plot->removeItem(m_label);if(m_arrow)

m_plot->removeItem(m_arrow);

}

}void XxwTracer::setPen(const QPen &pen)

{if(m_tracer)

m_tracer->setPen(pen);if(m_arrow)

m_arrow->setPen(pen);

}void XxwTracer::setBrush(const QBrush &brush)

{if(m_tracer)

m_tracer->setBrush(brush);

}void XxwTracer::setLabelPen(const QPen &pen)

{if(m_label)

{

m_label->setPen(pen);

m_label->setBrush(Qt::NoBrush);

m_label->setColor(pen.color());

}

}void XxwTracer::setText(const QString &text)

{if(m_label)

m_label->setText(text);

}void XxwTracer::setVisible(boolvis)

{

m_visible=vis;if(m_tracer)

m_tracer->setVisible(m_visible);if(m_label)

m_label->setVisible(m_visible);if(m_arrow)

m_arrow->setVisible(m_visible);

}void XxwTracer::updatePosition(double xValue, doubleyValue)

{if (!m_visible)

{

setVisible(true);

m_visible= true;

}if (yValue > m_plot->yAxis->range().upper)

yValue= m_plot->yAxis->range().upper;switch(m_type)

{caseXAxisTracer:

{

m_tracer->position->setCoords(xValue, 1);

m_label->position->setCoords(0, 15);

m_arrow->start->setCoords(0, 15);

m_arrow->end->setCoords(0, 0);

setText(QString::number(xValue));break;

}caseYAxisTracer:

{

m_tracer->position->setCoords(0, yValue);

m_label->position->setCoords(-20, 0);//m_arrow->start->setCoords(20, 0);//m_arrow->end->setCoords(0, 0);

setText(QString::number(yValue));break;

}caseDataTracer:

{

m_tracer->position->setCoords(xValue, yValue);

m_label->position->setCoords(20, 0);

setText(QString("x:%1,y:%2").arg(xValue).arg(yValue));break;

}default:break;

}

}

XxwTraceLine::XxwTraceLine(QCustomPlot*_plot, LineType _type, QObject *parent)

: QObject(parent),

m_type(_type),

m_plot(_plot)

{

m_lineV=Q_NULLPTR;

m_lineH=Q_NULLPTR;

initLine();

}

XxwTraceLine::~XxwTraceLine()

{if(m_plot)

{if(m_lineV)

m_plot->removeItem(m_lineV);if(m_lineH)

m_plot->removeItem(m_lineH);

}

}voidXxwTraceLine::initLine()

{if(m_plot)

{

QPen linesPen(Qt::red,1, Qt::DashLine);if(VerticalLine == m_type || Both ==m_type)

{

m_lineV= new QCPItemStraightLine(m_plot);//垂直线

m_lineV->setLayer("overlay");

m_lineV->setPen(linesPen);

m_lineV->setClipToAxisRect(true);

m_lineV->point1->setCoords(0, 0);

m_lineV->point2->setCoords(0, 0);

}if(HorizonLine == m_type || Both ==m_type)

{

m_lineH= new QCPItemStraightLine(m_plot);//水平线

m_lineH->setLayer("overlay");

m_lineH->setPen(linesPen);

m_lineH->setClipToAxisRect(true);

m_lineH->point1->setCoords(0, 0);

m_lineH->point2->setCoords(0, 0);

}

}

}void XxwTraceLine::updatePosition(double xValue, doubleyValue)

{if(VerticalLine == m_type || Both ==m_type)

{if(m_lineV)

{

m_lineV->point1->setCoords(xValue, m_plot->yAxis->range().lower);

m_lineV->point2->setCoords(xValue, m_plot->yAxis->range().upper);

}

}if(HorizonLine == m_type || Both ==m_type)

{if(m_lineH)

{

m_lineH->point1->setCoords(m_plot->xAxis->range().lower, yValue);

m_lineH->point2->setCoords(m_plot->xAxis->range().upper, yValue);

}

}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值