qt跟随鼠标动态绘制_基于QCustomPlot绘图,鼠标跟随动态显_weixin_39979516的博客-程序员秘密

技术标签: qt跟随鼠标动态绘制  

#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);

}

}

}

版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://blog.csdn.net/weixin_39979516/article/details/111526586

智能推荐

python 生成list的所有元素的组合_huoxingd的博客-程序员秘密

生成排列可以用product:from itertools import productl = [1, 2, 3]print (list(product(l, l)))print (list(product(l, repeat=4)))组合的话可以用combinations:from itertools import combinationsprint (list(comb...

使用Altium Designer绘制一个stm32最小系统的电路原理图并与SD卡使用成为相关电路_ad怎么仿真stm32_qq_58174923的博客-程序员秘密

一 绘制准备先去网上下载好Altium Designer并进行相应的激活以便使用。新建工程点击file-new-project-pcbproject新建一个pcbproject如下右键点击新建的pcb工程然后选择add new to project-Schematic新建原理图加载后如下二 添加元件库首先去网上下载相应的元件库解压后放在记得的地方。点击右方的Libraries,后如下点击顺序选择刚才下载的元件库点击打开就会出现如下然后最后点击cl...

Azure AD Connect 用户登录选项介绍_weixin_34303897的博客-程序员秘密

Azure AD Connect 相信大家都使用过,他的作用是让用户使用同一帐户密码访问本地和云资源。使IT管理员只需要对本地DC进行用户的管理即可。在新版的Azure AD Connect中,用户的登录选项发生了一些变化,添加了一项:直通身份验证,目前的身份验证方式包括3种:1) 密码同步2) 直通身份验证3) ADFS联合身份验证并且增加了:无缝SSO,此功能可以和密码同步及直通身份验证...

汇编文件后缀 .s 与 .S_asm文件和s文件的区别_scola的博客-程序员秘密

<br />一、大小写后缀的区别<br />.s     汇编语言源程序;汇编<br />.S     汇编语言源程序;预处理,汇编<br /><br />小写的s文件,在后期阶段不在进行预处理操作,所以我们不能在这里面写预处理的语句在里面<br />大写的S文件,还会进行预处理、汇编等操作,所以我们可以在这里面加入预处理的命令

Apache Shiro 使用手册(二)_chunli5093的博客-程序员秘密

认证就是验证用户身份的过程。在认证过程中,用户需要提交实体信息(Principals)和凭据信息(Credentials)以检验用户是否合法。最常见的“实体/凭证”组合便是“用户名/密码”组合。 一、Shiro认证过程 1、收集实体/凭据信息 //Exampleusingmostco...

随便推点

关于Vector删除末尾末尾元素时出现的怪现象_超级无敌陈大佬的跟班的博客-程序员秘密

此段代码是删除连续三个不符合规则的vector元素,情况是三个连续不符合规则的元素位于vector的末尾,当检查到最后一个元素时,正好累计到三个连续不符合规则的元素,此时对三个元素进行删除操作,因为此时for循环i的值已经为14(元素共15个),所以博主本来打算采用删除i-2也就是14-2=12的方式进行删除,但是代码删除到最后一个erase语句时会出现错误,显示指针越界了。目前不清楚为什么会这样

Java单例模式_luoj_616的博客-程序员秘密

1介绍1.1 模式说明实现1个类只有1个实例化对象 &amp; 提供一个全局访问点1.2 作用保证1个类只有1个对象,降低对象之间的耦合度1.3 工作原理Java中,我们通过使用对象(类实例化后)来操作这些类,类实例化是通过它的构造方法进行的,要是想实现一个类只有一个实例化对象,就要对类的构造方法下功夫:单例模式的原理单例模式的一般实现:(含使用步骤)......

Stetman读peper小记:Defense-Resistant Backdoor Attacks Against DeepNeural Networks in Outsourced Cloud_龚雪鸾-程序员秘密

作者在阅读Defense-Resistant Backdoor Attacks Against DeepNeural Networks in Outsourced Cloud 后的整理笔记与一些个人理解

JQ CSS设置_小森呀的博客-程序员秘密

方法名 描述 html() 获取元素中 HTML 内容 html(value) 设置元素中 HTML 内容 text() 获取元素中文本内容 text(value) 设置原生中文本内容 val() 获取表单中的文本内容 val(value) ...

c语言服务端接收信息后转发,基于c语言,网络编程,服务器接收客户端1的信息发送给客户端2..._weixin_40008969的博客-程序员秘密

满意答案audio111425推荐于 2016.04.12采纳率:54%等级:12已帮助:5097人char buff2[1024*10];int receiveLen;/*调用accept函数,等待客户端的连接*/client_fd=accept(sockfd,(struct sockaddr *)&amp;client_sockaddr,&amp;sin_size/*调用recv函数接...

Neo4j+Spring data 开发关系网络案例_chihan3585的博客-程序员秘密

Neo4J环境搭建 一、安装Neo4J数据库 1)下载neo4j :https://neo4j.com/download/?ref=home 开发请下载社区版,生产部署下载企业版。 2)安装 a)Windows系统...