Results 1 to 7 of 7

Thread: Update time on screen using a thread.

  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.

  2. #2
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: Update time on screen using a thread.

    Please edit your post to fix the closing [/code] tags (a forward slash).

    I need to use threads for on screen time update and to interface with a serial port.
    Do you really need to use threads? How are you doing the serial communications?

  3. #3
    Join Date
    Nov 2011
    Posts
    4
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Update time on screen using a thread.

    I was going to use the threads for the time as a way of getting a working thread.

    The serial port is the more pressing need as I will be going between screens depending on some serial feedback. I will also be sending some commands by doing screen functions.

  4. #4
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: Update time on screen using a thread.

    You can have an updating clock while other things are happening without worrying yourself with explicit threads provided the 'other things' are well behaved.

    How are you doing the serial communication? Using QExtSerialPort?

  5. #5
    Join Date
    Nov 2011
    Posts
    4
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Update time on screen using a thread.

    I am not using the Qextserialport. I have code that accesses the serial port and when I call the thread when the application starts up, I send characters out of the serial port and read data back. The thread then dies and I am stuck. So getting the thread running is my priority.

    Thanks

  6. #6
    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: Update time on screen using a thread.

    First of all to update time on screen You don't need extra thread. Just use a QTimer with a signal timeout() connected to the slot in MegaStoreMain. Something like this :
    Qt Code:
    1. MegaStoreMain::MegaStoreMain(QWidget *parent) :
    2. QGroupBox(parent),
    3. ui(new Ui::MegaStoreMain)
    4. {
    5. ui->setupUi(this);
    6. ui->tbIDEntry->isReadOnly();
    7. ui->leEmployeeNo->isReadOnly();
    8. FirstEntry = FALSE;
    9. PasswordEntry = FALSE;
    10. PasswordID = FALSE;
    11. EnteredPassword = "";
    12. ui->labelEmpoyeeID->setVisible(FALSE);
    13. ui->labelPassword->setVisible(FALSE);
    14. ui->password->setVisible(FALSE);
    15. ui->userID->setVisible(FALSE);
    16. ui->RunningTime->setVisible(TRUE);
    17.  
    18. QTimer *m_timer = new QTimer(this);
    19. connect(m_timer, SIGNAL(timnout()),this, SLOT(UpdateTime()));
    20. Time.start();
    21. m_timer->start(1000);//updating time on screen every 1 sec.
    22. }
    23. void MegaStoreMain::UpdateTime(void)
    24. {
    25. ui->RunningTime->setText(Time.currentTime().toString("HH:mm:ss");
    26. }
    To copy to clipboard, switch view to plain text mode 
    P.S.
    This is a bad idea to use the QTime to measure time. First QTime is working in 24 hours period. Second He is not immune to change summer / winter time. For this purpose use QElapsedTime. But from other side I don't know what You want using QTimer::start() and QTimer::currentTime().
    Last edited by Lesiok; 11th November 2011 at 17:55.

  7. #7
    Join Date
    Nov 2011
    Posts
    4
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Update time on screen using a thread.

    Thank you for the update Lesiok. I have used a signal from a timer event to get the time portion working. The bigest reason I was trying to use a thread for the time is to see how threads work, because the greatest need I have for the thread is to interface with a serial port. So any help I can get with gaining a good understanding about threads would be much needed.

    TFH

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.