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
{
Q_OBJECT
public:
explicit MegaStoreMain
(QWidget *parent
= 0);
~MegaStoreMain();
ControlProgram controlProgram;
private:
Ui::MegaStoreMain *ui;
};
MegaStoreMain
::MegaStoreMain(QWidget *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);
}
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);
}
To copy to clipboard, switch view to plain text mode
header
#include <QMutex>
#include <QSize>
#include <QThread>
#include <QTime>
#include <QWaitCondition>
class ControlProgram
: public QThread {
Q_OBJECT
public:
ControlProgram
(QObject *parent
= 0);
~ControlProgram();
int Hour;
int Minute;
int Second;
signals:
void RunTimeUpdate(char* TimeString);
protected:
void run();
private:
bool restart;
bool abort;
};
#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;
};
To copy to clipboard, switch view to plain text mode
source
#include <QtGui>
#include <math.h>
#include "controlProgram.h"
ControlProgram
::ControlProgram(QObject *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);
}
}
#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);
}
}
To copy to clipboard, switch view to plain text mode
Bookmarks