Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. QDateTimeAxis always shows 1970

QDateTimeAxis always shows 1970

Scheduled Pinned Locked Moved Solved General and Desktop
4 Posts 2 Posters 1.8k Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • pauleddP Offline
    pauleddP Offline
    pauledd
    wrote on last edited by
    #1

    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?

    J.HilkJ 1 Reply Last reply
    0
    • pauleddP pauledd

      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?

      J.HilkJ Offline
      J.HilkJ Offline
      J.Hilk
      Moderators
      wrote on last edited by
      #2

      @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?


      Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct


      Q: What's that?
      A: It's blue light.
      Q: What does it do?
      A: It turns blue.

      1 Reply Last reply
      0
      • pauleddP Offline
        pauleddP Offline
        pauledd
        wrote on last edited by
        #3

        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.

        1 Reply Last reply
        0
        • pauleddP Offline
          pauleddP Offline
          pauledd
          wrote on last edited by pauledd
          #4

          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.

          1 Reply Last reply
          1

          • Login

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • Users
          • Groups
          • Search
          • Get Qt Extensions
          • Unsolved