• 大小: 397KB
    文件类型: .rar
    金币: 1
    下载: 0 次
    发布日期: 2021-06-14
  • 语言: 其他
  • 标签: qt  QCustomPlot  

资源简介

该资源是qt用QCustomPlot实现鼠标实时回调xy轴数据并显示在曲线内的实现代码,详细请参考我的博客文章

资源截图

代码片段和文件信息

#include “mytracer.h“

myTracer::myTracer(QCustomPlot *_plot QCPGraph *_graph TracerType _type) : plot(_plot)
    graph(_graph)
    type(_type)
    visible(false)

{
    if (plot)
    {
        tracer = new QCPItemTracer(plot);
        tracer->setstyle(QCPItemTracer::tsCircle);//可以选择设置追踪光标的样式,这个是小十字,还有大十字,圆点等样式
        tracer->setPen(QPen(Qt::green));//设置tracer的颜色绿色
        //tracer->setPen(graph->pen());//设置tracer的颜色跟曲线
        tracer->setBrush(QPen(Qt::green).color());
        tracer->setSize(6);

        label = new QCPItemText(plot);
        label->setlayer(“overlay“);
        label->setClipToAxisRect(false);
        label->setPadding(QMargins(5 5 5 5));

        label->position->setParentAnchor(tracer->position);
        label->setFont(QFont(“宋体“ 10));

        arrow = new QCPItemLine(plot);
        arrow->setlayer(“overlay“);
        //arrow->setPen(graph->pen());//设置箭头的颜色跟随曲线
        arrow->setPen(QPen(Qt::red));//设置箭头的颜色红色
        arrow->setClipToAxisRect(false);
        arrow->setHead(QCPLineEnding::esSpikeArrow);

        switch (type) {
        case XAxisTracer:
        {
            tracer->position->setTypeX(QCPItemPosition::ptPlotCoords);
            tracer->position->setTypeY(QCPItemPosition::ptAxisRectRatio);
            label->setBrush(QBrush(QColor(244 244 244 100)));
            label->setPen(QPen(Qt::black));

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

            arrow->end->setParentAnchor(tracer->position);
            arrow->start->setParentAnchor(arrow->end);
            arrow->start->setCoords(20 0);//偏移量
            break;
        }
        case YAxisTracer:
        {
            tracer->position->setTypeX(QCPItemPosition::ptAxisRectRatio);
            tracer->position->setTypeY(QCPItemPosition::ptPlotCoords);

            label->setBrush(QBrush(QColor(244 244 244 100)));
            label->setPen(QPen(Qt::black));
            label->setPositionAlignment(Qt::AlignRight|Qt::AlignHCenter);

            arrow->end->setParentAnchor(tracer->position);
            arrow->start->setParentAnchor(label->position);
            arrow->start->setCoords(-20 0);//偏移量
            break;
        }
        case DataTracer:
        {
            tracer->position->setTypeX(QCPItemPosition::ptPlotCoords);
            tracer->position->setTypeY(QCPItemPosition::ptPlotCoords);

            label->setBrush(QBrush(QColor(244 244 244 150)));
            //label->setPen(graph->pen());//边框跟随曲线颜色
            label->setPen(QPen(Qt::red));//边框红色
            label->setPositionAlignment(Qt::AlignLeft|Qt::AlignVCenter);

            arrow->end->setParentAnchor(tracer->position);
            arrow->start->setParentAnchor(arrow->end);
            arrow->start->setCoords(25 0);
            break;
        }

        default:
            break;
        }

        setVisible(false);
    }
}

myTracer::~myTracer()
{
    if 

 属性            大小     日期    时间   名称
----------- ---------  ---------- -----  ----

     文件       4732  2019-07-10 00:44  MyTracer\mytracer.cpp

     文件       1109  2019-07-10 00:29  MyTracer\mytracer.h

     文件     851667  2015-12-22 08:02  QCustomPlot\qcustomplot-一版.cpp

     文件     149282  2015-12-22 08:02  QCustomPlot\qcustomplot-一版.h

     文件    1118717  2017-09-04 08:02  QCustomPlot\qcustomplot.cpp

     文件     266925  2017-09-04 08:02  QCustomPlot\qcustomplot.h

     目录          0  2019-07-10 09:12  MyTracer

     目录          0  2019-07-10 09:12  QCustomPlot

----------- ---------  ---------- -----  ----

              2392432                    8


评论

共有 条评论