自定义QCustomplot实现多条曲线查看某一条曲线上的点


当QCustomplot上有两条以上的曲线,怎么实现点击某一条曲线就查看这条曲线上的点呢?

效果如图:

可以分成两步解决

首先是要能查看一条曲线的点,这个qcustomplot有专门的一个QCPItemTracer类来实现。

鼠标移动实现只需重写mouseMoveEvent事件即可,

 

然后是要实现多条曲线选中某一条,QCustomplot也给出了实现方法:

setInteractions(QCP::iSelectPlottables);//设置曲线可选

在鼠标移动事件中加入曲线选择判断条件,并通过setGraph函数将锚点和曲线连接起来即可。

头文件:

protected:
    virtual void mouseMoveEvent(QMouseEvent *event);
private:
QCPItemTracer *tracer;

构造函数中:

tracer = new QCPItemTracer(this);  
tracer
->setInterpolating(false);
tracer
->setStyle(QCPItemTracer::tsCircle);
tracer
->setPen(QPen(Qt::red));
tracer
->setBrush(Qt::red);
tracer
->setSize(6);

setInteractions(QCP::iSelectPlottables);

mouseMoveEvent事件中:

QCPGraph *mGraph = graph(0);
//得到锚点的像素坐标
QPointF temp = tracer->position->coords();
//将像素点转换成qcustomplot中的坐标值,并通过setGraphKey将锚点值设为真实数据值。
tracer->setGraphKey(xAxis->pixelToCoord(event->pos().x())); //遍历曲线 for (int i = 0; i < graphCount(); ++i) { //判断哪一条曲线被选中 if(graph(i)->selected()) { //显示锚点 tracer->setVisible(true); mGraph = graph(i); //显示tip框 QToolTip::showText(event->globalPos(), tr( "<h4>%L1</h4>" "<table>" "<tr>" "<td><h5>X: %L2</h5></td>" "<td> , </td>" "<td><h5>Y: %L3</h5></td>" "</tr>" "</table>").arg(mGraph->name()).arg( QString::number( temp.x(), 'g' ) ).arg( QString::number( temp.y(), 'g' ) ), this, this->rect()); break; } else { //没有曲线被选中,不显示锚点 tracer->setVisible(false); }
}
//将锚点设置到被选中的曲线上 tracer->setGraph(mGraph); //重绘 replot();

 


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM