PDA

View Full Version : QDateTime .setTime problem



sergiofil
28th April 2011, 16:03
Hi, i''m workin with QDateTime and it's happen the following problem:

in mainwindow.h, i put:
QDateTime globalTime;

in mainwindow.cpp, in constructor:
globalTime.setTime(QTime(0,0));
qDebug() << "Time: " << globalTime.toString("mm:ss:zzz") << ";";
----> this debug print: Time: "" ;

I have a timer, that count to 50 ms and do:
globalTime = globalTime.addMSecs(50);
qDebug() << "Time: " << globalTime.toString("mm:ss:zzz") << ";";
------> In this debug, i get:
Time: "" ;
Time: "" ;
(...)
Time: "" ;
Time: "" ;
Time: "00:01:200" ;
Time: "00:01:250" ;
Time: "00:01:300" ;
Time: "00:01:350" ;
Time: "00:01:400" ;
(...)

-----------------------------------------------------------------------------------------------
If in constructor, i switch globalTime.setTime(QTime(0,0)); to globalTime.setTime_t(0); ,
the qDebug() << "Time: " << globalTime.toString("mm:ss:zzz") << ";";
print: Time: "00:00:000" ; and every 50ms i get the time.

So, i would like understand why the first way only show the time after 1:200 seconds.



Bye, Sérgio Silva

AlexSudnik
28th April 2011, 16:40
bool QDateTime::isValid () const
Returns true if both the date and the time are valid; otherwise returns false.

Not sure why it happens after 1 second , but in order to operate with QDateTime correctly , both time and data must be set. You've set the time only with setTime method and in case of latter , setTime_t has set the time and year to the 03:00:00 1970 (as it said in the documentation) So you've got both set and ready to go.