PDA

View Full Version : Elapsed Time on a QLabel. How to do it?



GuangZor
18th August 2010, 04:30
void MainWindow::updateETLabel(){
QString text = QTime::currentTime().toString("hh:mm:ss");
setETLabelText(text);
}

//MainWindow constructor:
ETTimer = new QTimer(this);
connect(ETTimer,SIGNAL(timeout()),this,SLOT(update ETLabel()));

//StartButton:
ETTimer->start(1000);


the code above not works of course, because "QTime::currentTime().toString("hh:mm:ss");" only returns the current time...

I wanna make a timer that starts from 00:00:00 and increase each second. But I dont know how to do it in QT. Please I really need your help!

Lykurg
18th August 2010, 06:54
Simple use:
QTime t;
t.start();
//...
t.elapsed();

=> QTime::elapsed()

GuangZor
18th August 2010, 10:34
Thank you for the answer, but how to format the value returned by elapsed() into a "hh:mm:ss" ?
Other question, will I need to use QTimer + QTime together for this task?

Ginsengelf
18th August 2010, 10:53
Hi, QTime is enough, you don't need a QTimer. To format the time (it's a millisecond value) just calculate seconds, minutes, etc and build a string from these values.

Ginsengelf

Lykurg
18th August 2010, 10:56
QTime is enough. And it returns the seconds so you can calculate the minutes/hours etc yourself using the modulo operator.

EDIT: Ginsengelf was faster and he is right, it returns milliseconds not seconds

GuangZor
18th August 2010, 20:29
I dont know how to do it without a QTimer because it not shows the time elapsed every second on a QLabel. Is there any signals for this?
I think think think and I cant reach in any place. Some real code would be appreciated.

Sorry for these very basic questions, but I came from C++BUILDER and using its VCL we do this task very easy using a TTimer component (which executes a callback every second, same as QTimer) and a global "Time" object (which supports arithmetic operations, something like "00:00:00 + 00:00:01" which increases the current time)...

Lykurg
19th August 2010, 00:11
Ehm. for clarification: Do you want an analog clock, or do you want an elapsed time? If the second, then yes you have to combine both. QTime to get the elapsed time and a timer for calling the label update.

ChrisW67
19th August 2010, 00:12
Something along these lines:

#include <QtGui>

class mainwindow: public QMainWindow {
Q_OBJECT
public:
mainwindow(QWidget *p = 0): QMainWindow(p) {
timeLabel = new QLabel(this);
setCentralWidget(timeLabel);
time.start();
updateDisplay();
connect(&timer, SIGNAL(timeout()), this, SLOT(updateDisplay()));
timer.start(500); // twice per second
}
public slots:
void updateDisplay() {
int secs = time.elapsed() / 1000;
int mins = (secs / 60) % 60;
int hours = (secs / 3600);
secs = secs % 60;
timeLabel->setText(QString("%1:%2:%3")
.arg(hours, 2, 10, QLatin1Char('0'))
.arg(mins, 2, 10, QLatin1Char('0'))
.arg(secs, 2, 10, QLatin1Char('0')) );
}
private:
QLabel *timeLabel;
QTime time;
QTimer timer;
};

int main(int argc, char *argv[])
{
QApplication app(argc, argv);

mainwindow m;
m.show();
return app.exec();
}
#include "main.moc"

Urthas
19th August 2010, 00:22
I think you might indeed need a QTimer whose timeout() signal is connected to a custom slot that updates the QLabel.

GuangZor
19th August 2010, 06:37
Thank you Chrisw67, for the code. Now its clear enough for me. Lykurg, yes I wanted to make an analog clock.

Lykurg
19th August 2010, 08:20
Lykurg, yes I wanted to make an analog clock.Ok, then the elapsed time is useless. There is a good example in the docs. See Digital Clock Example. (http://doc.trolltech.com/latest/widgets-digitalclock.html)

aamer4yu
19th August 2010, 08:43
Thats digital :D

Here's Analog example (http://doc.trolltech.com/4.6/widgets-analogclock.html)

selmi.asma
26th June 2013, 12:26
Thanck you so much ChrisW67,you litterately saved me ;)

ChrisW67
27th June 2013, 00:22
You're welcome. Welcome to Qt Centre

teka321
19th April 2017, 20:35
maybe better:

MyClass::MyClass(QWidget *parent) :
QWidget(parent)
{
QTimer *timer = new QTimer(this);
connect(timer, SIGNAL(timeout()), this, SLOT(updateDisplayTime()));
timer->start(1000);
}


void MyClass::updateDisplayTime()
{
ui->labelTime->setText(QDateTime::currentDateTime().toString("dddd h:m\ndd.MM.yyyyr.") );
}