Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. QTime stops and do not want to continue
Qt 6.11 is out! See what's new in the release blog

QTime stops and do not want to continue

Scheduled Pinned Locked Moved General and Desktop
11 Posts 3 Posters 5.4k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • ? Offline
    ? Offline
    A Former User
    wrote on last edited by
    #1

    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?

    1 Reply Last reply
    0
    • ? Offline
      ? Offline
      A Former User
      wrote on last edited by
      #2

      Oh i found out both "time_temp" and "current_time" does not change their values any more.

      1 Reply Last reply
      0
      • C Offline
        C Offline
        ckakman
        wrote on last edited by
        #3

        Is this your actual code or have you just made up an example to post here?

        1 Reply Last reply
        0
        • ? Offline
          ? Offline
          A Former User
          wrote on last edited by
          #4

          This is just an extract

          1 Reply Last reply
          0
          • C Offline
            C Offline
            ckakman
            wrote on last edited by
            #5

            So it is production code then, but I don't see any breaks in that loop. And you are not doing anything in the loop except qDebug()s.

            I don't think this is the actual code. I can't go on with what you have provided.

            1 Reply Last reply
            0
            • ? Offline
              ? Offline
              A Former User
              wrote on last edited by
              #6

              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&#40;&#41;;
              

              }@

              The same problem after some seconds. Both variables do not change their values after seconds.

              1 Reply Last reply
              0
              • C Offline
                C Offline
                ckakman
                wrote on last edited by
                #7

                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;
                @

                1 Reply Last reply
                0
                • ? Offline
                  ? Offline
                  A Former User
                  wrote on last edited by
                  #8

                  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();@

                  1 Reply Last reply
                  0
                  • JKSHJ Offline
                    JKSHJ Offline
                    JKSH
                    Moderators
                    wrote on last edited by
                    #9

                    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

                    Qt Doc Search for browsers: forum.qt.io/topic/35616/web-browser-extension-for-improved-doc-searches

                    1 Reply Last reply
                    0
                    • C Offline
                      C Offline
                      ckakman
                      wrote on last edited by
                      #10

                      [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.

                      1 Reply Last reply
                      0
                      • JKSHJ Offline
                        JKSHJ Offline
                        JKSH
                        Moderators
                        wrote on last edited by
                        #11

                        [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

                        Qt Doc Search for browsers: forum.qt.io/topic/35616/web-browser-extension-for-improved-doc-searches

                        1 Reply Last reply
                        0

                        • Login

                        • Login or register to search.
                        • First post
                          Last post
                        0
                        • Categories
                        • Recent
                        • Tags
                        • Popular
                        • Users
                        • Groups
                        • Search
                        • Get Qt Extensions
                        • Unsolved