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 starts in default date time (01-01-1970 01:00)
Forum Updated to NodeBB v4.3 + New Features

QDateTimeAxis starts in default date time (01-01-1970 01:00)

Scheduled Pinned Locked Moved Unsolved General and Desktop
2 Posts 2 Posters 195 Views 1 Watching
  • 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.
  • D Offline
    D Offline
    diego-qt
    wrote on last edited by
    #1

    Hello, I'm trying to plot a QChart with date-time horizontal axis and value vertical axis. I'm having trouble with the horizontal axis as I can't make it start with the current time. Here is my code:

    Chart::Chart(): QWidget(), axisX(new QDateTimeAxis), axisY(new QValueAxis),
    chart(new QChart), chartView(new QChartView)
    {
        chart->addSeries(lineSeries);
        chart->legend()->hide();
    
        //format the time axis
        axisX->setFormat("dd.MM.yyyy HH:mm");
        axisX->setTitleText("Time");
        axisX->setLabelsAngle(-90);
        QDateTime timeValue(QDate::currentDate(), QTime::currentTime());
        axisX->setRange(timeValue, timeValue);
        chart->addAxis(axisX, Qt::AlignBottom);
        lineSeries->attachAxis(axisX);
        qDebug() << timeValue;
    
        //format the temperature axis
        axisY->setLabelFormat("%i");
        axisY->setTitleText("Temperature (°C)");
        axisY->setTickInterval(5);
        axisY->setRange(0, 40);
        chart->addAxis(axisY, Qt::AlignLeft);
        lineSeries->attachAxis(axisY);
    
        //place the chart in the window
        QVBoxLayout *layout = new QVBoxLayout(this);
        layout->addWidget(chartView, 0);
        resize(500, 500);
    }
    

    This shows a chart where the range is the default date time 01-01-1970 01:00, even thought qDebug() tells me that timeValue is set to the current time. How can I solve this?

    One possible solution is in the function that adds a real value to the series (with lineSeries.append(...)) and updates the max value of the horizontal axis.

    void Chart::addValueToChart(qreal newTempValue)
    {
        QDateTime current(QDate::currentDate(), QTime::currentTime());
        lineSeries->append(current.toMSecsSinceEpoch(), newTempValue);
        axisX->setMax(current);
    }
    

    I could be able to update the min value of this axis in this function too, but I think that updating it every single time would be a waste of time. The ideal would be to set the min date-time value one time. Would that be possible?

    1 Reply Last reply
    0
    • SGaistS Offline
      SGaistS Offline
      SGaist
      Lifetime Qt Champion
      wrote on last edited by
      #2

      Hi,

      Just to check, what happens if you configure your axis after you set it on the chart ?

      Interested in AI ? www.idiap.ch
      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

      1 Reply Last reply
      0

      • Login

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