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::elapsed() behavior
Forum Updated to NodeBB v4.3 + New Features

QTime::elapsed() behavior

Scheduled Pinned Locked Moved Unsolved General and Desktop
2 Posts 2 Posters 1.0k 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.
  • B Offline
    B Offline
    Bart_Vandewoestyne
    wrote on last edited by
    #1

    While reviewing some code, I came across the following:

    QString FooBar::getNextNotification(int timeOut)
    {
        QTime startTime = QTime::currentTime();
        QTime::currentTime();
    
        while (startTime.elapsed() < timeOut)
        {
            ...
        }
        ...
    }
    

    I have two questions about this function:

    1. The documentation at http://doc.qt.io/archives/qt-4.8/qtime.html#elapsed writes "Returns the number of milliseconds that have elapsed since the last time start() or restart() was called." Note however that we are not calling start() or restart() explicitly. Can I safely assume that the first statement in the function implicitly calls start()or restart() and will this work? Or is this undefined behavior?
    2. Why on earth did the original author of the code add that second statement? It just calls QTime::currentTime() but doesn't assign that time to any variable. Does QTime::currentTime() have any side-effects that I should be aware of here? I don't see anything mentioned at http://doc.qt.io/archives/qt-4.8/qtime.html#currentTime

    As for my question 1), the following test code seems to give me the correct elapsed time:

    #include <QDebug>
    #include <QTime>
    #include <chrono>
    #include <thread>
    
    int main()
    {
        QTime startTime = QTime::currentTime();
    
        std::this_thread::sleep_for(std::chrono::milliseconds(5000));
    
        qDebug() << startTime.elapsed();
    }
    

    Is this undefined behavior and am I just having pure luck? Or is it rock-solid waterproof?

    JonBJ 1 Reply Last reply
    0
    • B Bart_Vandewoestyne

      While reviewing some code, I came across the following:

      QString FooBar::getNextNotification(int timeOut)
      {
          QTime startTime = QTime::currentTime();
          QTime::currentTime();
      
          while (startTime.elapsed() < timeOut)
          {
              ...
          }
          ...
      }
      

      I have two questions about this function:

      1. The documentation at http://doc.qt.io/archives/qt-4.8/qtime.html#elapsed writes "Returns the number of milliseconds that have elapsed since the last time start() or restart() was called." Note however that we are not calling start() or restart() explicitly. Can I safely assume that the first statement in the function implicitly calls start()or restart() and will this work? Or is this undefined behavior?
      2. Why on earth did the original author of the code add that second statement? It just calls QTime::currentTime() but doesn't assign that time to any variable. Does QTime::currentTime() have any side-effects that I should be aware of here? I don't see anything mentioned at http://doc.qt.io/archives/qt-4.8/qtime.html#currentTime

      As for my question 1), the following test code seems to give me the correct elapsed time:

      #include <QDebug>
      #include <QTime>
      #include <chrono>
      #include <thread>
      
      int main()
      {
          QTime startTime = QTime::currentTime();
      
          std::this_thread::sleep_for(std::chrono::milliseconds(5000));
      
          qDebug() << startTime.elapsed();
      }
      

      Is this undefined behavior and am I just having pure luck? Or is it rock-solid waterproof?

      JonBJ Offline
      JonBJ Offline
      JonB
      wrote on last edited by JonB
      #2

      @Bart_Vandewoestyne

      Reading the docs, QTime::start() only says: "Sets this time to the current time". So startTime.start() is only doing exactly the same as QTime startTime = QTime::currentTime();. And presumably startTime.elapsed(); is only doing same as QTime::currentTime() - startTime. Hence your code works.

      For the old code, it's only using startTime, which it assigns, so the stand-alone statement QTime::currentTime(); has no effect. You'd have to ask the author :), but I would presume it was unintended.

      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