How to convert seconds (double) to QDateTime in Qt?

8k views Asked by At

I have a number of seconds stored in a double format (but they are integer values if that's to be concerned). I would like to convert them to a hh:mm:ss format string. How to do that?

For example double secs = 120; would be 00:02:00.

3

There are 3 answers

3
Bowdzone On BEST ANSWER

You can create a QTime object by using its default constructor and then add your seconds to it:

double secs = 120;

QTime a(0,0,0);
a = a.addSecs(int(secs));
5
synacker On
QTime time;
time = time.addSecs((int)secs);
qDebug() << time.toString("hh:mm:ss");
1
vahancho On

Why can't you convert seconds into hours, mins and seconds?

double secsDouble = 3666.0; // 1 hour, 1 min, 6 seconds.
int secs = (int)secsDouble;

int h = secs / 3600;
int m = ( secs % 3600 ) / 60;
int s = ( secs % 3600 ) % 60;

QTime t(h, m, s);
qDebug() << time.toString("hh:mm:ss");