Results 1 to 6 of 6

Thread: How to make the time run in a GUI using QThreads ?

  1. #1
    Join Date
    May 2013
    Posts
    47
    Qt products
    Qt5
    Platforms
    Windows

    Default How to make the time run in a GUI using QThreads ?

    Hello all ,
    I am Making a Gui to show a running time in a text label,I tried doing it buut i am only to make a time stamp ,I cannot make the time run at all.
    It seems to me that the only method to do it is by using thread . I am unfamiliar with thread at all .Let alone QThread.Is there any other way to make the time run ?
    Or can someone help me out with threads ? Please help thank you in advance .

  2. #2
    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: How to make the time run in a GUI using QThreads ?

    One quick way
    Qt Code:
    1. class TimeLabel : public QLabel
    2. {
    3. Q_OBJECT
    4. public:
    5. explicit TimeLabel(QWidget * parent = 0)
    6. : QLabel(parent)
    7. {
    8. startTimer(50); // Update time every 50 ms
    9. }
    10. protected:
    11. void timerEvent(QTimerEvent *)
    12. {
    13. setText(QDateTime::currentDateTime().toString());
    14. }
    15. };
    16.  
    17. int main(int argc, char ** argv)
    18. {
    19. QApplication app(argc, argv);
    20.  
    21. TimeLabel label;
    22. label.show();
    23.  
    24. return app.exec();
    25. }
    26. #include "main.moc"
    To copy to clipboard, switch view to plain text mode 
    When you know how to do it then you may do it wrong.
    When you don't know how to do it then it is not that you may do it wrong but you may not do it right.

  3. #3
    Join Date
    May 2013
    Posts
    47
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: How to make the time run in a GUI using QThreads ?

    Hey thanks for the quick reply ,All i have to do is jus make a header file for that right and implent the label i need in the cpp file .I do not need to make a Cpp file for time label right ? just the label ?

  4. #4
    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: How to make the time run in a GUI using QThreads ?

    It's up to you, you could any way.
    When you know how to do it then you may do it wrong.
    When you don't know how to do it then it is not that you may do it wrong but you may not do it right.

  5. #5
    Join Date
    May 2013
    Posts
    47
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: How to make the time run in a GUI using QThreads ?

    Ok i seem to have a problem . I still dont understand how to implement that .for the class timelabel..... it is suppose to be in a new header file right ?
    I am not too sure how threading works or what , so the codes i dont understand at all.
    what in tried to do is creating a new C++ class in the project . and i tried using the codes in a header file. I gotten errors


    Added after 36 minutes:


    Please ignore the previous reply. I already solved it . For now i am having troubles . I tried calling it in the main .cpp but it seems that it will open a new window to display the time and date . What i want to do is to display it one my gui , For eg. LoginGui.cpp, which doesnt have a main(), what must i do to display it on the gui ?
    Last edited by 020394; 12th July 2013 at 07:38.

  6. #6
    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: How to make the time run in a GUI using QThreads ?

    Create a QTimer instance in the class of yours, add a slot that you connect the timer to, make the slot update whatever widget you are using to display the time.
    Start the timer with the desired update interval.

    Cheers,
    _

Similar Threads

  1. progress bar in QThreads
    By jackajack01 in forum Qt Programming
    Replies: 2
    Last Post: 25th August 2012, 00:43
  2. Setting the system time make the Qt program blocked.
    By alps_xing in forum Qt Programming
    Replies: 0
    Last Post: 10th June 2010, 08:12
  3. Tangled in QThreads
    By spawn9997 in forum Qt Programming
    Replies: 7
    Last Post: 14th October 2009, 22:00
  4. Qthreads
    By Sheetal in forum Qt Programming
    Replies: 5
    Last Post: 23rd March 2007, 11:12
  5. QTcpSockets and QThreads
    By TheRonin in forum Newbie
    Replies: 3
    Last Post: 21st June 2006, 09:41

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.