QCustomPlot Discussion and Comments

I have a problem in real time data plottingReturn to overview

hi sir, i am sudhakar, actually i have a problem in real time data plotting. i copied code from http://www.qcustomplot.com/index.php/demos/realtimedatademo this page. everything is ok than dataTimer. it is saying (issue is)

C:\Users\DEBEL\Desktop\Projects\Qt2\serialtest\mainwindow.cpp:42: error: 'dataTimer' was not declared in this scope
     connect(&dataTimer,SIGNAL(timeout()),this,SLOT(realtimedata()));. 

i dont know how to resolve this issue. i hope u people will make me reply with solution.

this is my code

 ui->Plot->addGraph(); // blue line
    ui->Plot->graph(0)->setPen(QPen(QColor(40, 110, 255)));
    QSharedPointer<QCPAxisTickerTime> timeTicker(new QCPAxisTickerTime);
    timeTicker->setTimeFormat("%h:%m:%s");
    ui->Plot->xAxis->setTicker(timeTicker);
    ui->Plot->axisRect()->setupFullAxesBox();
    ui->Plot->yAxis->setRange(-1.2, 1.2);
    // make left and bottom axes transfer their ranges to right and top axes:
    connect(ui->Plot->xAxis,SIGNAL(rangeChanged(QCPRange)),SLOT(setRange(QCPRange)));
    connect(ui->Plot->yAxis,SIGNAL(rangeChanged(QCPRange)),SLOT(setRange(QCPRange)));
    // setup a timer that repeatedly calls MainWindow::realtimeDataSlot:
    connect(&dataTimer,SIGNAL(timeout()),this,SLOT(realtimedata()));
    dataTimer.start(0); // Interval 0 means to refresh as fast as possible

this is my slot
void MainWindow::realtimedata()
{
    static QTime time(QTime::currentTime());
    // calculate two new data points:
    double key = time.elapsed()/1000.0; // time elapsed since start of demo, in seconds
    static double lastPointKey = 0;
    if (key-lastPointKey > 0.002) // at most add point every 2 ms
    {
      // add data to lines:
      ui->Plot->graph(0)->addData(key, qSin(key)+qrand()/(double)RAND_MAX*1*qSin(key/0.3843));
      //ui->Plot->graph(1)->addData(key, qCos(key)+qrand()/(double)RAND_MAX*0.5*qSin(key/0.4364));
      // rescale value (vertical) axis to fit the current data:
      //ui->customPlot->graph(0)->rescaleValueAxis();
      //ui->customPlot->graph(1)->rescaleValueAxis(true);
      lastPointKey = key;
    }
    // make key axis range scroll with the data (at a constant range size of 8):
    ui->Plot->xAxis->setRange(key, 8, Qt::AlignRight);
    ui->Plot->replot();

    // calculate frames per second:
    static double lastFpsKey;
    static int frameCount;
    ++frameCount;
    if (key-lastFpsKey > 2) // average fps over 2 seconds
    {
      ui->statusBar->showMessage(
            QString("%1 FPS, Total Data points: %2")
            .arg(frameCount/(key-lastFpsKey), 0, 'f', 0)
            .arg(ui->Plot->graph(0)->data()->size())
            , 0);
      lastFpsKey = key;
      frameCount = 0;
    }

}

R.Sudhakar
Thanks and Regards

Just doing a quick search on this forum and you would have see that you're not the first one with this problem...

