Qt Customplot关于情节变化

时间:2017-05-02 06:10:53

标签: c++ qt qcustomplot

我正在向函数plotMainGraph(double serData)发送一个值,因此它会绘制每次发送的值。举个例子,我只是连续发送值5和25,但是我得到的情节不正确,即使我每次发送5和25。该图可以保持在25的值并且突然减少到5.有时该图在一段时间内保持恒定在5,然后突然增加到25然后再回到5.

我希望情节如上所述是统一的:它应该以恒定的速率绘制5和25,但这不会发生。

这是我的代码:

       ui->customPlot->setInteractions(QCP::iRangeDrag | QCP::iRangeZoom | QCP::iSelectAxes |
                                       QCP::iSelectLegend | QCP::iSelectPlottables);
       ui->customPlot->xAxis->setRange(0, 20);
       ui->customPlot->yAxis->setRange(-5, 5);



       /*customPlot->plotLayout()->insertRow(0);
       QCPTextElement *title = new QCPTextElement(customPlot, "Interaction Example", QFont("sans", 17, QFont::Bold));
       customPlot->plotLayout()->addElement(0, 0, title);*/

      // ui->customPlot->xAxis->setLabel("Time(in Second)");
      // ui->customPlot->yAxis->setLabel("Pressure");
       //ui->customPlot->legend->setVisible(true);
       QFont legendFont = font();
       legendFont.setPointSize(10);
       ui->customPlot->legend->setFont(legendFont);
       ui->customPlot->legend->setSelectedFont(legendFont);
       ui->customPlot->legend->setSelectableParts(QCPLegend::spItems); // legend box shall not be selectable, only legend items



       //background color code extra

       ui->customPlot->setBackground(QColor("lightgrey"));
       //till here

       ui->customPlot->addGraph(); // blue line
       ui->customPlot->graph(0)->setPen(QPen(QColor("yellow"),3));//

       QSharedPointer<QCPAxisTickerTime> timeTicker(new QCPAxisTickerTime);
       timeTicker->setTimeFormat("%h:%m:%s");
       ui->customPlot->xAxis->setTicker(timeTicker);
   //    ui->customPlot->axisRect()->setupFullAxesBox();
       ui->customPlot->yAxis->setRange(-5, 25);//

       // make left and bottom axes transfer their ranges to right and top axes:
       connect(ui->customPlot->xAxis, SIGNAL(rangeChanged(QCPRange)), ui->customPlot->xAxis2, SLOT(setRange(QCPRange)));
       connect(ui->customPlot->yAxis, SIGNAL(rangeChanged(QCPRange)), ui->customPlot->yAxis2, SLOT(setRange(QCPRange)));

void MainWindow::plotMainGraph(double serData)
   {
       connect(&dataTimer, &QTimer::timeout, [this,serData]()
       {
           static QTime time(QTime::currentTime());
           double key = time.elapsed() / 1000;
           static double lastPointKey = 0;
           if (key - lastPointKey > 0.01)
           {

               double value = serData;
               ui->customPlot->graph(0)->addData(key, value);
               lastPointKey = key;
           }
           ui->customPlot->xAxis->setRange(key, 30, Qt::AlignRight);
           ui->customPlot->replot();
       });
   }
  ui->customPlot->setInteractions(QCP::iRangeDrag | QCP::iRangeZoom | QCP::iSelectAxes |QCP::iSelectLegend | QCP::iSelectPlottables);
       ui->customPlot->xAxis->setRange(0, 20);
       ui->customPlot->yAxis->setRange(-5, 5);

0 个答案:

没有答案