QCustomPlot Discussion and Comments

Real time plotting problemReturn to overview

Hi there,

Thanks for this amazing library. I'm net to QT and I was trying to use this tool to plot some real time data.

I followed the basic plotting example and it worked, then I tried to the real-time by following this https://www.qcustomplot.com/index.php/demos/realtimedatademo

Sadly, it did not work, I get an empty graph, the axes are displayed but nothing is drawn on it.


MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
    , ui(new Ui::MainWindow)
{
    ui->setupUi(this);


    MainWindow:: makePlot();
}

MainWindow::~MainWindow()
{
    delete ui;
}


void delay(int n)
{
    QTime dieTime= QTime::currentTime().addSecs(n);
    while (QTime::currentTime() < dieTime)
        QCoreApplication::processEvents(QEventLoop::AllEvents, 100);
}


void MainWindow::makePlot(){
    // generate some data:

    QTimer dataTimer;
//QTimer *dataTimer = new QTimer(this);


    dataTimer.setSingleShot(false);
    ui->customPlot->addGraph(); // blue line
    ui->customPlot->graph(0)->setPen(QPen(QColor(40, 110, 255)));
    ui->customPlot->addGraph(); // red line
    ui->customPlot->graph(1)->setPen(QPen(QColor(255, 110, 40)));

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

    // 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)));

    // setup a timer that repeatedly calls MainWindow::realtimeDataSlot:
    std::cout<<"looped in for 1"<<std::endl;
    connect(&dataTimer, SIGNAL(timeout()), this, SLOT(realtimeDataSlot()));
    std::cout<<"looped in for 2"<<std::endl;
    dataTimer.start(0); // Interval 0 means to refresh as fast as possible
    std::cout<<"looped in for 3"<<std::endl;


}





void MainWindow::realtimeDataSlot()
{
cout<<"test string"<<endl;
/*the code from the demo page*/}





I added a cout in realtimeDataSlot as you can see and that "test string" never appears, which means the functions is never called...

Could some please help me with this?

Thanks in advance!

well the first thing i see is that your timer 'dataTimer' is being deleted at the end of the 'makePlot' function. when it gets deleted, it wont process any of the timer/signals you've created. i suggest moving the timer to a class member.

Hi Moka,

In the makePlot() function, you have declared QTimer dataTimer; on the stack, so it gets destroyed after method execution. Try defining it on the heap and it should work.