2

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.

halfer
  • 19,824
  • 17
  • 99
  • 186
Łukasz Przeniosło
  • 2,725
  • 5
  • 38
  • 74

3 Answers3

9

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));
Bowdzone
  • 3,827
  • 11
  • 39
  • 52
1

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");
vahancho
  • 20,808
  • 3
  • 47
  • 55
0
QTime time;
time = time.addSecs((int)secs);
qDebug() << time.toString("hh:mm:ss");
synacker
  • 1,722
  • 13
  • 32