QT QCustomPlot 中用参考线标示曲线的值

一、前言

嵌入式触摸系统

主要介绍qt用QCustomPlot实现曲线绘制后,点击屏幕,在曲线上显示点击位置的曲线值和参考线

二、开发环境

开发环境 QT5 + linux + QCustomPlot 2.0

前提是已经完成基本曲线绘制功能,这里不介绍曲线绘制功能

三、正文

这里需要使用QCustomPlot的里面的三个类

1.QCPItemTracer

        这个是用来在曲线上画红色圆形标记的(追踪器)

2.QCPItemText

        这个是用来显示曲线值的(label 标签)

3.QCPItemStraightLine

        这个是用来绘制红色的垂直虚线的(在方向上无限延伸)

因为需要标识2个曲线的值,所以在使用时需要

定义2个追踪器、2个数值标签、1个直线

    QCPItemTracer *m_tracer_temp;//追踪器的一个标记
    QCPItemText *m_cur_Label_temp;//显示坐标的文本
    QCPItemTracer *m_tracer_power;
    QCPItemText *m_cur_Label_power;

    QCPItemStraightLine *m_refer_lineV;//参考线

初始化配置,主要是配置对象显示风格,可以自己根据需求修改

    m_tracer_temp = new QCPItemTracer(ui->widgetPlot);        //设置部件的父类
    m_tracer_temp->setStyle(QCPItemTracer::tsCircle);        //标记点的类型设置为圆形
    m_tracer_temp->setPen(QPen(Qt::red, 3, Qt::SolidLine));  //标记点颜色、粗细、线型
    m_tracer_temp->setBrush(Qt::SolidPattern);               //画刷

    m_tracer_power = new QCPItemTracer(ui->widgetPlot);
    m_tracer_power->setStyle(QCPItemTracer::tsCircle);
    m_tracer_power->setPen(QPen(Qt::red, 3, Qt::SolidLine));
    m_tracer_power->setBrush(Qt::SolidPattern);

    m_tracer_power->position->setType(QCPItemPosition::ptPlotCoords);//在曲线上显示
    m_tracer_power->setSize(5);    //标记点大小

    m_tracer_temp->position->setType(QCPItemPosition::ptPlotCoords);//在曲线上显示
    m_tracer_temp->setSize(5);

    m_tracer_temp->setGraph(ui->widgetPlot->graph(0));    //标记点绑定曲线
    m_tracer_power->setGraph(ui->widgetPlot->graph(1));
    m_cur_Label_temp = new QCPItemText(ui->widgetPlot);//文本框父类设置
//    m_cur_Label->setPadding(QMargins(3, 3, 3, 3));    //这个根据需求设置,我这不需要
//    m_cur_Label->setPen(QPen(QColor(130, 130, 130), 0, Qt::DotLine));
//    m_cur_Label->setBrush(Qt::NoBrush);

    //设置标签显示位置跟随锚点的位置
    m_cur_Label_temp->position->setParentAnchor(m_tracer_temp->position);
    m_cur_Label_temp->setFont(QFont(qApp->font().family(), 12));      //设置字体大小
    m_cur_Label_temp->setColor(Qt::white);                            //设置字体颜色

    m_cur_Label_power = new QCPItemText(ui->widgetPlot);//文本框父类设置
    //设置标签显示位置跟随锚点的位置
    m_cur_Label_power->position->setParentAnchor(m_tracer_power->position);
    m_cur_Label_power->setFont(QFont(qApp->font().family(), 12));
    m_cur_Label_power->setColor(Qt::white);

    //设置标签对齐方式
    m_cur_Label_temp->setPositionAlignment(Qt::AlignLeft | Qt::AlignVCenter);
    m_cur_Label_power->setPositionAlignment(Qt::AlignLeft | Qt::AlignVCenter);
    //垂直参考线
    m_refer_lineV = new QCPItemStraightLine(ui->widgetPlot);
    m_refer_lineV->setPen(ReferLinePen);

4.定义鼠标事件

用鼠标按下和释放都行

private slots:
    void slot_mouseRelease(QMouseEvent *event);