You need to instantiate a QTimer with the name dataTimer... It isnt in the code !
(You maybe need to add #include <QTimer> in you .h file if the compiler say it doesn't know what's a QTimer)

QTimer dataTimer;
dataTimer.setSingleShot(false);

Thank you a lot sir... now its working.. but again it has a problem. my project is running but it is not plotting the real time graph.

here the problem
QObject::connect: No such slot MainWindow::setRange(QCPRange) in SerialPortData\mainwindow.cpp:32
QObject::connect: (receiver name: 'MainWindow')
QObject::connect: No such slot MainWindow::setRange(QCPRange) in SerialPortData\mainwindow.cpp:33
QObject::connect: (receiver name: 'MainWindow')


My Main Code

//Graph plotting
    ui->Plot->addGraph(); // blue line
       ui->Plot->graph(0)->setPen(QPen(QColor(40, 110, 255)));
       QSharedPointer<QCPAxisTickerTime> timeTicker(new QCPAxisTickerTime);
       timeTicker->setTimeFormat("%h:%m:%s");
       ui->Plot->xAxis->setTicker(timeTicker);
       ui->Plot->axisRect()->setupFullAxesBox();
       ui->Plot->yAxis->setRange(-100, 100);
       // make left and bottom axes transfer their ranges to right and top axes:
       connect(ui->Plot->xAxis,SIGNAL(rangeChanged(QCPRange)),SLOT(setRange(QCPRange)));
       connect(ui->Plot->yAxis,SIGNAL(rangeChanged(QCPRange)),SLOT(setRange(QCPRange)));
       // setup a timer that repeatedly calls MainWindow::realtimeDataSlot:
       QTimer dataTimer;
       connect(&dataTimer,SIGNAL(timeout()),this,SLOT(realtimedata()));
       dataTimer.start(0); // Interval 0 means to refresh as fast as possible


My slot

void MainWindow::realtimedata()
{
    static QTime time(QTime::currentTime());
        // calculate two new data points:
        double key = time.elapsed()/1000.0; // time elapsed since start of demo, in seconds
        static double lastPointKey = 0;
        if (key-lastPointKey > 0.002) // at most add point every 2 ms
        {
          // add data to lines:
          ui->Plot->graph(0)->addData(key, qSin(key)+qrand()/(double)RAND_MAX*1*qSin(key/0.3843));
          //ui->Plot->graph(1)->addData(key, qCos(key)+qrand()/(double)RAND_MAX*0.5*qSin(key/0.4364));
          // rescale value (vertical) axis to fit the current data:
          //ui->customPlot->graph(0)->rescaleValueAxis();
          //ui->customPlot->graph(1)->rescaleValueAxis(true);
          lastPointKey = key;
        }
        // make key axis range scroll with the data (at a constant range size of 8):
        ui->Plot->xAxis->setRange(key, 8, Qt::AlignRight);
        ui->Plot->replot();

        // calculate frames per second:
        static double lastFpsKey;
        static int frameCount;
        ++frameCount;
        if (key-lastFpsKey > 2) // average fps over 2 seconds
        {
          ui->statusBar->showMessage(
                QString("%1 FPS, Total Data points: %2")
                .arg(frameCount/(key-lastFpsKey), 0, 'f', 0)
                .arg(ui->Plot->graph(0)->data()->size())
                , 0);
          lastFpsKey = key;
          frameCount = 0;
       }
}

i dont know how to set the value of real time data for plotting a graph.
i had my image here
https://drive.google.com/open?id=135X0yYAELZQqO60f4_rC2hVPfvlyZ21m
Please open,see and help me to resolve
i hope u all make me understand...

Thanks in advance
R.Sudhakar

You have a problem on your line 10 & 11

connect(ui->Plot->xAxis,SIGNAL(rangeChanged(QCPRange)),SLOT(setRange(QCPRange)));
connect(ui->Plot->yAxis,SIGNAL(rangeChanged(QCPRange)),SLOT(setRange(QCPRange)));

You didn't declare who instantiated the functions/slot setRange(QCPRange) so the compiler thinks that this slot is part of this / your MainWindow which doesn't have any slot setRange(QCPRange)

If you look closely to the example you'll see that you need to give the "2nd axis" to make sure both top/down axis are the same, same things with right/left axis

so both line should instead be :

connect(ui->Plot->xAxis, SIGNAL(rangeChanged(QCPRange)), ui->Plot->xAxis2, SLOT(setRange(QCPRange)));
connect(ui->Plot->yAxis, SIGNAL(rangeChanged(QCPRange)), ui->Plot->yAxis2, SLOT(setRange(QCPRange)));

this should get rid of the problems

Sir now i added that second axis too but my graph is not plotting.

like this i added.

connect(ui->Plot->xAxis, SIGNAL(rangeChanged(QCPRange)), ui->Plot->xAxis2, SLOT(setRange(QCPRange)));
connect(ui->Plot->yAxis, SIGNAL(rangeChanged(QCPRange)), ui->Plot->yAxis2, SLOT(setRange(QCPRange)));

i give image here
https://drive.google.com/open?id=1XF-On8hSeq5c6m2wHs4y3jdCrZtxGBiX
no error is come.. but my graph is not plotting.
i hope u will help me till graph plot.

Thank you sir.

Line 13 should'nt be commented...

sir i removed the comment. but still it didnt plot the graph.

Try debugging.....

- Is your slot is really called ? put a breakpoint in it and see what happen...

- what's happening in you main function... do you have a MainWindow, does QTimer get out of scope... does your function just finished after the initialization of your variables.....

Guess you'll need to improve your debugging skills / C++ basics