Results 1 to 10 of 10

Thread: Problem in feteching time and date.

  1. #1
    Join Date
    May 2011
    Posts
    120
    Thanks
    9
    Qt products
    Qt3 Qt4
    Platforms
    Windows

    Default Problem in feteching time and date.

    Hello everyone
    I have facing problem in feteching time and date.The date and time i fetched but the problem is it is not updating with the system time and date. When i run the application it is giving the time and date but if the application is open then the time is not getting update , it update when i again run the application.

    One more thing the format of time is in hr.min.sec while i want to fetch it like hr.min.
    Thank you for help

  2. #2
    Join Date
    Nov 2010
    Posts
    315
    Thanked 53 Times in 51 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: Problem in feteching time and date.


  3. #3
    Join Date
    May 2011
    Posts
    120
    Thanks
    9
    Qt products
    Qt3 Qt4
    Platforms
    Windows

    Default Re: Problem in feteching time and date.

    i have seen the example and my code is
    Qt Code:
    1. QTime time = QTime::currentTime();
    2. QString timeString = time.toString();
    3. label->setText(timeString);
    4. startTimer(1000);
    To copy to clipboard, switch view to plain text mode 

    but the time is not getting update. and the format i also have to change. How can i do all that.

  4. #4
    Join Date
    Jun 2011
    Posts
    7
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Problem in feteching time and date.

    you have to specify a time format in the toString()-method... see documentation.

    Where is that snippet of your code? Is it in a slot which is called every second? Did you connect the timeout()-Signal from the QTimer to this slot?
    What is the startTimer()-function? Did you write it on your own to call timer.start() or do you use the QObject::startTimer()-function? In the second case you need to reimplement the timerEvent(QTimerEvent *)-function.

  5. #5
    Join Date
    May 2011
    Posts
    120
    Thanks
    9
    Qt products
    Qt3 Qt4
    Platforms
    Windows

    Default Re: Problem in feteching time and date.

    I implement timerEvent(QTimerEvent *)-function and in that function i update the time like
    Qt Code:
    1. label->setText(timeString);
    To copy to clipboard, switch view to plain text mode 
    but the time is not getting update.

  6. #6
    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: Problem in feteching time and date.

    Do you re-ask for the time in the timer event?
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  7. #7
    Join Date
    May 2011
    Posts
    120
    Thanks
    9
    Qt products
    Qt3 Qt4
    Platforms
    Windows

    Default Re: Problem in feteching time and date.

    No i am not reasking time in timer event. i just update the label text in timer event.

  8. #8
    Join Date
    Mar 2011
    Location
    Hyderabad, India
    Posts
    1,882
    Thanks
    3
    Thanked 452 Times in 435 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows
    Wiki edits
    15

    Default Re: Problem in feteching time and date.

    Check your code against this

    Qt Code:
    1. class MyClass : QObject {
    2. ...
    3. protected:
    4. void timerEvent(QTimerEvent * event);
    5. }
    6.  
    7. void MyClass::MyClass(QObject * parent)
    8. {
    9. ...
    10. mytime = startTimer(1000);
    11. }
    12.  
    13. void MyClass::timerEvent(QTimerEvent * event)
    14. {
    15. QTime time = QTime::currentTime();
    16. QString timeString = time.toString("hh:mm");
    17. label->setText(timeString);
    18. }
    To copy to clipboard, switch view to plain text mode 

  9. The following user says thank you to Santosh Reddy for this useful post:

    Niamita (15th June 2011)

  10. #9
    Join Date
    May 2011
    Posts
    120
    Thanks
    9
    Qt products
    Qt3 Qt4
    Platforms
    Windows

    Default Re: Problem in feteching time and date.

    Hi
    Thaks for the answer but i am facing with a problem that i set timer at two places one to update paint and one to update time , but both are not working right, they collapsed.
    How to handle both of them simultanously.

  11. #10
    Join Date
    Mar 2011
    Location
    Hyderabad, India
    Posts
    1,882
    Thanks
    3
    Thanked 452 Times in 435 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows
    Wiki edits
    15

    Default Re: Problem in feteching time and date.

    Quote Originally Posted by Niamita
    i set timer at two places one to update paint and one to update time
    Ok you can use two timers like this

    Qt Code:
    1. class MyClass : QObject {
    2. ...
    3. protected:
    4. void timerEvent(QTimerEvent * event);
    5.  
    6. private:
    7. int timeUpdateTimer;
    8. int paintUpdateTimer;
    9. }
    10.  
    11. void MyClass::MyClass(QObject * parent)
    12. {
    13. timeUpdateTimer = 0; // Just to be safe.
    14. paintUpdateTimer = 0;
    15. ...
    16. timeUpdateTimer = startTimer(1000); // Update time every 1 second
    17. paintUpdateTimer= startTimer(500); // Update paint every 0.5 second, you can move this to any place in you class
    18. }
    19.  
    20. void MyClass::timerEvent(QTimerEvent * event)
    21. {
    22. if(timeUpdateTimer == event->timerID())
    23. {
    24. QTime time = QTime::currentTime();
    25. QString timeString = time.toString("hh:mm");
    26. label->setText(timeString);
    27. }
    28. else if(paintUpdateTimer == event->timerID())
    29. {
    30. widget->update(); //update paint here
    31. }
    32. else
    33. {
    34. killTimer(event->timerId()); //Have this, till you clearly understand how QObject, abd QTimers work
    35. }
    36. }
    To copy to clipboard, switch view to plain text mode 

Similar Threads

  1. QTimer problem with Date/Time
    By bhavikdhoot in forum Qt Programming
    Replies: 2
    Last Post: 12th May 2011, 11:12
  2. Date Time
    By javed_alam786 in forum Qt Programming
    Replies: 2
    Last Post: 11th May 2011, 14:52
  3. other time...can't get date
    By mmm286 in forum Newbie
    Replies: 7
    Last Post: 4th March 2010, 15:53
  4. Date Time
    By starcontrol in forum Qt Programming
    Replies: 3
    Last Post: 4th April 2008, 11:02
  5. date-time problem
    By aegis in forum Qt Programming
    Replies: 5
    Last Post: 11th February 2007, 18:45

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.