Qt QCustomPlot 和 QCPItemTracer 游标的使用

    科技2024-06-18  68

    .h文件中申明要用的对象指针

    QCustomPlot *m_plot; QCPItemTracer *tracer; //游标 QCPItemText *textLabel; //图标标签 QCPItemText *tracerLabel; //游标标签 QCPGraph *tracerGraph;//游标要吸附哪个graph

    将X轴坐标设置为以时间

    //以时间格式显示坐标轴 QSharedPointer<QCPAxisTickerDateTime> dateTicker(new QCPAxisTickerDateTime); //以智能指针方式定义dateTicker dateTicker->setDateTimeFormat("hh:mm:ss.zzz\nyy-MM-dd"); //时间格式 dateTicker->setTickCount(10); //设置栅格数为10 plot->xAxis->setTickLabelRotation(35); //X轴偏移35度显示 dateTicker->setTickStepStrategy(QCPAxisTicker::tssMeetTickCount); plot->xAxis->setTicker(dateTicker); //设置X轴为时间 plot->setWindowTitle(item->text());

    在图标上设置一个图标标签

    //在图表上显示具体的IO点 textLabel = new QCPItemText(plot);//在QCustomplot中新建文字框 textLabel->setPositionAlignment(Qt::AlignTop|Qt::AlignRight);//文字布局:顶、左对齐 textLabel->position->setType(QCPItemPosition::ptAxisRectRatio);//位置类型(当前轴范围的比例为单位/实际坐标为单位) textLabel->position->setCoords(0.4, 0); //把文字框放在X轴的中间,Y轴的最顶部 textLabel->setText(QString("第%1组IO板 %2").arg(flag+1).arg(item->text())); textLabel->setFont(QFont(font().family(), 16)); //字体大小 textLabel->setPen(QPen(Qt::black)); //字体颜色 textLabel->setPadding(QMargins(2,2,2,2));//文字距离边框几个像素 plot->setWindowTitle(item->text());

    设置画笔工具

    QPen pen; //新建一个画笔 pen.setWidth(2); pen.setColor(Qt::green); //画笔设置红色

    添加图标并生成图线

    m_plot->addGraph(); //添加图表 //添加数据 m_plot->graph(0)->addData(time, y); //设置图表数据 m_plot->graph(0)->setName("输入输出IO"); //设置曲线名称 m_plot->graph(0)->setLineStyle(QCPGraph::lsStepCenter); //设置连线类型 m_plot->graph(0)->setPen(pen); //设置画笔 m_plot->graph(0)->rescaleAxes(true); //重新调整数据点 m_plot->graph(0)->setScatterStyle(QCPScatterStyle(QCPScatterStyle::ssDisc, 4)); //显示散点数据

    设置图像X、Y轴,设置图像可拖动,放大

    m_plot->setInteractions(QCP::iRangeZoom|QCP::iRangeDrag | QCP::iSelectAxes | QCP::iSelectLegend | QCP::iSelectPlottables); //设置图表支持放大,拖动 m_plot->axisRect()->setRangeDrag(Qt::Horizontal); m_plot->axisRect()->setRangeZoom(Qt::Horizontal); m_plot->xAxis->ticker()->setTickCount(8);//12个主刻度 m_plot->xAxis->ticker()->setTickStepStrategy(QCPAxisTicker::tssReadability);//可读性优于设置 m_plot->xAxis->setRange(time.at(0),time.at(time.size()-1)); //设置X轴范围,为第一条时间到最后一条时间 m_plot->yAxis->setRange(-1,2); //设置Y轴范围

    设置游标

    //下面的代码就是设置游标的外观 this->setMouseTracking(true); tracer->setInterpolating(false);//禁用插值 tracer->setPen(QPen(Qt::DashLine));//虚线游标 tracer->setStyle(QCPItemTracer::tsPlus);//游标样式:十字星、圆圈、方框

    设置游标标签

    //下面的代码就是设置游标说明的外观和对齐方式等状态 tracerLabel->setLayer("overlay"); tracerLabel->setPen(QPen(Qt::black)); tracerLabel->setBrush(Qt::cyan); tracerLabel->setPadding(QMargins(2,2,2,2));//边界宽度 tracerLabel->setPositionAlignment(Qt::AlignLeft | Qt::AlignTop); //下面这个语句很重要,它将游标说明锚固在tracer位置处,实现自动跟随 tracerLabel->position->setParentAnchor(tracer->position);

    当有多条曲线时,设置游标指定曲线

    if(m_plot->graphCount() > 0){ tracerGraph = m_plot->graph(0); }

    刷新图标

    m_plot->replot(); //刷新图表

    获取鼠标移动事件,实时动态显示鼠标指向点的坐标

    bool tracerEnable = false; connect(m_plot, &QCustomPlot::mouseMove,this,[=](QMouseEvent *event){ double x = m_plot->xAxis->pixelToCoord(event->pos().x()); //下面的代码很关键 tracer->setGraph(tracerGraph); //设置游标吸附于指定曲线 if(!tracerEnable) tracer->setGraph(NULL); tracer->setGraphKey(x); //将游标横坐标(key)设置成刚获得的横坐标数据x tracer->setInterpolating(true); //游标的纵坐标可以通过曲线数据线性插值自动获得 tracer->updatePosition(); //使得刚设置游标的横纵坐标位置生效 //以下代码用于更新游标说明的内容 double xValue = tracer->position->key(); double yValue = ((int)(tracer->position->value()*1000))/1000.0; //y轴值保留三位有效数字 tracerLabel->setText(QString("x = %1\ny = %2").arg(QDateTime::fromMSecsSinceEpoch(xValue*1000.0).toString("yy-MM-dd hh:mm:ss.zzz")).arg(yValue));//(*1000) ms 转 s m_plot->replot(); //刷新图标,不能省略 });

    设置游标吸附的曲线

    connect(m_plot, &QCustomPlot::selectionChangedByUser, this,[=]() mutable{ for (int i = 0; i < m_plot->graphCount(); ++i) { QCPGraph *graph = m_plot->graph(i); QCPPlottableLegendItem *item = m_plot->legend->itemWithPlottable(graph); if (item->selected() || graph->selected())//选中了哪条曲线或者曲线的图例 { tracerGraph = graph; tracerEnable = true; } } });

    Processed: 0.013, SQL: 8