QDateTimeAxis always shows 1970



  • Hi there

    I try to draw a chart with a QDateTimeAxis but it gets always filled with the default date 01.01.1970. I read the dates and the values by a function (updateValues) from an csv file into a struct that fills then a QList of the same struct type.
    Thats a sample line of the csv:

    2017-03-11 07:50:01,17.7,1026.2,2.9,99.9
    

    mainwindow.h:

    #ifndef MAINWINDOW_H
    #define MAINWINDOW_H
    #include <QMainWindow>
    #include <QList>
    #include <QtCharts>
    using namespace QtCharts;
    
    struct Wstruct{
    	QDateTime date;
    	double press;
    };
    
    class MainWindow:public QMainWindow
    {
    	Q_OBJECT
    public:
    	explicit MainWindow(QWidget *parent = 0);
    	~MainWindow();
    private:
    	QLineSeries *seriesPressure;
    	QChart *chart;
    	QDateTimeAxis *axisX;
    	QValueAxis *axisY;
    	QChartView *chartView;
    	QList<Wstruct> values;
    	Wstruct filler;
    	QWidget *widgetChart;
    private slots:
    	void updateValues();
    };
    
    #endif // MAINWINDOW_H
    
    

    mainwindow.cpp:

    #include "mainwindow.h"
    #include <QFile>
    #include <QDebug>
    #include <QTime>
    using namespace QtCharts;
    
    MainWindow::MainWindow(QWidget *parent) :
    	QMainWindow(parent)
    {
    	this->setFixedSize(800,480);
    	
    	seriesPressure = new QLineSeries();
    	chart = new QChart();
    	chart->legend()->hide();
    	chart->addSeries(seriesPressure);
    	axisX = new QDateTimeAxis;
    	axisX->setFormat("hh:mm dd.MM.yyyy");
    	chart->addAxis(axisX,Qt::AlignBottom);
    	axisY = new QValueAxis;
    	chart->addAxis(axisY,Qt::AlignRight);
    	seriesPressure->attachAxis(axisX);
    	seriesPressure->attachAxis(axisY);
    	chartView = new QChartView(chart);
    	
    	widgetChart = new QWidget(this);
    	widgetChart->setGeometry(15,15,760,320);
    	QGridLayout *gl = new QGridLayout(widgetChart);
    	gl->addWidget(chartView);
    	updateValues();
    }
    
    MainWindow::~MainWindow()
    {
    }
    
    void MainWindow::updateValues()
    {
    	QFile file("289_24h.log");
    	if (!file.open(QIODevice::ReadOnly)) {
    		qDebug() << file.errorString();
    	}
             // clear previous updates
    	seriesPressure->clear();
    	values.clear();
    	
    	while (!file.atEnd()) {
    		QByteArray line = file.readLine();
    		filler.date = QDateTime::fromString(line.split(',').at(0),"yyyy-MM-dd hh:mm:ss");
    		filler.press = line.split(',').at(2).toDouble();
    		values.append(filler);
    	} 
    	
    	for (int i=0; i<values.size();i++)
    	{
    		seriesPressure->append(values.at(i).date.toMSecsSinceEpoch(),values.at(i).press);	
    		qDebug()<<values.at(i).date.toMSecsSinceEpoch()<<values.at(i).press;
    	}
    }
    
    

    After I append the values to the list I did a qDebug to see if the values for the date and the y values are correctly stored in the list but it seems the are proper stored. But why does the QDateTimeAxis always plot 1970?
    Any idea anyone?



  • @pauledd

    hi,

    I believe 1970 is the default time.
    Therefore I think

    QDateTime::fromString(line.split(',').at(0),"yyyy-MM-dd hh:mm:ss")
    

    results in an invalid QDateTime -> 1970. Can you check if that conversion from String to QdateTime results in a valid and correct date?



  • yes it seems, the line:

    		qDebug()<<values.at(i).date.toMSecsSinceEpoch()<<values.at(i).press;
    
    

    outputs this:

    ...
    1489215301000 1026.1
    1489215601000 1026.1
    ...
    

    and thats msecsSinceEpoch that QDateTimeAxis expects.



  • ok I think I solved it. It was all correct, the data was in the chart and the conversion was valid but it simply displayed the wrong range. I believe thats because if you explicitly specify an Axis instead of creating createDefaultAxes() you have to specify the range manually with axisX->setMin or axisX->setRange, like in my case for the QDateTimeAxis. I had to:

    axisX->setMin(values.first().date);
    axisX->setMax(values.last().date);
    

    to get the proper range to display.


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.