QTime stops and do not want to continue
-
Hello,
in a part of my program I want to find out a time difference between the last call and now but my temporary variable stops working:
@...
QTime current_time= QTime::currentTime();
QTime time_temp;
quint16 time_delta;current_time = QTime::currentTime();
while(1){
time_temp = current_time; //save time current_time = QTime::currentTime(); //current time absolute_frames++; time_delta = (current_time.second()*1000 + current_time.msec()) - (time_temp.second()*1000 + time_temp.msec()); if(time_delta > 1000) { current_time = time_temp; qDebug() << "time problem!"; } qDebug() << "time_temp:" << time_temp << " current_time:" << current_time << " time_delta:" << time_delta;
}
...@
At some point, I get the qDebug() message and the value of time temp never changed from this moment although I wrote:
@time_temp = current_time; //save time@Does anybody know what it could be or what my mistake is?
-
Oh i found out both "time_temp" and "current_time" does not change their values any more.
-
This is just an extract
-
My original file includes more than 500 lines of code.
Now i wrote a complete new one. This is it:
@#include <QCoreApplication>
#include <QTime>
#include <QDebug>int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv);QTime current_time= QTime::currentTime(); QTime time_temp; quint16 time_delta; current_time = QTime::currentTime(); while(1) { time_temp = current_time; //save time current_time = QTime::currentTime(); //current time time_delta = (current_time.second()*1000 + current_time.msec()) - (time_temp.second()*1000 + time_temp.msec()); if(time_delta > 1000) { current_time = time_temp; qDebug() << "time problem!"; } qDebug() << "time_temp:" << time_temp << " current_time:" << current_time << " time_delta:" << time_delta; } return a.exec();
}@
The same problem after some seconds. Both variables do not change their values after seconds.
-
You are assigning time_temp back to current_time if time_delta gets larger than 1000. Then you assign current_time back to time_temp at beginning of the loop. You never update time_temp with current_time thereafter.
Remove line 24 in the if block:
@
current_time = time_temp;
@ -
Yes I could but i need the approximate time difference. My problem is that at one point the values of the variable "current_time" never change. If I print the values of "current_time" the values or the variables are frozen but QTime::currentTime is running. I can not change the values of "current_time" with the command any more:
@current_time = QTime::currentTime();@ -
Hi,
-Your loop is running at max speed (it makes your CPU run at 100%), and it is flooding your system with thousands of debug messages. I'm guessing that your program has actually frozen.-
-Try adding a delay in your loop and see what happens.-
Also, to measure time deltas, the recommended class is "QElapsedTimer":http://doc.qt.io/qt-5/qelapsedtimer.html
-
[quote author="Bartholomew" date="1421541823"]Yes I could but i need the approximate time difference. My problem is that at one point the values of the variable "current_time" never change. If I print the values of "current_time" the values or the variables are frozen but QTime::currentTime is running. I can not change the values of "current_time" with the command any more:
@current_time = QTime::currentTime();@[/quote]They are "frozen" because of a logic error in your code as I explained above. Once the difference is larger than 1000, you freeze time_temp by storing it in current_temp. t the next iteration of the loop, you save current_time back to temp_time. Then you update current_time, calculate the difference, which is again larger than 1000 and you again enter the if block. You again update current_time with time_temp. As you see, you never ever update time_temp with the newer value of current_time but instead update current_time with time_temp. This is your logic error, that's why you need to remove line 24.
If you can't follow the explanation above, I suggest you get a pen and track the values of these 2 variables on paper when the delta becomes greater than 1000.
-
[quote author="ckakman" date="1421545003"]They are "frozen" because of a logic error in your code as I explained above.[/quote]Good catch! I need to read more carefully before answering :P