QCustomPlot Discussion and Comments

If Qt application hangs briefly, realtime plot will be falsified.Return to overview

Hey guys, I have a Qt application which implements the realtime plot example from QCustomplot. When evertything's fine, my plot looks like that the whole time:


But when my application gets stuck for a few seconds, because i.e. too many other applications are runnning (youtube and so on), then the plot gets falsified:


What might be the problem be? Is this maybe a bug? My data points are the following (more or less like the realtime plot example)

void PlotterController::updatePlotsSlot(){
    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_acceleration->graph(0)->addData(key, qSin(key)+qrand()/(double)RAND_MAX*1*qSin(key/0.3843));
        ui->plot_acceleration->graph(1)->addData(key, qCos(key)+qrand()/(double)RAND_MAX*0.5*qSin(key/0.4364));
        ui->plot_acceleration->graph(2)->addData(key, qCos(key)+qrand()/(double)RAND_MAX*0.7*qSin(key/0.4056));
        // rescale value (vertical) axis to fit the current data:

        lastPointKey = key;
    // make key axis range scroll with the data (at a constant range size of 8):
    ui->plot_acceleration->xAxis->setRange(key, 8, Qt::AlignRight);



here is the realtime plot example:


Hmm it's just the way the demo is made. It adds data points whenever your application is able to call the timer slot, and it adds it with the current time as x.

If you want something different you need to program it differently. For example you could generate all data points that need to be created with an even time-spacing to reach the current time.

@isso thank you, but how can this be done? In my example, I have removed the timer slot - my updatePlotsSlot is called everytime new sensor data are available (every second).