MainThread::MainThread(){ run=true; tmr = new QTime;}void MainThread::process(){ double PVSim; tmr->start(); while (run) { PVSim = 2+sin(tmr->elapsed()); emit SendData(PVSim); Sleep(100); } emit finished();}void MainThread::stop(){ run=false;}
ControlThread::ControlThread(){}void ControlThread::process(){ percent = PV * 100 / 2; emit AutoFirst(percent,1,1);}void ControlThread::stop(){ emit finished();}void ControlThread::GetPV(double data){ PV = data; process();}
threadControl = new QThread; threadMain = new QThread; Main = new MainThread; Control = new ControlThread(1,16,12); Main->moveToThread(threadMain); Control->moveToThread(threadControl); connect(threadControl, SIGNAL(started()), Control, SLOT(process())); connect(Control, SIGNAL(finished()), threadControl, SLOT(quit())); connect(Control, SIGNAL(finished()), Control, SLOT(deleteLater())); connect(threadControl, SIGNAL(finished()), threadControl, SLOT(deleteLater())); connect(this,SIGNAL(SetSP(double)),Control,SLOT(GetSP(double))); connect(Control,SIGNAL(AutoFirst(int,int,int)),this,SLOT(Auto(int,int,int))); connect(threadMain, SIGNAL(started()), Main, SLOT(process())); connect(Main, SIGNAL(finished()), threadMain, SLOT(quit())); connect(Main, SIGNAL(finished()), Main, SLOT(deleteLater())); connect(threadMain, SIGNAL(finished()), threadMain, SLOT(deleteLater())); connect(Main,SIGNAL(SendData(double)),Control,SLOT(GetPV(double)));
MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent){ setupUi(this); threadControl = new QThread; threadMain = new QThread; Main = new MainThread; Control = new ControlThread(1,16,12); Main->moveToThread(threadMain); Control->moveToThread(threadControl); connect(threadControl, SIGNAL(started()), Control, SLOT(process())); connect(Control, SIGNAL(finished()), threadControl, SLOT(quit())); connect(Control, SIGNAL(finished()), Control, SLOT(deleteLater())); connect(threadControl, SIGNAL(finished()), threadControl, SLOT(deleteLater())); connect(this,SIGNAL(SetSP(double)),Control,SLOT(GetSP(double))); connect(Control,SIGNAL(AutoFirst(int,int,int)),this,SLOT(Auto(int,int,int))); connect(threadMain, SIGNAL(started()), Main, SLOT(process())); connect(Main, SIGNAL(finished()), threadMain, SLOT(quit())); connect(Main, SIGNAL(finished()), Main, SLOT(deleteLater())); connect(threadMain, SIGNAL(finished()), threadMain, SLOT(deleteLater())); connect(Main,SIGNAL(SendData(double,int)),Control,SLOT(GetPV(double,int))); threadControl->start(); threadMain->start(); tmr = new QTimer; connect(tmr,SIGNAL(timeout()),Control,SLOT(process())); connect(Control,SIGNAL(delay(int)),this,SLOT(setTimer(int)));}void MainWindow::setTimer(int tm){ tmr->start(tm);}
void ControlThread::process(){ percent = SensPV * 100 / 2; emit delay(miliseconds);}void ControlThread::GetPV(double data){ SensPV = data;}