关联鼠标事件槽函数 

connect(ui->widgetPlot, SIGNAL(mouseRelease(QMouseEvent *)), this, SLOT(slot_mouseRelease(QMouseEvent *)));

定义槽函数,绘制标记点和参考线 

void PlotPanel::slot_mouseRelease(QMouseEvent *event)
{
    //点击坐标
    QPointF CPoint = event->pos();

    //鼠标点击的后屏幕位置转换到下坐标轴对应坐标
    int16_t cur_x = ui->widgetPlot->xAxis->pixelToCoord(CPoint.x());

    //标记点和标签
    double y_value = m_vect_temp_all.at(cur_x);                     //获取曲线在X轴位置的Y值
    m_tracer_temp->setGraphKey(cur_x);                              //设置标记点X轴的位置
    m_tracer_temp->position->setCoords(cur_x, y_value);             //设置标记点在位置
    m_cur_Label_temp->position->setCoords(0, 10);                   //设置标签显示偏移位置
    m_cur_Label_temp->setText(QString::number(y_value, 'f', 1));    //设置标签的值

    double y_value1 = m_vect_power_all.at(cur_x);
    m_tracer_power->setGraphKey(cur_x);
    m_tracer_power->position->setCoords(cur_x, y_value1);
    m_cur_Label_power->position->setCoords(0, 10);
    m_cur_Label_power->setText(QString::number(y_value1, 'f', 1));

    //垂直参考线,就是两点一线
    m_refer_lineV->point1->setCoords(cur_x, y_value);               //设置点1的值
    m_refer_lineV->point2->setCoords(cur_x, y_value1);              //设置点2的值

    ui->widgetPlot->replot();
}

  • 6
    点赞
  • 53
    收藏
    觉得还不错? 一键收藏
  • 5
    评论
Qt中,可以使用QCustomPlot库来绘制多曲线图,并且也可以实现曲线图例的选中功能。 QCustomPlot是一个非常强大的Qt绘图库,它提供了丰富的绘图功能,可以绘制曲线、散点、柱状图等。要实现多曲线图例的选中功能,可以按照以下步骤进行操作: 首先,我们需要创建一个QCustomPlot对象,并向其添加曲线。可以使用addGraph()函数添加多个曲线。例如: QCustomPlot *customPlot = new QCustomPlot(this); QCPGraph *graph1 = customPlot->addGraph(); QCPGraph *graph2 = customPlot->addGraph(); ... 然后,我们需要为每个曲线指定一个名称,以便在图例中显示。可以使用graph1->setName()函数为曲线1指定名称,graph2->setName()函数为曲线2指定名称,以此类推。 graph1->setName("曲线1"); graph2->setName("曲线2"); ... 接下来,我们需要显示图例。可以使用customPlot->legend->setVisible()函数将图例设置为可见,并根据需要设置图例的位置。 customPlot->legend->setVisible(true); customPlot->axisRect()->insetLayout()->setInsetAlignment(0, Qt::AlignBottom | Qt::AlignRight); 最后,我们需要设置曲线图例的选中功能。可以使用graph1->setSelectable()函数将曲线1设置为可选中,并根据需要设置曲线的选中状态。 graph1->setSelectable(QCP::stSingleData); graph1->setSelection(QCPDataSelection(graph1->data()->dataRange())); 通过以上步骤,我们可以实现曲线图例的选中功能。当图例中的某个曲线被选中时,可以通过相应的槽函数来处理选中事件,例如: connect(customPlot, &QCustomPlot::selectionChangedByUser, this, &MyClass::handleSelectionChanged); void MyClass::handleSelectionChanged() { // 处理选中事件 // 例如,可以根据选中的曲线来进行相应的操作 QCPDataSelection selection = customPlot->graph(0)->selection(); if (!selection.isEmpty()) { // 曲线1被选中 } else { // 曲线1未被选中 } } 以上就是使用QCustomPlot库实现多曲线图例选中的简单示例。通过设置曲线的名称、显示图例并设置选中功能,我们可以轻松地实现这个功能。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 5
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值