PDA

View Full Version : Update time on screen using a thread.



TFH
10th November 2011, 19:07
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


class MegaStoreMain : public QGroupBox
{
Q_OBJECT

public:
explicit MegaStoreMain(QWidget *parent = 0);
~MegaStoreMain();


ControlProgram controlProgram;
private:
Ui::MegaStoreMain *ui;
QTime Time;
};
MegaStoreMain::MegaStoreMain(QWidget *parent) :
QGroupBox(parent),
ui(new Ui::MegaStoreMain)
{
ui->setupUi(this);
ui->tbIDEntry->isReadOnly();
ui->leEmployeeNo->isReadOnly();
FirstEntry = FALSE;
PasswordEntry = FALSE;
PasswordID = FALSE;
EnteredPassword = "";
ui->labelEmpoyeeID->setVisible(FALSE);
ui->labelPassword->setVisible(FALSE);
ui->password->setVisible(FALSE);
ui->userID->setVisible(FALSE);
ui->RunningTime->setVisible(TRUE);
connect(&controlProgram, SIGNAL(RunTimeUpdate(TimeString)),
this, SLOT(UpdateTime(String)));
Time.start();
Time.currentTime();
int Hour = Time.hour();
int Minute = Time.minute();
int Second = Time.second();
char buf[20];
sprintf(buf,"%d:%d:%d",Hour,Minute,Second);
ui->RunningTime->setText(buf);
}
void MegaStoreMain::UpdateTime(char* TimeString)
{
ui->RunningTime->setText(TimeString);
}


header


#include <QMutex>
#include <QSize>
#include <QThread>
#include <QTime>
#include <QWaitCondition>


class ControlProgram : public QThread
{
Q_OBJECT

public:
ControlProgram(QObject *parent = 0);
~ControlProgram();

QTime SessionTime;
QTime RunningTime;
int Hour;
int Minute;
int Second;


signals:
void RunTimeUpdate(char* TimeString);

protected:
void run();

private:

QMutex mutex;
QWaitCondition condition;
bool restart;
bool abort;

};


source


#include <QtGui>

#include <math.h>

#include "controlProgram.h"

ControlProgram::ControlProgram(QObject *parent)
: QThread(parent)
{
restart = false;
abort = false;
run();

}

ControlProgram::~ControlProgram()
{
mutex.lock();
abort = true;
condition.wakeOne();
mutex.unlock();

wait();
}


void ControlProgram::run()
{
SessionTime.start();
SessionTime.currentTime();
RunningTime.start();
RunningTime.currentTime();
// forever
{
mutex.lock();
Hour = RunningTime.hour();
Minute = RunningTime.minute();
Second = RunningTime.second();
char buf[20];
sprintf(buf,"%d:%d:%d",Hour,Minute,Second);
mutex.unlock();
emit RunTimeUpdate(&buf[0]);
if (!restart)
condition.wait(&mutex);
restart = false;
// sleep(1);
}
}

ChrisW67
10th November 2011, 22:57
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?

TFH
10th November 2011, 23:31
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.

ChrisW67
11th November 2011, 00:00
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?

TFH
11th November 2011, 16:34
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

Lesiok
11th November 2011, 18:48
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 :

MegaStoreMain::MegaStoreMain(QWidget *parent) :
QGroupBox(parent),
ui(new Ui::MegaStoreMain)
{
ui->setupUi(this);
ui->tbIDEntry->isReadOnly();
ui->leEmployeeNo->isReadOnly();
FirstEntry = FALSE;
PasswordEntry = FALSE;
PasswordID = FALSE;
EnteredPassword = "";
ui->labelEmpoyeeID->setVisible(FALSE);
ui->labelPassword->setVisible(FALSE);
ui->password->setVisible(FALSE);
ui->userID->setVisible(FALSE);
ui->RunningTime->setVisible(TRUE);

QTimer *m_timer = new QTimer(this);
connect(m_timer, SIGNAL(timnout()),this, SLOT(UpdateTime()));
Time.start();
m_timer->start(1000);//updating time on screen every 1 sec.
}
void MegaStoreMain::UpdateTime(void)
{
ui->RunningTime->setText(Time.currentTime().toString("HH:mm:ss");
}
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().

TFH
14th November 2011, 15:41
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