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?
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?
Last edited by GuangZor; 18th August 2010 at 10:43.
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
GuangZor (18th August 2010)
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
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)...
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.
Something along these lines:Qt Code:
#include <QtGui> Q_OBJECT public: 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; } private: QLabel *timeLabel; QTime time; QTimer timer; }; int main(int argc, char *argv[]) { mainwindow m; m.show(); return app.exec(); } #include "main.moc"To copy to clipboard, switch view to plain text mode
GuangZor (19th August 2010)
Thanck you so much ChrisW67,you litterately saved me![]()
You're welcome. Welcome to Qt Centre
maybe better:
Qt Code:
{ connect(timer, SIGNAL(timeout()), this, SLOT(updateDisplayTime())); timer->start(1000); } void MyClass::updateDisplayTime() { }To copy to clipboard, switch view to plain text mode
Last edited by wysota; 19th April 2017 at 20:38. Reason: missing [code] tags
I think you might indeed need a QTimer whose timeout() signal is connected to a custom slot that updates the QLabel.
Thank you Chrisw67, for the code. Now its clear enough for me. 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.
Thats digital
Here's Analog example
Bookmarks