如何在 Qt 中将秒数(双精度)转换为 QDateTime?

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

我以双精度格式存储了一些秒数(但如果需要注意的话,它们是整数值)。我想将它们转换为 hh:mm:ss format 字符串。怎么做?

例如 double secs = 120;be 00:02:00.

你可以为它创建一个QTime object by using its default constructor and then add your seconds

double secs = 120;

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

为什么不能将秒转换成小时、分钟和秒?

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");