Results 1 to 15 of 15

Thread: Elapsed Time on a QLabel. How to do it?

  1. #1
    Join Date
    Aug 2010
    Posts
    4
    Thanks
    3
    Qt products
    Qt4
    Platforms
    Windows

    Default Elapsed Time on a QLabel. How to do it?

    Qt Code:
    1. void MainWindow::updateETLabel(){
    2. QString text = QTime::currentTime().toString("hh:mm:ss");
    3. setETLabelText(text);
    4. }
    5.  
    6. //MainWindow constructor:
    7. ETTimer = new QTimer(this);
    8. connect(ETTimer,SIGNAL(timeout()),this,SLOT(updateETLabel()));
    9.  
    10. //StartButton:
    11. ETTimer->start(1000);
    To copy to clipboard, switch view to plain text mode 

    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!

  2. #2
    Join Date
    Jan 2006
    Location
    Germany
    Posts
    4,380
    Thanks
    19
    Thanked 1,005 Times in 913 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Symbian S60
    Wiki edits
    5

    Default Re: Elapsed Time on a QLabel. How to do it?

    Simple use:
    Qt Code:
    1. t.start();
    2. //...
    3. t.elapsed();
    To copy to clipboard, switch view to plain text mode 
    => QTime::elapsed()

  3. The following 2 users say thank you to Lykurg for this useful post:

    frankiefrank (14th October 2012), GuangZor (18th August 2010)

  4. #3
    Join Date
    Aug 2010
    Posts
    4
    Thanks
    3
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Elapsed Time on a QLabel. How to do it?

    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.

  5. #4
    Join Date
    Jul 2008
    Location
    Germany
    Posts
    506
    Thanks
    11
    Thanked 76 Times in 74 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Elapsed Time on a QLabel. How to do it?

    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

  6. The following user says thank you to Ginsengelf for this useful post:

    GuangZor (18th August 2010)

  7. #5
    Join Date
    Jan 2006
    Location
    Germany
    Posts
    4,380
    Thanks
    19
    Thanked 1,005 Times in 913 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Symbian S60
    Wiki edits
    5

    Default Re: Elapsed Time on a QLabel. How to do it?

    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

  8. #6
    Join Date
    Aug 2010
    Posts
    4
    Thanks
    3
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Elapsed Time on a QLabel. How to do it?

    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)...

  9. #7
    Join Date
    Jan 2006
    Location
    Germany
    Posts
    4,380
    Thanks
    19
    Thanked 1,005 Times in 913 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Symbian S60
    Wiki edits
    5

    Default Re: Elapsed Time on a QLabel. How to do it?

    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.

  10. #8
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: Elapsed Time on a QLabel. How to do it?

    Something along these lines:
    Qt Code:
    1. #include <QtGui>
    2.  
    3. class mainwindow: public QMainWindow {
    4. Q_OBJECT
    5. public:
    6. mainwindow(QWidget *p = 0): QMainWindow(p) {
    7. timeLabel = new QLabel(this);
    8. setCentralWidget(timeLabel);
    9. time.start();
    10. updateDisplay();
    11. connect(&timer, SIGNAL(timeout()), this, SLOT(updateDisplay()));
    12. timer.start(500); // twice per second
    13. }
    14. public slots:
    15. void updateDisplay() {
    16. int secs = time.elapsed() / 1000;
    17. int mins = (secs / 60) % 60;
    18. int hours = (secs / 3600);
    19. secs = secs % 60;
    20. timeLabel->setText(QString("%1:%2:%3")
    21. .arg(hours, 2, 10, QLatin1Char('0'))
    22. .arg(mins, 2, 10, QLatin1Char('0'))
    23. .arg(secs, 2, 10, QLatin1Char('0')) );
    24. }
    25. private:
    26. QLabel *timeLabel;
    27. QTime time;
    28. QTimer timer;
    29. };
    30.  
    31. int main(int argc, char *argv[])
    32. {
    33. QApplication app(argc, argv);
    34.  
    35. mainwindow m;
    36. m.show();
    37. return app.exec();
    38. }
    39. #include "main.moc"
    To copy to clipboard, switch view to plain text mode 

  11. The following user says thank you to ChrisW67 for this useful post:

    GuangZor (19th August 2010)

  12. #9
    Join Date
    May 2009
    Location
    Canada
    Posts
    163
    Thanks
    7
    Thanked 20 Times in 20 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Windows Android

    Default Re: Elapsed Time on a QLabel. How to do it?

    I think you might indeed need a QTimer whose timeout() signal is connected to a custom slot that updates the QLabel.

  13. #10
    Join Date
    Aug 2010
    Posts
    4
    Thanks
    3
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Elapsed Time on a QLabel. How to do it?

    Thank you Chrisw67, for the code. Now its clear enough for me. Lykurg, yes I wanted to make an analog clock.

  14. #11
    Join Date
    Jan 2006
    Location
    Germany
    Posts
    4,380
    Thanks
    19
    Thanked 1,005 Times in 913 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Symbian S60
    Wiki edits
    5

    Default Re: Elapsed Time on a QLabel. How to do it?

    Quote Originally Posted by GuangZor View Post
    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.

  15. #12
    Join Date
    Oct 2006
    Location
    New Delhi, India
    Posts
    2,467
    Thanks
    8
    Thanked 334 Times in 317 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Elapsed Time on a QLabel. How to do it?

    Thats digital

    Here's Analog example

  16. #13
    Join Date
    Jun 2013
    Posts
    1
    Thanks
    2
    Qt products
    Qt5
    Platforms
    Windows

    Thumbs up Re: Elapsed Time on a QLabel. How to do it?

    Thanck you so much ChrisW67,you litterately saved me

  17. #14
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: Elapsed Time on a QLabel. How to do it?

    You're welcome. Welcome to Qt Centre

  18. #15
    Join Date
    May 2013
    Posts
    1
    Qt products
    Qt5

    Default Re: Elapsed Time on a QLabel. How to do it?

    maybe better:
    Qt Code:
    1. MyClass::MyClass(QWidget *parent) :
    2. QWidget(parent)
    3. {
    4. QTimer *timer = new QTimer(this);
    5. connect(timer, SIGNAL(timeout()), this, SLOT(updateDisplayTime()));
    6. timer->start(1000);
    7. }
    8.  
    9.  
    10. void MyClass::updateDisplayTime()
    11. {
    12. ui->labelTime->setText(QDateTime::currentDateTime().toString("dddd h:m\ndd.MM.yyyyr.") );
    13. }
    To copy to clipboard, switch view to plain text mode 
    Last edited by wysota; 19th April 2017 at 20:38. Reason: missing [code] tags

Similar Threads

  1. Replies: 6
    Last Post: 18th August 2010, 12:52
  2. Replies: 1
    Last Post: 25th June 2010, 18:31
  3. Replies: 1
    Last Post: 29th September 2009, 19:44
  4. Replies: 1
    Last Post: 2nd August 2008, 15:46
  5. check the time of last time keyboard is pressed
    By Aki Tomar in forum General Programming
    Replies: 2
    Last Post: 5th February 2008, 09:10

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.