Results 1 to 13 of 13

Thread: QTime

  1. #1
    Join Date
    Jan 2006
    Posts
    115
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default QTime

    Hello anyone,

    I ám a newbie,
    I have a question about QTime and QTimer.
    I have read the posts in the forum about QTime and Qtimer but it gives me not the solution that i want.

    I want to countdown the Time(not the currentTime)in lcdNumber. Example(05:00:00->04:59:59...)
    Is it possible to do it in QTime ?.


    See my implentation:
    Qt Code:
    1. countdownDialog::countdownDialog(QWidget *parent)
    2. :QDialog(parent)
    3.  
    4. {
    5.  
    6. setupUi(this);
    7.  
    8.  
    9. QTimer *timer = new QTimer(this);
    10. connect(timer, SIGNAL(timeout()), this, SLOT(displayTime()));
    11. timer->start(1000);
    12.  
    13. }
    14. void countdownDialog::displayTime()
    15. {
    16. QTime *time = new QTime();
    17. time->setHMS(5,0,0,0);
    18. lcdNumber->display(time->toString());
    19.  
    20. }
    21.  
    22. Thanks in advance.
    To copy to clipboard, switch view to plain text mode 

  2. #2
    Join Date
    Feb 2006
    Location
    Oslo, Norway
    Posts
    6,264
    Thanks
    36
    Thanked 1,519 Times in 1,389 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: QTime

    Let's start with fixing an obvious memory leak. Here "time" is allocated on the heap but never deleted:
    Quote Originally Posted by dragon View Post
    Qt Code:
    1. void countdownDialog::displayTime()
    2. {
    3. QTime *time = new QTime();
    4. time->setHMS(5,0,0,0);
    5. lcdNumber->display(time->toString());
    6. }
    To copy to clipboard, switch view to plain text mode 
    Just for the learning purposes, you should start with rewriting it as:
    Qt Code:
    1. void countdownDialog::displayTime()
    2. {
    3. QTime time;
    4. time.setHMS(5,0,0,0);
    5. lcdNumber->display(time.toString());
    6. }
    To copy to clipboard, switch view to plain text mode 
    Now, "time" get automatically destructed while going out of scope.

    The next thing is to actually implement some counter functionality. All the above code does is converting the same time (05:00:00) over and over again to a string and setting it to the lcd number widget. You could make the time object a member variable instead and reduce a second every iteration:
    Qt Code:
    1. class countdownDialog
    2. {
    3. ...
    4. private:
    5. QTime time; // declare a member variable
    6. }
    7.  
    8. countdownDialog::countdownDialog(QWidget *parent)
    9. :QDialog(parent)
    10. {
    11. ...
    12. // initialize it to anything you want,
    13. // for example 5 hours as you did before in displayTime()
    14. time = QTime(5, 0, 0, 0);
    15. }
    16.  
    17. void countdownDialog::displayTime()
    18. {
    19. time = time.addSecs(-1); // reduce a second
    20. lcdNumber->display(time.toString());
    21. }
    To copy to clipboard, switch view to plain text mode 
    J-P Nurmi

  3. #3
    Join Date
    Jan 2006
    Posts
    115
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: QTime

    Thanks J-P Nurmi for advise its works fine now.

  4. #4
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: QTime

    A bit more precise solution is to substract the elapsed time from 5 hours every time. It may happen that a timer event gets stalled, so substracting a second every time it is triggered might get your timer out of sync. But if you don't expect a heavy load, it should work fine too.

  5. #5
    Join Date
    Jan 2006
    Posts
    115
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: QTime

    I have a question i only see the hours, minutes, seconds in the lcdNumber.
    I.don't see the milliseconds.
    Is this depending on the system where the program is running on.
    How can i change the code to see the milliseconds and counting down in lcdNumber.

    thanks in advance.

  6. #6
    Join Date
    Feb 2006
    Location
    Romania
    Posts
    2,744
    Thanks
    8
    Thanked 541 Times in 521 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QTime

    instead of time.toString() you can build your own representation by using QTime::hour(), minute(), second() and msec().
    Just convert them to QString and concatenate them.

    Regards

  7. #7
    Join Date
    Feb 2006
    Location
    Romania
    Posts
    2,744
    Thanks
    8
    Thanked 541 Times in 521 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QTime


    That, or:
    Qt Code:
    1. time.toString("hh:mm:ss.zzz");
    To copy to clipboard, switch view to plain text mode 
    There are other formatters from which you can choose.

    Regards

  8. #8
    Join Date
    Jan 2006
    Posts
    115
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: QTime

    I see in my lcdNumber the milliseconds but is not counting down only the seconds is counting down.
    Must i time.addMSecs to the code?

  9. #9
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: QTime

    You are only reducing the number of seconds so how do you expect anything else to change?

  10. #10
    Join Date
    Feb 2006
    Location
    Romania
    Posts
    2,744
    Thanks
    8
    Thanked 541 Times in 521 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QTime

    Yes, addMsecs(-x), where x is smaller than 1000, in order to see a difference.

    Regards

  11. #11
    Join Date
    Jan 2006
    Posts
    115
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: QTime

    Thanks Marcel and Wysota for your advise.

  12. #12
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: QTime

    Or try my approach

  13. #13
    Join Date
    Feb 2012
    Posts
    19
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: QTime

    hello.

    is it possible to add a start, reset and pause button

    but already thank you for what i see here :-)

Similar Threads

  1. QTime private member mds means what?
    By jamadagni in forum Qt Programming
    Replies: 2
    Last Post: 24th March 2006, 19:08
  2. Extending QDate, QTime and QDateTime
    By jamadagni in forum Qt Programming
    Replies: 8
    Last Post: 24th March 2006, 16:51

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.