QCustomPlot Discussion and Comments

Problem with current time plottingReturn to overview

Can anyone help me..
My project is to get a serial data and plot with current time using qcustomplot . So the problem is , I fetch the current time but when I give it to plot it's repeatedly show the starting time. If I connect the time ticker with timeout signal , then all the ticklabel time changes at same time. So I want the tick label show the current time incrementing forwardly . I am trying also to set the tick step to increment with time by calling current time at beginning but it shows the error like " error: 'setAutoTickStep' is not a member of 'QCPAxis' ".
Please help me.

 #include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QHBoxLayout>
#include <QMessageBox>
#include <QFrame>
#include <QRect>
#include <QTimer>
#include <QDebug>
#include <QDateTime>
#include <QtSerialPort/QSerialPort>
#include <QtSerialPort/QSerialPortInfo>

unsigned int s1=0,s2=0,s3=0;

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


init_port();
init_plot();


//dataTimer = new QTimer;
//connect(dataTimer, SIGNAL(timeout()), this, SLOT(init_plot()));
//dataTimer->start(0);
QTimer *t = new QTimer();
connect(t, SIGNAL(timeout()), this, SLOT(realtimeDataSlot()));
t->start(10);
connect(serial, SIGNAL(readyRead()), this, SLOT(receive()));

}

MainWindow::~MainWindow()
{
delete ui;
serial->close();
}
void MainWindow::init_port()
{
QTextStream out(stdout);
QString portname = "/dev/tty0";
serial = new QSerialPort(portname);
if (!serial->open(QIODevice::ReadWrite))
{
QMessageBox::warning(this, "Error", "cannot open port");
}

serial->setBaudRate(QSerialPort::Baud9600);
serial->setFlowControl(QSerialPort::NoFlowControl);
serial->setParity(QSerialPort::NoParity);
serial->setDataBits(QSerialPort::Data8);
serial->setStopBits(QSerialPort::OneStop);
out <<"port opened"<<endl<<endl;
}

void MainWindow::init_plot()
{

  //   plot = new QCustomPlot (this);

    ui->plot->xAxis->setLabel("t [hh:mm:ss]");
    ui->plot->yAxis->setLabel("V [mV]");

    ui->plot->addGraph(); // blue line
    ui->plot->graph(0)->setPen(QPen(Qt::blue));
   // ui->plot->addGraph(); // red line
    //ui->plot->graph(1)->setPen(QPen(Qt::red));
    //ui->plot->addGraph(); // green line
   // ui->plot->graph(2)->setPen(QPen(Qt::green));


    QSharedPointer<QCPAxisTickerTime> timeTicker(new QCPAxisTickerTime);
    QDateTime ti = QDateTime::currentDateTime();
    QString tim = ti.toString("hh:mm:ss");
    timeTicker->setTimeFormat(tim);
    ui->plot->xAxis->setTicker(timeTicker);


    ui->plot->axisRect()->setupFullAxesBox();
    ui->plot->yAxis->setRange(0, 100);

    // make left and bottom axes transfer their ranges to right and top axes:
    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)));

    // setup a timer that repeatedly calls MainWindow::realtimeDataSlot:


}

void MainWindow::realtimeDataSlot()
{

    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 1s
    {

      // add data to lines:
      ui->plot->graph(0)->addData(key, s1);
      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();
    

}

void MainWindow::receive()
{
// recieves data as ASCII string
    char buff1[3];

QByteArray bytesRead = serial->readAll();
for (int i = 0; i < bytesRead.size(); i++)
{
            if (bytesRead.at(i))
            {

                buff1[i] << bytesRead[i];

            }
}

s1=atoi(buff1);


//QTextStream out(stdout);//
//out << buff << endl;
addDataPoint(atof(buff1));
}

void MainWindow::addDataPoint(double datapoint)
{

if (x_position>250)data_x.pop_front();
double ms = timer->elapsed();
data_x.push_back((double)ms/1000);
x_position++;
if (x_position>250) data_y.pop_front();
data_y.push_back(datapoint);


ui->plot->graph(0)->setData(data_x,data_y);
ui->plot->graph(0)->rescaleAxes();
ui->plot->replot();

}

This is not at all the way i would get the time. you should not be using static at all for this.

just grab QTime::currentTime and then use msecsSinceStartOfDay/1000.0 to get the seconds. dont touch the 'elapsed' function and dont use static. There is also no point in doing your check if you just set your realtimeDataSlot to be called every second.

as for showing the time since the start of demo instead of the start of day, just store the msecsSinceStartOfDay as a member variable.

here is my output
https://drive.google.com/open?id=1b5HSD2ZjlOz3enznFSCIwgd5oPyO_7R1
i want the time update only for forward direction not update all label at same time.

Thank you Mr. lan , i did exactly

  QTime time=QTime::currentTime();
   double key = time.msecsSinceStartOfDay()/1000.0; 
    static double lastPointKey = 0;

but no changes in result.
https://drive.google.com/open?id=1JL7f-W_r4nEj2RXy8A5Gice6ZyuhYCuv
all label time changes at same time but my requirement is only the time change in forward direction. please help..