Results 1 to 5 of 5

Thread: Qt timer problem

  1. #1
    Join Date
    Oct 2014
    Posts
    71
    Thanks
    13
    Qt products
    Qt5
    Platforms
    Windows

    Post Qt timer problem

    Hello,

    I want to add a time on my design which starts with formate 00:00:00 and my number should keep on increasing every seconds.
    I tried to add the code
    Qt Code:
    1. showtime()
    2. {
    3. QTime time =QTime::currentTime();
    4. QString time_text = time.toString("hh:mm:ss");
    5. ui.clock->setText(time_text);
    6. }
    7. constructor()
    8. {
    9. QTimer *timer = new QTimer(this);
    10. connect(timer,SIGNAL(timeout()),this,SLOT(showtime()));
    11. timer->start();
    12. }
    To copy to clipboard, switch view to plain text mode 

    but I am getting my system time.
    I want it should start from zero and keep on increasing every second.
    Please let me know some solution.

  2. #2
    Join Date
    Feb 2012
    Location
    Warsaw, Poland
    Posts
    37
    Thanks
    3
    Thanked 6 Times in 6 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows Android

    Default Re: Qt timer problem

    I guess this solution is sufficient for you

    Qt Code:
    1. constructor : m_time(QTime(0,0,0))
    2. {
    3. ui.clock->setText(m_time.toString("hh:mm:ss"));
    4. }
    5.  
    6. showtime
    7. {
    8. m_time = m_time.addSecs(1);
    9. ui.clock->setText(m_time.toString("hh:mm:ss"));
    10. }
    To copy to clipboard, switch view to plain text mode 


    Added after 6 minutes:


    To be more precise:

    Qt Code:
    1. constructor : m_time(QTime::currentTime())
    2. {
    3. ui.clock->setText(QTime(0,0,0).toString("hh:mm:ss"));
    4. }
    5.  
    6. showtime
    7. {
    8. ui.clock->setText(QTime(0,0,0).addSecs(m_time.secsTo(QTime::currentTime())).toString("hh:mm:ss"));
    9. }
    To copy to clipboard, switch view to plain text mode 
    Last edited by west; 14th October 2014 at 16:24.

  3. The following user says thank you to west for this useful post:

    anh5kor (15th October 2014)

  4. #3
    Join Date
    Mar 2008
    Location
    Kraków, Poland
    Posts
    1,536
    Thanked 284 Times in 279 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Qt timer problem

    Use QElapsedTimer instead of QTimer.

  5. #4
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Qt timer problem

    Quote Originally Posted by Lesiok View Post
    Use QElapsedTimer instead of QTimer.
    You meant QElapsedTimer instead of QTime?

    QTimer is an "active" class, it has a signal.

    Cheers,
    _

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

    anh5kor (15th October 2014)

  7. #5
    Join Date
    Mar 2008
    Location
    Kraków, Poland
    Posts
    1,536
    Thanked 284 Times in 279 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Qt timer problem

    Quote Originally Posted by anda_skoa View Post
    You meant QElapsedTimer instead of QTime?

    QTimer is an "active" class, it has a signal.

    Cheers,
    _
    Of course QTime, thanks anda_skoa for correction.

  8. The following user says thank you to Lesiok for this useful post:

    anh5kor (15th October 2014)

Similar Threads

  1. Replies: 6
    Last Post: 4th September 2014, 09:06
  2. problem with start timer
    By ramin.lich in forum Newbie
    Replies: 1
    Last Post: 31st August 2014, 13:57
  3. Strange problem with timer
    By nagabathula in forum Qt Programming
    Replies: 12
    Last Post: 13th April 2011, 10:13
  4. timer problem(timer does not work)
    By masuk in forum Newbie
    Replies: 6
    Last Post: 14th February 2011, 05:00
  5. Problem about timer in multithread programs
    By vql in forum General Programming
    Replies: 4
    Last Post: 17th October 2007, 15:00

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.