Time count and display it
-
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
-
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);
-
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
,
-
I am geting the following error when i run the code
field 'm_elTim' has incomplete type 'QElapsedTimer'
QElapsedTimer m_elTim;
^ -
@rockon209 said in Time count and display it:
QElapsedTimer
Did you you type
#include <QElapsedTimer>
? -
-
yes i have already included it in source file, but still the same error
-
-
@rockon209 said in Time count and display it:
my application crashes
That's not due to the
#include
could you post your code? -
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
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
-
Can't see anything obvious (but I might just be blind). could you post your stack trace at the moment of crash?
-
ah,
in your watch.h add
#include<QElapsedTimer>
and in your watch.cpp intilize it withm_elTim = new QElapsedTimer();
it's missing from @VRonin code example -
@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
-
now the code is not crashing. everyting is running but when i push startbutton nothing happens
-
-
@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...
-
I create a new code just to check if everyting works fine and then i found out that when i click startbutton nothing happens
-
ok sorry guys it working but its not showing the seconds only minute and hours how to change the format? to hh:mm:ss
Post 3 of 21