Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Getting milliseconds accuracy current time in Qt

Tags:

c++

qt

qtime

Qt documentation about QTime::currentTime() says :

Note that the accuracy depends on the accuracy of the underlying operating system; not all systems provide 1-millisecond accuracy.

But is there any way to get this time with milliseconds accuracy in windows 7?

like image 843
HMD Avatar asked Jul 30 '15 19:07

HMD


People also ask

How do I get the current time in Qt?

Show activity on this post. Create a QTimer with 1 sec interval (or e.g. 100 msec for more accuracy), connect its timeout signal to your slot. In the slot get the current time using QTime::currentTime() static function, convert it to string using toString and assign it to a GUI element (e.g. a label).

How do I display current date and time in Qt?

[static] QDateTime QDateTime::currentDateTimeUtc() Returns the current datetime, as reported by the system clock, in UTC. See also currentDateTime(), QDate::currentDate(), QTime::currentTime(), and toTimeSpec().

How do I disable QElapsedTimer?

You can't stop QElapsedTimer , because there is no timer. When you call method start() , QElapsedTimer saves the current time. When elapsed, it gets current time again, and calculate difference.


2 Answers

you can use the functionality provided by time.h header file in C/C++.

#include <time.h> 
clock_t start, end; 
double cpu_time_used; 
int main()
{
    start = clock();
    /* Do the work. */ 
    end = clock(); 
    cpu_time_used = ((double)(end-start)/ CLOCKS_PER_SEC);
}
like image 191
Anurag Singh Avatar answered Oct 18 '22 22:10

Anurag Singh


You can use QDateTime class and convert the current time with the appropriate format:

QDateTime::currentDateTime().toString("yyyy/MM/dd hh:mm:ss,zzz")

where 'z' corresponds to miliseconds accuracy.

like image 24
Ritchy MFinda Avatar answered Oct 18 '22 20:10

Ritchy MFinda