Results 1 to 7 of 7

Thread: Update time on screen using a thread.

Threaded View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Join Date
    Nov 2011
    Posts
    4
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Update time on screen using a thread.

    I need to use threads for on screen time update and to interface with a serial port. I have tried to impliment code I have found in examples and I always end up preventing my application from showing up. How do I have a thread run in the background (like incrimenting the time) while the application runs in paralel? Here is the thread code for the time function.

    thread code in application
    Qt Code:
    1. class MegaStoreMain : public QGroupBox
    2. {
    3. Q_OBJECT
    4.  
    5. public:
    6. explicit MegaStoreMain(QWidget *parent = 0);
    7. ~MegaStoreMain();
    8.  
    9.  
    10. ControlProgram controlProgram;
    11. private:
    12. Ui::MegaStoreMain *ui;
    13. QTime Time;
    14. };
    15. MegaStoreMain::MegaStoreMain(QWidget *parent) :
    16. QGroupBox(parent),
    17. ui(new Ui::MegaStoreMain)
    18. {
    19. ui->setupUi(this);
    20. ui->tbIDEntry->isReadOnly();
    21. ui->leEmployeeNo->isReadOnly();
    22. FirstEntry = FALSE;
    23. PasswordEntry = FALSE;
    24. PasswordID = FALSE;
    25. EnteredPassword = "";
    26. ui->labelEmpoyeeID->setVisible(FALSE);
    27. ui->labelPassword->setVisible(FALSE);
    28. ui->password->setVisible(FALSE);
    29. ui->userID->setVisible(FALSE);
    30. ui->RunningTime->setVisible(TRUE);
    31. connect(&controlProgram, SIGNAL(RunTimeUpdate(TimeString)),
    32. this, SLOT(UpdateTime(String)));
    33. Time.start();
    34. Time.currentTime();
    35. int Hour = Time.hour();
    36. int Minute = Time.minute();
    37. int Second = Time.second();
    38. char buf[20];
    39. sprintf(buf,"%d:%d:%d",Hour,Minute,Second);
    40. ui->RunningTime->setText(buf);
    41. }
    42. void MegaStoreMain::UpdateTime(char* TimeString)
    43. {
    44. ui->RunningTime->setText(TimeString);
    45. }
    To copy to clipboard, switch view to plain text mode 
    header
    Qt Code:
    1. #include <QMutex>
    2. #include <QSize>
    3. #include <QThread>
    4. #include <QTime>
    5. #include <QWaitCondition>
    6.  
    7.  
    8. class ControlProgram : public QThread
    9. {
    10. Q_OBJECT
    11.  
    12. public:
    13. ControlProgram(QObject *parent = 0);
    14. ~ControlProgram();
    15.  
    16. QTime SessionTime;
    17. QTime RunningTime;
    18. int Hour;
    19. int Minute;
    20. int Second;
    21.  
    22.  
    23. signals:
    24. void RunTimeUpdate(char* TimeString);
    25.  
    26. protected:
    27. void run();
    28.  
    29. private:
    30.  
    31. QMutex mutex;
    32. QWaitCondition condition;
    33. bool restart;
    34. bool abort;
    35.  
    36. };
    To copy to clipboard, switch view to plain text mode 
    source
    Qt Code:
    1. #include <QtGui>
    2.  
    3. #include <math.h>
    4.  
    5. #include "controlProgram.h"
    6.  
    7. ControlProgram::ControlProgram(QObject *parent)
    8. : QThread(parent)
    9. {
    10. restart = false;
    11. abort = false;
    12. run();
    13.  
    14. }
    15.  
    16. ControlProgram::~ControlProgram()
    17. {
    18. mutex.lock();
    19. abort = true;
    20. condition.wakeOne();
    21. mutex.unlock();
    22.  
    23. wait();
    24. }
    25.  
    26.  
    27. void ControlProgram::run()
    28. {
    29. SessionTime.start();
    30. SessionTime.currentTime();
    31. RunningTime.start();
    32. RunningTime.currentTime();
    33. // forever
    34. {
    35. mutex.lock();
    36. Hour = RunningTime.hour();
    37. Minute = RunningTime.minute();
    38. Second = RunningTime.second();
    39. char buf[20];
    40. sprintf(buf,"%d:%d:%d",Hour,Minute,Second);
    41. mutex.unlock();
    42. emit RunTimeUpdate(&buf[0]);
    43. if (!restart)
    44. condition.wait(&mutex);
    45. restart = false;
    46. // sleep(1);
    47. }
    48. }
    To copy to clipboard, switch view to plain text mode 
    Last edited by TFH; 10th November 2011 at 22:27.

Similar Threads

  1. Lock screen after inactivity time
    By baobui in forum Qt for Embedded and Mobile
    Replies: 1
    Last Post: 20th March 2011, 22:18
  2. Replies: 0
    Last Post: 5th November 2010, 17:46
  3. How to find out scene update time
    By nileshsince1980 in forum Qt Programming
    Replies: 5
    Last Post: 20th September 2007, 09:46
  4. hide/show screen update problem
    By SkripT in forum Qt Programming
    Replies: 2
    Last Post: 6th February 2006, 17:49
  5. Problem with screen update...
    By mysearch05 in forum Qt Programming
    Replies: 2
    Last Post: 27th January 2006, 18:24

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.