Results 1 to 5 of 5

Thread: Displaying a live timer in a window

  1. #1
    Join Date
    Jan 2014
    Posts
    2
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Displaying a live timer in a window

    Hi guys

    I've been trying to use QTcreator to display a timer in a dialog or window. I start the timer, then use a delay function, then measure the time elapsed. My goal is to continuously display the time elapsed onto the window, as if I had a live timer, but it seems that every time I output the time elapsed to the window, it only outputs the final time ie if I use a delay of 5 seconds, then my window takes 5 seconds to open, then it displays 5 seconds; when in fact i want the window to open instantly and diplay 1,2,3,4,5.

    Here's what I have so far:

    Qt Code:
    1. void delay()
    2. {
    3. QTime dieTime= QTime::currentTime().addSecs(3);
    4. while( QTime::currentTime() < dieTime )
    5. QCoreApplication::processEvents(QEventLoop::AllEvents, 100);
    6. }
    7.  
    8.  
    9. MainWindow::MainWindow(QWidget *parent) :
    10. QMainWindow(parent),
    11. ui(new Ui::MainWindow)
    12. {
    13. ui->setupUi(this);
    14. QElapsedTimer timer;
    15. timer.start();
    16. delay();
    17. connect(timer.elapsed(),SIGNAL(valueChange(int)), ui->progressBar,SLOT(setValue(int))); // but apparently timer.elapsed doesn't work as a const QObject
    To copy to clipboard, switch view to plain text mode 

    Then I tried another approach
    Qt Code:
    1. for (int i=1; i<=3000; i++)
    2. while (!timer.hasExpired(3000))
    3. {
    4. ui->lineEdit->setText(QString::number(timer.elapsed())); // trying to display timer in line edit
    5. ui->progressBar->setValue(timer.elapsed()/30); // trying to use a progress bar to track the time
    6. }
    To copy to clipboard, switch view to plain text mode 

    Does anyone have good pointers to help me solve this?

    Thanks in advance

    -Emara

  2. #2
    Join Date
    Jan 2009
    Location
    The Netherlands and Spain
    Posts
    150
    Thanks
    6
    Thanked 18 Times in 18 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Displaying a live timer in a window

    Create a QTimer.
    Connect the timeout() signal to a slot.
    Set interval.
    Start the timer.
    Have fun.

  3. #3
    Join Date
    Jan 2014
    Posts
    2
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Displaying a live timer in a window

    I've tried that now, but now it doesn't even update the time. I changed the Second "this" to ui->progressBar, and the progress bar just shows me 24.
    Do you mind expanding your answer a little bit more? I just started using QT last week so I might be doing something else wrong.

    Thank you for your reply.

  4. #4
    Join Date
    Jan 2009
    Location
    The Netherlands and Spain
    Posts
    150
    Thanks
    6
    Thanked 18 Times in 18 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Displaying a live timer in a window

    Start reading this:
    http://qt-project.org/doc/qt-4.8/qtimer.html

    I don't think a QElapsedTimer is the right class to use, but I might be misunderstanding your goal...

  5. #5
    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: Displaying a live timer in a window

    Use QTime::start() to start recording elapsed time on a QTime object. Use QTimer (or some other mechanism) to periodically fire a slot and update a display with the current QTime::elapsed() time from the QTime object. No loops, no processEvents() calls.

Similar Threads

  1. Displaying plot in main QT window
    By Gavin Harper in forum Qwt
    Replies: 1
    Last Post: 23rd August 2010, 13:59
  2. Displaying 2 QApplication's in single window
    By masandeep in forum Qt Programming
    Replies: 1
    Last Post: 15th September 2009, 08:05
  3. Displaying DOS Window with QProcess
    By GTBuilder in forum Newbie
    Replies: 0
    Last Post: 19th February 2008, 02:26
  4. Problem displaying my main window
    By Salazaar in forum Newbie
    Replies: 104
    Last Post: 16th January 2008, 14:01
  5. New to QT, problem displaying main window...
    By McCall in forum Qt Programming
    Replies: 4
    Last Post: 15th June 2007, 14:27

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.