QCustomPlot Discussion and Comments

v1.3.2 - Plotted Time 1 Hour Less Than It Should BeReturn to overview

I have this code:

QDateTime currentTime = QDateTime::currentDateTime();
currentTime = currentTime.toUTC();
int currentTimeInSeconds = currentTime.time().hour() * 3600 + currentTime.time().minute() * 60 + currentTime.time().second();
double currentTimeInSecondsFloat = (double) currentTimeInSeconds;
this->ui->Chart->xAxis->setLabel("Time (h:mm:ss)");
this->ui->Chart->graph(0)->addData(currentTimeInSecondsFloat, currentTimeInSecondsFloat + 3600);

The problem is that the x axis of the chart shows times that are 1 hour less than the actual time. For example, if the time is now 12:57:00, the axis would show 11:57:00. Is there any reason why this would happen?

i think it is an issue with your conversion... i dont know why you are doing all this extra math instead of just taking the number of milliseconds that currentMsechSinceEpoch() gives and dividing by 1000.0.

Also, there is a function QDateTime::currentMsecsSinceEpoch(), which would give you what your first two lines give.

(pretty sure your line 3 is what is causing the problem).

i would replace your first 4 lines with:

double currentTimeInSecondsFloat=((double)QDateTime::currentMsechSinceEpoch())/1000.0

This is only a snapshot of my entire code. I use another function that requires a QTime variable alongside this code snippet. Anyway, I figured it out. It has to do with Daylight Savings Time. To fix it, I used:

double dataValue = 100.0;
QDateTime currentTime = QDateTime::currentDateTime();
printFunctionWithQTime(currentTime.time()) //prints the correct time

                    currentTime = currentTime.toUTC();
                { currentTime = currentTime.toUTC(); }
currentTimeInSeconds = currentTime.time().hour() * 3600 + currentTime.time().minute() * 60 + currentTime.time().second();
currentTimeInSecondsFloat = (double) currentTimeInSeconds;
this->ui->Chart->graph(0)->addData(currentTimeInSecondsFloat, dataValue); //only plots correct time when above if statement is used