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. Low performance, timer and label.
Forum Updated to NodeBB v4.3 + New Features

Low performance, timer and label.

Scheduled Pinned Locked Moved Unsolved General and Desktop
36 Posts 7 Posters 10.8k Views 3 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.
  • V VRonin
    9 Feb 2018, 12:13

    @Loc888 said in Low performance, timer and label.:

    it's working, but i need it in MS

    Unfortunately this is platform dependant. Best you could do is:

    #include <QApplication>
    #include <QTimer>
    #include <QLabel>
    
    int main(int argc, char *argv[])
    {
        QApplication a(argc, argv);
        QLabel mainWid;
        QTimer mainTimer;
        mainTimer.setTimerType(Qt::PreciseTimer);
        unsigned int counter = 0;
        QObject::connect(&mainTimer,&QTimer::timeout,[&mainWid,&counter]()->void{
            mainWid.setText(QObject::tr("%1 msec").arg(++counter));
            mainWid.repaint();
        });
        mainTimer.start(1);
        mainWid.show();
        return a.exec();
    }
    

    Please note, from http://doc.qt.io/qt-5/qt.html#TimerType-enum:

    Precise timers try to keep millisecond accuracy

    try != will, unfortunately

    L Offline
    L Offline
    Loc888
    wrote on 10 Feb 2018, 01:10 last edited by
    #18

    @VRonin Maybe i expressed wrong, but i don't want it in Ms (the total), so 5500 = 5,5s, i want that pattern "mm.ss.zzz" or "mm.ss.zz" , but i want it to start from 0, not from the current time. Is any way to force it? (In sec, ther is no delay when the method update the text).

    1 Reply Last reply
    0
    • J J.Hilk
      9 Feb 2018, 12:10

      @Loc888
      this code examples are for the purpose of trouble shooting.
      But it's easy enought to adapt.

      #include <QApplication>
      #include <QTimer>
      #include <QLabel>
      #include <QTime>
      #include <QElapsedTimer>
      int main(int argc, char *argv[])
      {
          QApplication a(argc, argv);
      
          QLabel mainWid;
          QTimer mainTimer;
          QTime time(0,0);
          QElapsedTimer actualTimePassed;
          QObject::connect(&mainTimer,&QTimer::timeout,[&mainWid, &time, &actualTimePassed]()->void{
              time = time.addMSecs(actualTimePassed.elapsed());
              actualTimePassed.start();
              mainWid.setText(time.toString("hh.mm.ss.zzz"));
          });
          actualTimePassed.start();
          mainTimer.start(10);
          mainWid.show();
      
          return a.exec();
      }
      
      L Offline
      L Offline
      Loc888
      wrote on 10 Feb 2018, 02:20 last edited by Loc888 2 Oct 2018, 02:25
      #19

      @J.Hilk Guys,i tried J.Hilk code,and it's not working how it should.

      It doesnt help me, it's working, but if i compare the timer to my windows time, the difference is arround 10s (after 2min test), it's too mutch.

      Why it's happening?

      Ps. I tried with "setTimerType(Qt::PreciseTimer);", but still doesn't help...

      ????????

      1 Reply Last reply
      0
      • M Offline
        M Offline
        mpergand
        wrote on 10 Feb 2018, 03:53 last edited by
        #20

        My turn ;)

        QLabel label;
        label.setGeometry(100,100,200,50);
         QTimer timer;
         QTime time;
        
         QObject::connect(&timer,&QTimer::timeout,[&time,&label](){
            QTime elapsed=QTime::fromMSecsSinceStartOfDay(time.elapsed());
            label.setText(elapsed.toString("mm:ss:zzz"));
            });
        
        time.start();
        timer.start(100);
        label.show();
        
        L 1 Reply Last reply 10 Feb 2018, 04:43
        5
        • M mpergand
          10 Feb 2018, 03:53

          My turn ;)

          QLabel label;
          label.setGeometry(100,100,200,50);
           QTimer timer;
           QTime time;
          
           QObject::connect(&timer,&QTimer::timeout,[&time,&label](){
              QTime elapsed=QTime::fromMSecsSinceStartOfDay(time.elapsed());
              label.setText(elapsed.toString("mm:ss:zzz"));
              });
          
          time.start();
          timer.start(100);
          label.show();
          
          L Offline
          L Offline
          Loc888
          wrote on 10 Feb 2018, 04:43 last edited by
          #21

          @mpergand

          It seems you are the winner... Why it's working?

          I thought it's working, because you set the delay to 100ms, but even if i change it to 1ms, still working 100%.
          I don't understand why this is working,and the other timers including my, are all retarded... Why it's happening?

          And that's the text update method fault's, i tried to turn off the update, and just set the last result, and it's 100% accurate.

          1 Reply Last reply
          0
          • M Offline
            M Offline
            mpergand
            wrote on 10 Feb 2018, 05:11 last edited by mpergand 2 Oct 2018, 05:41
            #22

            I know by experience that to rely on a timer is all wrong.
            You must rely on the system clock for good accuracy.

            And that's the text update method fault's, i tried to turn off the update, and just set the last result, and it's 100% accurate.

            Refresh 1000 times per sec is a heavy task, it consumes about 20% cpu on my machine and serves nothing.
            Timers use the event loop and if the computer is doing heavy tasks, timers triggers might be lost.
            Rely on event loops for a counter is definitely a bad idea.

            L 2 Replies Last reply 10 Feb 2018, 05:18
            0
            • M mpergand
              10 Feb 2018, 05:11

              I know by experience that to rely on a timer is all wrong.
              You must rely on the system clock for good accuracy.

              And that's the text update method fault's, i tried to turn off the update, and just set the last result, and it's 100% accurate.

              Refresh 1000 times per sec is a heavy task, it consumes about 20% cpu on my machine and serves nothing.
              Timers use the event loop and if the computer is doing heavy tasks, timers triggers might be lost.
              Rely on event loops for a counter is definitely a bad idea.

              L Offline
              L Offline
              Loc888
              wrote on 10 Feb 2018, 05:18 last edited by
              #23

              @mpergand Ok, how i can use it with Start, Stop, and Reset button? Because i am trying to move your code to another project, and work with buttons, but when i start the timer, it doesn't do anything.. Still be on 00:00:000, i run it ones, and it start from the actual minutes and seconds, then i modified your code to force it start from 0, and i broke something, now nothing is working...

              1 Reply Last reply
              0
              • M mpergand
                10 Feb 2018, 05:11

                I know by experience that to rely on a timer is all wrong.
                You must rely on the system clock for good accuracy.

                And that's the text update method fault's, i tried to turn off the update, and just set the last result, and it's 100% accurate.

                Refresh 1000 times per sec is a heavy task, it consumes about 20% cpu on my machine and serves nothing.
                Timers use the event loop and if the computer is doing heavy tasks, timers triggers might be lost.
                Rely on event loops for a counter is definitely a bad idea.

                L Offline
                L Offline
                Loc888
                wrote on 10 Feb 2018, 08:16 last edited by
                #24

                @mpergand Ok, nevermind, i am blind sometimes... I fix it.

                I find the way to restart it, but i have some problems with

                " time.fromString("05:00:000"); "

                It's not working, how should i use that? Or how to stop the time and the timer, and re-start it again but from the last time? (I dont want to reset it when i press the stop button, and then start).

                J 1 Reply Last reply 10 Feb 2018, 12:08
                0
                • V Offline
                  V Offline
                  VRonin
                  wrote on 10 Feb 2018, 10:15 last edited by
                  #25

                  You missed an argument and the fact that it's a static function: http://doc.qt.io/qt-5/qtime.html#fromString-1

                  time = QTime::fromString("05:00:000","mm:ss:zzz");

                  Have you considered QTimeEdit instead of QLabel?

                  "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
                  ~Napoleon Bonaparte

                  On a crusade to banish setIndexWidget() from the holy land of Qt

                  L 1 Reply Last reply 10 Feb 2018, 11:02
                  2
                  • V VRonin
                    10 Feb 2018, 10:15

                    You missed an argument and the fact that it's a static function: http://doc.qt.io/qt-5/qtime.html#fromString-1

                    time = QTime::fromString("05:00:000","mm:ss:zzz");

                    Have you considered QTimeEdit instead of QLabel?

                    L Offline
                    L Offline
                    Loc888
                    wrote on 10 Feb 2018, 11:02 last edited by
                    #26

                    @VRonin Still reset when i press the start button, and when i stop and start,it starts from 0.
                    Comon, how to fix that? It's almost fixed.

                    K 1 Reply Last reply 10 Feb 2018, 11:58
                    0
                    • L Loc888
                      10 Feb 2018, 11:02

                      @VRonin Still reset when i press the start button, and when i stop and start,it starts from 0.
                      Comon, how to fix that? It's almost fixed.

                      K Offline
                      K Offline
                      koahnig
                      wrote on 10 Feb 2018, 11:58 last edited by
                      #27

                      @Loc888 said in Low performance, timer and label.:

                      @VRonin Still reset when i press the start button, and when i stop and start,it starts from 0.
                      Comon, how to fix that? It's almost fixed.

                      I suggest that you are showing your actual code snippets and explain what you think they should do.

                      Above there are only code snippets from others, not a single snippet from you. Nobody knows what you have taken over into your code. Therefore nobody can give you sound advice.

                      Vote the answer(s) that helped you to solve your issue(s)

                      L 1 Reply Last reply 10 Feb 2018, 12:18
                      3
                      • L Loc888
                        10 Feb 2018, 08:16

                        @mpergand Ok, nevermind, i am blind sometimes... I fix it.

                        I find the way to restart it, but i have some problems with

                        " time.fromString("05:00:000"); "

                        It's not working, how should i use that? Or how to stop the time and the timer, and re-start it again but from the last time? (I dont want to reset it when i press the stop button, and then start).

                        J Offline
                        J Offline
                        JKSH
                        Moderators
                        wrote on 10 Feb 2018, 12:08 last edited by
                        #28

                        @Loc888 said in Low performance, timer and label.:

                        how to stop the time and the timer, and re-start it again but from the last time? (I dont want to reset it when i press the stop button, and then start).

                        The Qt timer classes don't have a built-in ability to resume timing like a stopwatch. This means you need to write extra logic to do it.

                        1. Start your QElapsedTimer. It starts counting from 0.
                        2. Call QElapsedTimer::elapsed() to find out how many milliseconds have passed since the timer was started.
                        3. When the user clicks the stop button, store the value of the elapsed time in a variable.
                        4. When the user clicks the start the button again, restart your QElapsedTimer. This makes it start from 0 again.
                        5. Add the value of the stored variable to QElapsedTimer::elapsed() to get your final stopwatch value.

                        @Loc888 said in Low performance, timer and label.:

                        Comon, how to fix that? It's almost fixed.

                        Since you have not shown your code at all, all the discussions in this thread is only to teach you how to use the time-related classes and functions. We cannot see how you have designed your program, so we cannot tell you how to "fix" your program.

                        You need to sit down and think through the logic and maths to implement the stopwatch.

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

                        1 Reply Last reply
                        4
                        • K koahnig
                          10 Feb 2018, 11:58

                          @Loc888 said in Low performance, timer and label.:

                          @VRonin Still reset when i press the start button, and when i stop and start,it starts from 0.
                          Comon, how to fix that? It's almost fixed.

                          I suggest that you are showing your actual code snippets and explain what you think they should do.

                          Above there are only code snippets from others, not a single snippet from you. Nobody knows what you have taken over into your code. Therefore nobody can give you sound advice.

                          L Offline
                          L Offline
                          Loc888
                          wrote on 10 Feb 2018, 12:18 last edited by Loc888 2 Oct 2018, 12:19
                          #29

                          @koahnig

                          #include "mainwindow.h"
                          #include "ui_mainwindow.h"
                          #include <QTimer>
                          #include <QTime>
                          #include <QElapsedTimer>
                          #include <QLabel>
                          
                          
                          int Time_Counter;
                          
                          QString Temp_Time;
                          
                          QTime elapsed_time_time(0,0);
                          
                          QTime time(0,0);
                          
                          
                          
                          
                          
                          MainWindow::MainWindow(QWidget *parent) :
                              QMainWindow(parent),
                              ui(new Ui::MainWindow)
                          {
                              ui->setupUi(this);
                          
                          
                              timer = new QTimer(this);
                          
                              connect(timer,SIGNAL(timeout()),this,SLOT(Time_Counter_Int()));
                          
                              
                          }
                          
                          MainWindow::~MainWindow()
                          {
                              delete ui;
                          }
                          
                          void MainWindow::Show_Results()
                          {
                          
                              elapsed_time = QTime::fromMSecsSinceStartOfDay(time.elapsed_time());
                          
                              ui->Timer_Label->setText(elapsed_time.toString("mm:ss:zzz"));
                          
                              Temp_Time = elapsed_time.toString("mm:ss:zzz");
                          
                          
                          }
                          
                          void MainWindow::on_Start_Button_clicked()
                          {
                          
                          
                              time.start();
                          
                              timer->start(1);
                          
                              elapsed_time = elapsed_time.fromString(Temp_Time);
                          
                          }
                          
                          void MainWindow::on_Stop_Button_clicked()
                          {
                          
                              timer->stop();
                          
                              Temp_Time = elapsed_time.toString("mm:ss:zzz");
                          
                              elapsed_time = elapsed_time.fromString(Temp_Time,"mm:ss:zzz");
                          
                          
                          }
                          
                          void MainWindow::on_Reset_Button_clicked()
                          {
                          
                              time.restart();
                          
                              Time_Counter = 0;
                          }
                          
                          1 Reply Last reply
                          0
                          • M Offline
                            M Offline
                            mrjj
                            Lifetime Qt Champion
                            wrote on 11 Feb 2018, 20:34 last edited by
                            #30

                            Hi
                            You connect timer to Time_Counter_Int
                            but you do not seem to have such slot ??

                            L 1 Reply Last reply 12 Feb 2018, 04:49
                            2
                            • M mrjj
                              11 Feb 2018, 20:34

                              Hi
                              You connect timer to Time_Counter_Int
                              but you do not seem to have such slot ??

                              L Offline
                              L Offline
                              Loc888
                              wrote on 12 Feb 2018, 04:49 last edited by Loc888 2 Dec 2018, 04:50
                              #31

                              @mrjj Yes, i see. This is simplified version, don't care about that, i have everything in the place.
                              I just need to know, how to reset and stop that timer, and something to convert the value of time, to total MS, because if i just add any variable and increment it, probably it's just 80% accurate, i need to convert the time from timer to total value in MS. I am gonna try, if i can find any way to convert an QString to int value.

                              M J 2 Replies Last reply 12 Feb 2018, 07:29
                              0
                              • L Loc888
                                12 Feb 2018, 04:49

                                @mrjj Yes, i see. This is simplified version, don't care about that, i have everything in the place.
                                I just need to know, how to reset and stop that timer, and something to convert the value of time, to total MS, because if i just add any variable and increment it, probably it's just 80% accurate, i need to convert the time from timer to total value in MS. I am gonna try, if i can find any way to convert an QString to int value.

                                M Offline
                                M Offline
                                mrjj
                                Lifetime Qt Champion
                                wrote on 12 Feb 2018, 07:29 last edited by
                                #32

                                @Loc888 said in Low performance, timer and label.:

                                find any way to convert an QString to int value.

                                you mean like
                                http://doc.qt.io/qt-5/qstring.html#toInt
                                `?

                                1 Reply Last reply
                                0
                                • L Loc888
                                  12 Feb 2018, 04:49

                                  @mrjj Yes, i see. This is simplified version, don't care about that, i have everything in the place.
                                  I just need to know, how to reset and stop that timer, and something to convert the value of time, to total MS, because if i just add any variable and increment it, probably it's just 80% accurate, i need to convert the time from timer to total value in MS. I am gonna try, if i can find any way to convert an QString to int value.

                                  J Offline
                                  J Offline
                                  JKSH
                                  Moderators
                                  wrote on 12 Feb 2018, 13:35 last edited by
                                  #33

                                  @Loc888 said in Low performance, timer and label.:

                                  I just need to know, how to reset and stop that timer

                                  Have you tried anything at all from my last message?

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

                                  1 Reply Last reply
                                  1
                                  • M Offline
                                    M Offline
                                    mpergand
                                    wrote on 12 Feb 2018, 13:51 last edited by mpergand 2 Dec 2018, 16:11
                                    #34

                                    @Loc888 said in Low performance, timer and label.:

                                    I just need to know, how to reset and stop that timer

                                    #include <QApplication>
                                    #include <QTimer>
                                    #include <QElapsedTimer>
                                    #include <QTime>
                                    #include <QLabel>
                                    #include <QVBoxLayout>
                                    #include <QPushButton>
                                    
                                    int main(int argc, char *argv[])
                                    {
                                    
                                        QApplication app(argc, argv);
                                    
                                        int totalTime=0;
                                        QElapsedTimer time;
                                        QTimer timer;
                                        timer.setInterval(99);
                                    
                                        QWidget win;
                                        QLabel* timeLabel=new QLabel("00:00:000");
                                        QLabel* totalLabel=new QLabel("00:00:000");
                                        QPushButton* startButton=new QPushButton("Start");
                                        startButton->setCheckable(true);
                                        QPushButton* resetButton=new QPushButton("Reset");
                                    
                                        QVBoxLayout* vLayout=new QVBoxLayout;
                                        QHBoxLayout* hLayout=new QHBoxLayout;
                                    
                                        hLayout->addWidget(timeLabel);
                                        hLayout->addWidget(startButton);
                                        hLayout->addWidget(resetButton);
                                        vLayout->addLayout(hLayout);
                                        vLayout->addWidget(totalLabel);
                                    
                                        win.setLayout(vLayout);
                                        win.show();
                                    
                                        // timer action
                                        QObject::connect(&timer,&QTimer::timeout,[&time,&timeLabel](){
                                            QTime elapsed=QTime::fromMSecsSinceStartOfDay(time.elapsed());
                                            timeLabel->setText(elapsed.toString("mm:ss:zzz"));
                                            });
                                    
                                        // start/stop action
                                        QObject::connect(startButton,&QPushButton::toggled,[&](bool checked){
                                    
                                            if(checked)
                                                {
                                                time.start();
                                                timer.start();
                                                resetButton->setEnabled(false);
                                                startButton->setText("Stop");
                                                }
                                            else
                                                {
                                                timer.stop();
                                                int t=time.elapsed();
                                                totalTime+=t;
                                                QTime elapsed=QTime::fromMSecsSinceStartOfDay(totalTime);
                                                totalLabel->setText(elapsed.toString("mm:ss:zzz"));
                                                // adjust time label
                                                elapsed=QTime::fromMSecsSinceStartOfDay(t);
                                                timeLabel->setText(elapsed.toString("mm:ss:zzz"));
                                                resetButton->setEnabled(true);
                                                startButton->setText("Start");
                                                }
                                    
                                            });
                                    
                                        // reset action
                                        QObject::connect(resetButton,&QPushButton::clicked,[&timeLabel,&totalLabel,&totalTime](bool checked){
                                            totalTime=0;
                                            totalLabel->setText("00:00:000");
                                            timeLabel->setText("00:00:000");
                                            });
                                    
                                        return app.exec();
                                    }
                                    

                                    Allows multiple start/stop, cumulates each duration in totalTime.

                                    L 1 Reply Last reply 13 Feb 2018, 15:23
                                    0
                                    • M mpergand
                                      12 Feb 2018, 13:51

                                      @Loc888 said in Low performance, timer and label.:

                                      I just need to know, how to reset and stop that timer

                                      #include <QApplication>
                                      #include <QTimer>
                                      #include <QElapsedTimer>
                                      #include <QTime>
                                      #include <QLabel>
                                      #include <QVBoxLayout>
                                      #include <QPushButton>
                                      
                                      int main(int argc, char *argv[])
                                      {
                                      
                                          QApplication app(argc, argv);
                                      
                                          int totalTime=0;
                                          QElapsedTimer time;
                                          QTimer timer;
                                          timer.setInterval(99);
                                      
                                          QWidget win;
                                          QLabel* timeLabel=new QLabel("00:00:000");
                                          QLabel* totalLabel=new QLabel("00:00:000");
                                          QPushButton* startButton=new QPushButton("Start");
                                          startButton->setCheckable(true);
                                          QPushButton* resetButton=new QPushButton("Reset");
                                      
                                          QVBoxLayout* vLayout=new QVBoxLayout;
                                          QHBoxLayout* hLayout=new QHBoxLayout;
                                      
                                          hLayout->addWidget(timeLabel);
                                          hLayout->addWidget(startButton);
                                          hLayout->addWidget(resetButton);
                                          vLayout->addLayout(hLayout);
                                          vLayout->addWidget(totalLabel);
                                      
                                          win.setLayout(vLayout);
                                          win.show();
                                      
                                          // timer action
                                          QObject::connect(&timer,&QTimer::timeout,[&time,&timeLabel](){
                                              QTime elapsed=QTime::fromMSecsSinceStartOfDay(time.elapsed());
                                              timeLabel->setText(elapsed.toString("mm:ss:zzz"));
                                              });
                                      
                                          // start/stop action
                                          QObject::connect(startButton,&QPushButton::toggled,[&](bool checked){
                                      
                                              if(checked)
                                                  {
                                                  time.start();
                                                  timer.start();
                                                  resetButton->setEnabled(false);
                                                  startButton->setText("Stop");
                                                  }
                                              else
                                                  {
                                                  timer.stop();
                                                  int t=time.elapsed();
                                                  totalTime+=t;
                                                  QTime elapsed=QTime::fromMSecsSinceStartOfDay(totalTime);
                                                  totalLabel->setText(elapsed.toString("mm:ss:zzz"));
                                                  // adjust time label
                                                  elapsed=QTime::fromMSecsSinceStartOfDay(t);
                                                  timeLabel->setText(elapsed.toString("mm:ss:zzz"));
                                                  resetButton->setEnabled(true);
                                                  startButton->setText("Start");
                                                  }
                                      
                                              });
                                      
                                          // reset action
                                          QObject::connect(resetButton,&QPushButton::clicked,[&timeLabel,&totalLabel,&totalTime](bool checked){
                                              totalTime=0;
                                              totalLabel->setText("00:00:000");
                                              timeLabel->setText("00:00:000");
                                              });
                                      
                                          return app.exec();
                                      }
                                      

                                      Allows multiple start/stop, cumulates each duration in totalTime.

                                      L Offline
                                      L Offline
                                      Loc888
                                      wrote on 13 Feb 2018, 15:23 last edited by Loc888
                                      #35

                                      @mpergand Ye good one, but too much stuff to add,and it looks a little bit too complicated.
                                      I try more than few times,and i find much much more better way. I just add:

                                      time = elapsed.fromMSecsSinceStartOfDay(elapsed.elapsed());

                                      Just by this one line,now when i press the start button, then stop it and restart, it still counting from the last result. Did you think about that?

                                      Ps. When i add that line, i had some problems , because the timer doesn't want to reset the value, i fix it fast just by adding some bool variables and control some parts of code.

                                      1 Reply Last reply
                                      0
                                      • M Offline
                                        M Offline
                                        mpergand
                                        wrote on 13 Feb 2018, 16:25 last edited by
                                        #36

                                        Simplified to display total time only:

                                        int main(int argc, char *argv[])
                                        {
                                            QApplication app(argc, argv);
                                        
                                            int totalTime=0;
                                            QElapsedTimer time;
                                            QTimer timer;
                                            timer.setInterval(99);
                                        
                                            QWidget win;
                                            QLabel* totalLabel=new QLabel("00:00:000");
                                            QPushButton* startButton=new QPushButton("Start");
                                            startButton->setCheckable(true);
                                            QPushButton* resetButton=new QPushButton("Reset");
                                            QHBoxLayout* hLayout=new QHBoxLayout;
                                        
                                            hLayout->addWidget(totalLabel);
                                            hLayout->addWidget(startButton);
                                            hLayout->addWidget(resetButton);
                                        
                                            win.setLayout(hLayout);
                                            win.show();
                                        
                                            // timer action
                                            QObject::connect(&timer,&QTimer::timeout,[&](){
                                                QTime elapsed=QTime::fromMSecsSinceStartOfDay(time.elapsed());
                                                elapsed=elapsed.addMSecs(totalTime);
                                                totalLabel->setText(elapsed.toString("mm:ss:zzz"));
                                                });
                                        
                                            // start/stop action
                                            QObject::connect(startButton,&QPushButton::toggled,[&](bool checked){
                                        
                                                if(checked)
                                                    {
                                                    time.start();
                                                    timer.start();
                                                    resetButton->setEnabled(false);
                                                    startButton->setText("Stop");
                                                    }
                                                else
                                                    {
                                                    timer.stop();
                                                    totalTime+=time.elapsed();
                                                    QTime elapsed=QTime::fromMSecsSinceStartOfDay(totalTime);
                                                    totalLabel->setText(elapsed.toString("mm:ss:zzz"));
                                        
                                                    resetButton->setEnabled(true);
                                                    startButton->setText("Start");
                                                    }
                                                });
                                        
                                            // reset action
                                            QObject::connect(resetButton,&QPushButton::clicked,[&totalLabel,&totalTime](bool checked){
                                                totalTime=0;
                                                totalLabel->setText("00:00:000");
                                                });
                                        
                                            return app.exec();
                                        }
                                        
                                        1 Reply Last reply
                                        2

                                        27/36

                                        10 Feb 2018, 11:58

                                        • Login

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