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. Time count and display it
Forum Updated to NodeBB v4.3 + New Features

Time count and display it

Scheduled Pinned Locked Moved Solved General and Desktop
21 Posts 4 Posters 8.9k 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.
  • R Offline
    R Offline
    rockon209
    wrote on 6 Apr 2017, 14:03 last edited by
    #1

    Hello Everyone,

    How to show in a QTextEdit that how much time is passed after a pushbutton is pressed. Bascially i want to count time in min and second after a button is pressed and show it live. I know i have to use Qtimer but how to show it live.

    Thanks in advance

    1 Reply Last reply
    0
    • V Offline
      V Offline
      VRonin
      wrote on 6 Apr 2017, 15:52 last edited by VRonin 4 Jun 2017, 15:54
      #2

      header

      private:
      QElapsedTimer m_elTim;
      QPushButton* m_startButton;
      QTimer* m_intervalClock;
      QTimeEdit* m_liveCounter;
      

      source

      m_startButton=new QPushButton("Start",this);
      m_liveCounter = new QTimeEdit(this);
      m_intervalClock=new QTimer(this);
      connect(m_startButton,&QPushButton::clicked,this,[this](){m_elTim.start();});
      connect(m_intervalClock,&QTimer::timeout,this,[this](){if(m_elTim.isValid()) m_liveCounter->setTime(QTime(0,0).addMSecs(m_elTim.elapsed()));});
      m_intervalClock->start(500);
      

      "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

      1 Reply Last reply
      4
      • R Offline
        R Offline
        rockon209
        wrote on 7 Apr 2017, 06:33 last edited by
        #3

        @VRonin thanks for reply but i want to display the time in a QtExtEdit and also the startbutton has other function also. HOw to do this?

        J 1 Reply Last reply 7 Apr 2017, 06:43
        0
        • R rockon209
          7 Apr 2017, 06:33

          @VRonin thanks for reply but i want to display the time in a QtExtEdit and also the startbutton has other function also. HOw to do this?

          J Offline
          J Offline
          J.Hilk
          Moderators
          wrote on 7 Apr 2017, 06:43 last edited by J.Hilk 4 Jul 2017, 07:09
          #4

          @rockon209

          You can convert QTime to a QString easy enough with QTime::toString(const QString &format) const

          And to connect your button to multiple Slots just add an other connect:

          connect(m_startButton,&QPushButton::clicked,this,[this](){m_elTim.start();});
          ...
          connect(m_startButton,&QPushButton::clicked, receiver1, &SLOT1);
          connect(m_startButton,&QPushButton::clicked, receiver2, &SLOT2);
          ...
          ...
          connect(m_startButton,&QPushButton::clicked, receiverX, &SLOTX);
          

          Edit: Fixed missing ,


          Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct


          Q: What's that?
          A: It's blue light.
          Q: What does it do?
          A: It turns blue.

          1 Reply Last reply
          2
          • R Offline
            R Offline
            rockon209
            wrote on 7 Apr 2017, 07:39 last edited by rockon209 4 Jul 2017, 07:40
            #5

            I am geting the following error when i run the code

            field 'm_elTim' has incomplete type 'QElapsedTimer'
            QElapsedTimer m_elTim;
            ^

            M J 2 Replies Last reply 7 Apr 2017, 07:41
            0
            • R rockon209
              7 Apr 2017, 07:39

              I am geting the following error when i run the code

              field 'm_elTim' has incomplete type 'QElapsedTimer'
              QElapsedTimer m_elTim;
              ^

              M Offline
              M Offline
              mrjj
              Lifetime Qt Champion
              wrote on 7 Apr 2017, 07:41 last edited by
              #6

              @rockon209 said in Time count and display it:

              QElapsedTimer

              Did you you type
              #include <QElapsedTimer>
              ?

              1 Reply Last reply
              3
              • R rockon209
                7 Apr 2017, 07:39

                I am geting the following error when i run the code

                field 'm_elTim' has incomplete type 'QElapsedTimer'
                QElapsedTimer m_elTim;
                ^

                J Offline
                J Offline
                J.Hilk
                Moderators
                wrote on 7 Apr 2017, 07:41 last edited by
                #7

                @rockon209
                you're missing some includes,

                in this case add:

                #include <QElapsedTimer>
                

                Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct


                Q: What's that?
                A: It's blue light.
                Q: What does it do?
                A: It turns blue.

                1 Reply Last reply
                1
                • R Offline
                  R Offline
                  rockon209
                  wrote on 7 Apr 2017, 07:43 last edited by rockon209 4 Jul 2017, 07:56
                  #8

                  yes i have already included it in source file, but still the same error

                  V 1 Reply Last reply 7 Apr 2017, 08:24
                  0
                  • R rockon209
                    7 Apr 2017, 07:43

                    yes i have already included it in source file, but still the same error

                    V Offline
                    V Offline
                    VRonin
                    wrote on 7 Apr 2017, 08:24 last edited by
                    #9

                    @rockon209 said in Time count and display it:

                    in source file

                    That should go in the header file

                    "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

                    1 Reply Last reply
                    0
                    • R Offline
                      R Offline
                      rockon209
                      wrote on 7 Apr 2017, 08:38 last edited by
                      #10

                      @VRonin but when i include it in header file my application crashes

                      V 1 Reply Last reply 7 Apr 2017, 08:41
                      0
                      • R rockon209
                        7 Apr 2017, 08:38

                        @VRonin but when i include it in header file my application crashes

                        V Offline
                        V Offline
                        VRonin
                        wrote on 7 Apr 2017, 08:41 last edited by
                        #11

                        @rockon209 said in Time count and display it:

                        my application crashes

                        That's not due to the #include could you post your code?

                        "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

                        1 Reply Last reply
                        0
                        • R Offline
                          R Offline
                          rockon209
                          wrote on 7 Apr 2017, 09:11 last edited by VRonin 4 Jul 2017, 09:13
                          #12

                          main.cpp

                          #include "watch.h"
                          #include <QApplication>
                          
                          int main(int argc, char *argv[])
                          {
                              QApplication a(argc, argv);
                              watch w;
                              w.show();
                          
                              return a.exec();
                          }
                          

                          watch.cpp

                          #include "watch.h"
                          
                          
                          watch::watch(QWidget *parent) :
                              QWidget(parent)
                          
                          {
                          
                              m_startButton=new QPushButton("Start",this);
                              m_liveCounter = new QTimeEdit(this);
                              m_intervalClock=new QTimer(this);
                              connect(m_startButton,&QPushButton::clicked,this,[this](){m_elTim.start();});
                              connect(m_intervalClock,&QTimer::timeout,this,[this](){if(m_elTim.isValid()) m_liveCounter->setTime(QTime(0,0).addMSecs(m_elTim.elapsed()));});
                              m_intervalClock->start(500);
                              QBoxLayout* mainLayout = new QHBoxLayout();
                              mainLayout->addWidget(m_startButton);
                              mainLayout->addWidget(m_liveCounter );
                              setLayout(mainLayout);
                          }
                          

                          watch.h

                          #ifndef WATCH_H
                          #define WATCH_H
                          
                          #include <QWidget>
                          #include <QElapsedTimer>
                          #include <QPushButton>
                          #include <QTimeEdit>
                          #include <QTimer>
                          #include <QBoxLayout>
                          namespace Ui {
                          class watch;
                          }
                          
                          class watch : public QWidget
                          {
                              Q_OBJECT
                          
                          public:
                              explicit watch(QWidget *parent = 0);
                          
                          
                          private:
                              QElapsedTimer m_elTim;
                              QPushButton* m_startButton;
                              QTimer* m_intervalClock;
                              QTimeEdit* m_liveCounter;
                          };
                          
                          #endif // WATCH_H
                          

                          stopwatch.pro

                          QT       += core gui
                          
                          greaterThan(QT_MAJOR_VERSION, 4): QT += widgets
                          
                          TARGET = stopwatch
                          TEMPLATE = app
                          
                          
                          SOURCES += main.cpp\
                                  watch.cpp
                          
                          HEADERS  += watch.h
                          
                          FORMS    += watch.ui
                          
                          1 Reply Last reply
                          0
                          • V Offline
                            V Offline
                            VRonin
                            wrote on 7 Apr 2017, 09:16 last edited by
                            #13

                            Can't see anything obvious (but I might just be blind). could you post your stack trace at the moment of crash?

                            "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

                            1 Reply Last reply
                            0
                            • J Offline
                              J Offline
                              J.Hilk
                              Moderators
                              wrote on 7 Apr 2017, 09:16 last edited by
                              #14

                              ah,

                              in your watch.h add #include<QElapsedTimer> and in your watch.cpp intilize it with m_elTim = new QElapsedTimer(); it's missing from @VRonin code example


                              Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct


                              Q: What's that?
                              A: It's blue light.
                              Q: What does it do?
                              A: It turns blue.

                              V 1 Reply Last reply 7 Apr 2017, 09:17
                              0
                              • J J.Hilk
                                7 Apr 2017, 09:16

                                ah,

                                in your watch.h add #include<QElapsedTimer> and in your watch.cpp intilize it with m_elTim = new QElapsedTimer(); it's missing from @VRonin code example

                                V Offline
                                V Offline
                                VRonin
                                wrote on 7 Apr 2017, 09:17 last edited by
                                #15

                                @J.Hilk said in Time count and display it:

                                in your watch.cpp intilize it with m_elTim = new QElapsedTimer(); it's missing from @VRonin code example

                                It's on the stack so no need to do this. QElapsedTimer is not a QObject

                                "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

                                J 1 Reply Last reply 7 Apr 2017, 09:21
                                0
                                • R Offline
                                  R Offline
                                  rockon209
                                  wrote on 7 Apr 2017, 09:18 last edited by
                                  #16

                                  now the code is not crashing. everyting is running but when i push startbutton nothing happens

                                  V 1 Reply Last reply 7 Apr 2017, 09:19
                                  0
                                  • R rockon209
                                    7 Apr 2017, 09:18

                                    now the code is not crashing. everyting is running but when i push startbutton nothing happens

                                    V Offline
                                    V Offline
                                    VRonin
                                    wrote on 7 Apr 2017, 09:19 last edited by
                                    #17

                                    @rockon209 said in Time count and display it:

                                    now the code is not crashing.

                                    What did you change?

                                    "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

                                    1 Reply Last reply
                                    0
                                    • V VRonin
                                      7 Apr 2017, 09:17

                                      @J.Hilk said in Time count and display it:

                                      in your watch.cpp intilize it with m_elTim = new QElapsedTimer(); it's missing from @VRonin code example

                                      It's on the stack so no need to do this. QElapsedTimer is not a QObject

                                      J Offline
                                      J Offline
                                      J.Hilk
                                      Moderators
                                      wrote on 7 Apr 2017, 09:21 last edited by
                                      #18

                                      @VRonin said in Time count and display it:

                                      It's on the stack so no need to do this. QElapsedTimer is not a QObject

                                      You're right of course, I must have missed that...


                                      Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct


                                      Q: What's that?
                                      A: It's blue light.
                                      Q: What does it do?
                                      A: It turns blue.

                                      1 Reply Last reply
                                      0
                                      • R Offline
                                        R Offline
                                        rockon209
                                        wrote on 7 Apr 2017, 09:22 last edited by rockon209 4 Jul 2017, 09:23
                                        #19

                                        I create a new code just to check if everyting works fine and then i found out that when i click startbutton nothing happens

                                        1 Reply Last reply
                                        0
                                        • R Offline
                                          R Offline
                                          rockon209
                                          wrote on 7 Apr 2017, 09:46 last edited by
                                          #20

                                          ok sorry guys it working but its not showing the seconds only minute and hours how to change the format? to hh:mm:ss

                                          1 Reply Last reply
                                          0

                                          1/21

                                          6 Apr 2017, 14:03

                                          • Login

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