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. Showing timer in "hh:mm:ss.zzz" and reset when button clicked

Showing timer in "hh:mm:ss.zzz" and reset when button clicked

Scheduled Pinned Locked Moved Solved General and Desktop
qtime
4 Posts 3 Posters 1.4k Views
  • 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.
  • T Offline
    T Offline
    TUStudi
    wrote on 28 Apr 2020, 14:32 last edited by
    #1

    Hi,

    I have the following use case: I am receiving data from a sensor and every time a slot is called, where I write these data to a file. I would also like to isnert a timestamp in the file like a stopwatch, which begins when the Slot is called for the first time. I have the following code in my Sot:

        static QTime time(QTime::currentTime());
        formattedTime = QDateTime::fromTime_t(time.elapsed()/1000.0).toUTC().toString("hh:mm:ss.zzz"); //
        QTextStream outStream_secondDevice(&file);
        outStream << formattedTime <<";"<<"sensor value" << ";" << "sensor value;
    
    
    • First problem: The time is shown like this, without the millliseconds :
    00:00:00.000;
    00:00:01.000;
    00:00:01.000;
    00:00:02.000;
    00:00:04.000;
    
    • The second problem is, when I "reset" my GUI via Button (without exiting the programm or the event loop in the main) and then start to transfer the data again, I expect the timer to start from the beginning (0 seconds) but it continues (because of the static keyword). So would it be enough if I add a flag "firstDataTransfer" which is true for the first time the Slot is called and then set to false and if it is true, then the timer should "restart" or is there an easier way?
    • The time.elapsed is deprecated - is there another method or should I use QElapsedTimer?
    P 1 Reply Last reply 28 Apr 2020, 14:56
    0
    • B Offline
      B Offline
      Bonnie
      wrote on 28 Apr 2020, 15:01 last edited by Bonnie
      #3

      I think QElapsedTimer is better for that situation.
      And instead of a static variable in a function, how about make it a member variable that can be used in other member functions.

      in the header, define a member variable of QElapsedTimer

      QElapsedTimer timer;
      

      in the slot function

      if(!timer.isValid())
          timer.start();
      QString formattedTime = QTime::fromMSecsSinceStartOfDay(timer.elapsed()).toString("hh:mm:ss.zzz");
      

      when you "reset" the GUI

      timer.invalidate();
      
      T 1 Reply Last reply 28 Apr 2020, 16:09
      2
      • T TUStudi
        28 Apr 2020, 14:32

        Hi,

        I have the following use case: I am receiving data from a sensor and every time a slot is called, where I write these data to a file. I would also like to isnert a timestamp in the file like a stopwatch, which begins when the Slot is called for the first time. I have the following code in my Sot:

            static QTime time(QTime::currentTime());
            formattedTime = QDateTime::fromTime_t(time.elapsed()/1000.0).toUTC().toString("hh:mm:ss.zzz"); //
            QTextStream outStream_secondDevice(&file);
            outStream << formattedTime <<";"<<"sensor value" << ";" << "sensor value;
        
        
        • First problem: The time is shown like this, without the millliseconds :
        00:00:00.000;
        00:00:01.000;
        00:00:01.000;
        00:00:02.000;
        00:00:04.000;
        
        • The second problem is, when I "reset" my GUI via Button (without exiting the programm or the event loop in the main) and then start to transfer the data again, I expect the timer to start from the beginning (0 seconds) but it continues (because of the static keyword). So would it be enough if I add a flag "firstDataTransfer" which is true for the first time the Slot is called and then set to false and if it is true, then the timer should "restart" or is there an easier way?
        • The time.elapsed is deprecated - is there another method or should I use QElapsedTimer?
        P Offline
        P Offline
        Pl45m4
        wrote on 28 Apr 2020, 14:56 last edited by
        #2

        @TUStudi said in Showing timer in "hh:mm:ss.zzz" and reset when button clicked:

        should I use QElapsedTimer?

        Yes.

        Do you even have to pause and continue from there or just start and stop/reset?


        If debugging is the process of removing software bugs, then programming must be the process of putting them in.

        ~E. W. Dijkstra

        1 Reply Last reply
        2
        • B Offline
          B Offline
          Bonnie
          wrote on 28 Apr 2020, 15:01 last edited by Bonnie
          #3

          I think QElapsedTimer is better for that situation.
          And instead of a static variable in a function, how about make it a member variable that can be used in other member functions.

          in the header, define a member variable of QElapsedTimer

          QElapsedTimer timer;
          

          in the slot function

          if(!timer.isValid())
              timer.start();
          QString formattedTime = QTime::fromMSecsSinceStartOfDay(timer.elapsed()).toString("hh:mm:ss.zzz");
          

          when you "reset" the GUI

          timer.invalidate();
          
          T 1 Reply Last reply 28 Apr 2020, 16:09
          2
          • B Bonnie
            28 Apr 2020, 15:01

            I think QElapsedTimer is better for that situation.
            And instead of a static variable in a function, how about make it a member variable that can be used in other member functions.

            in the header, define a member variable of QElapsedTimer

            QElapsedTimer timer;
            

            in the slot function

            if(!timer.isValid())
                timer.start();
            QString formattedTime = QTime::fromMSecsSinceStartOfDay(timer.elapsed()).toString("hh:mm:ss.zzz");
            

            when you "reset" the GUI

            timer.invalidate();
            
            T Offline
            T Offline
            TUStudi
            wrote on 28 Apr 2020, 16:09 last edited by TUStudi
            #4

            @Bonnie said in Showing timer in "hh:mm:ss.zzz" and reset when button clicked:

            I think QElapsedTimer is better for that situation.
            And instead of a static variable in a function, how about make it a member variable that can be used in other member functions.

            in the header, define a member variable of QElapsedTimer

            QElapsedTimer timer;
            

            in the slot function

            if(!timer.isValid())
                timer.start();
            QString formattedTime = QTime::fromMSecsSinceStartOfDay(timer.elapsed()).toString("hh:mm:ss.zzz");
            

            when you "reset" the GUI

            timer.invalidate();
            

            Thank you very much, that's exactly what I wanted :)

            Do you even have to pause and continue from there or just start and stop/reset?

            And yes, just start and stop/reset.

            1 Reply Last reply
            0

            4/4

            28 Apr 2020, 16:09

            • Login

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