【问题标题】:Bad Values in QDateTimeAxis (QtCharts)QDateTimeAxis (QtCharts) 中的错误值
【发布时间】:2017-07-26 12:04:24
【问题描述】:

我正在使用 QtCharts 来显示模拟数据。模拟从零时间开始,但我的图表轴似乎总是从 19 小时开始。这让我很困惑。图表的设置很简单:

std::vector<SimData> data;

// ... Populate data

auto series = new QLineSeries();

for(auto i : data)
{
  // Append time in milliseconds and a value
  series->append(i.msTime, i.value);
}

this->legend()->hide();

this->addSeries(series);

this->axisX = new QDateTimeAxis;
this->axisX->setTickCount(10);
this->axisX->setFormat("HH:mm:ss");
this->axisX->setTitleText("Sim Time");
this->axisX->setMin(QDateTime());
this->addAxis(this->axisX, Qt::AlignBottom);
series->attachAxis(this->axisX);

this->axisY = new QValueAxis;
this->axisY->setLabelFormat("%i");
this->axisY->setTitleText(x->getID().c_str());
this->addAxis(this->axisY, Qt::AlignLeft);
series->attachAxis(this->axisY);

如果我在没有数据的情况下运行,但只显示图表,我会得到:

如果我添加数据,从时间 0 开始,数据总量是正确的,但时间仍然从 19:00:00 开始。为什么时间不是从 00:00:00 开始?

【问题讨论】:

  • 尝试删除this-&gt;axisX-&gt;setMin(QDateTime());
  • 谢谢。我确实尝试删除“setMin”调用,但没有效果。我最初添加它是为了尝试解决问题,但也没有效果。

标签: c++ qt qdatetime qtcharts


【解决方案1】:

我相信这是因为您在东海岸 (UTC-5),因此 0 表示 UTC-5 的凌晨 12 点 (2400),0ms 表示提前 5 小时(前一天的 1900)。我遇到了同样的问题,将我的时区设置为 UTC(在 ubuntu 下)并且 voila 轴从 0 小时而不是 19 小时开始。

【讨论】:

  • 我确定您对时区的看法是正确的。但不清楚为什么我的计算机的时区与我正在绘制的数据有任何关系。必须有一种程序化的方式来解决这个问题。 (多么可怕的默认值。)
【解决方案2】:

确实确认问题是UTC偏移。 SO有一个很好的例子来说明如何获得UTC偏移量,然后我用它来偏移进入图表的数据:

Easy way to convert a struct tm (expressed in UTC) to time_t type

我由此创建了一个实用函数,用于 QDateTimeAxis 系列数据。

double GetUTCOffsetForQDateTimeAxis()
{
  time_t zero = 24 * 60 * 60L;
  struct tm* timeptr;
  int gmtime_hours;

  // get the local time for Jan 2, 1900 00:00 UTC
  timeptr = localtime(&zero);
  gmtime_hours = timeptr->tm_hour;

  // if the local time is the "day before" the UTC, subtract 24 hours
  // from the hours to get the UTC offset
  if(timeptr->tm_mday < 2)
  {
    gmtime_hours -= 24;
  }

  return 24.0 + gmtime_hours;
}

那么数据转换就简单了。

std::vector<SimData> data;

// ... Populate data

auto series = new QLineSeries();

const auto utcOffset = sec2ms(hours2sec(GetUTCOffsetForQDateTimeAxis()));

for(auto i : data)
{
  // Append time in milliseconds and a value
  series->append(i.msTime - utcOffset, i.value);
}

// ...

【讨论】:

    【解决方案3】:

    对于可能会在这里结束的孤独流浪者。

    我的 KISS 解决方案实际上是设置时间,然后稍等片刻,最后添加一个新数据点:

    for(int i = 0; i <= points; i++) {
        QDateTime timeStamp;
        timeStamp.setDate(QDate(1980, 1, 1));
        timeStamp.setTime(QTime(0, 0, 0));
        timeStamp = timeStamp.addSecs(i);
        data->append(timeStamp.toMSecsSinceEpoch(), /* your y here */);
    }
    

    后来我使用了绘制图表的地方:

    QSplineSeries *temps1 = /* wherever you get your series */;
    QChart *chTemp = new QChart();
    
    tempAxisX->setTickCount(5);
    tempAxisX->setFormat(QString("hh:mm:ss"));
    tempAxisX->setTitleText("Time");
    chTemp->addAxis(tempAxisX, Qt::AlignBottom);
    temps1->attachAxis(tempAxisX);
    

    希望这对未来的访客(包括我自己)有所帮助。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2019-11-26
      • 2021-05-22
      • 1970-01-01
      • 1970-01-01
      • 2021-12-26
      • 2016-12-12
      • 1970-01-01
      相关资源
      最近更新 更多