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
Forum Updated to NodeBB v4.3 + New Features

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.7k 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.
  • T Offline
    T Offline
    TUStudi
    wrote on 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?
    Pl45m4P 1 Reply Last reply
    0
    • B Offline
      B Offline
      Bonnie
      wrote on 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
      2
      • T TUStudi

        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?
        Pl45m4P Offline
        Pl45m4P Offline
        Pl45m4
        wrote on 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 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
          2
          • B Bonnie

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

            • Login

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