I just had the same problem in one of my applications. With a small trick it is possible to use the toString
method in QTime
. It needs to be considered, that toString
also takes care of the locale and allows for different time formats. This makes it a non-trivial function to implement.
The following complete code should be enough to explain the basic idea:
#include <QApplication>
#include <QTime>
#include <QTimer>
#include <QEventLoop>
#include <QDebug>
int main(int argc, char** args) {
QApplication app(argc, args);
QTime x;
x.start();
QTimer wait;
QEventLoop e;
QTimer::singleShot(500, &e, &QEventLoop::quit);
e.exec();
QTime y(0, 0);
qDebug() << x.elapsed();
y=y.addMSecs(x.elapsed());
qDebug() << y.toString("mm:ss.zzz");
}