#ifndef THREAD_H#define THREAD_H#include <QWidget>#include "ui_thread.h"#include <QThread>#include <QProgressDialog>class for_a:public QThread{public: void run() { setObjectName("for_a"); int b=0; for(int i=0; i<1000000000;i++) {b++;} }};class for_b:public QThread{ public: void run() { setObjectName("for_b"); int b=0; for(int i=0; i<1000000000;i++) {b++;} } };class for_c:public QThread{ public: void run() { setObjectName("for_c"); int b=0; for(int i=0; i<1000000000;i++) {b++;} }};using namespace Ui;class thread : public QWidget, public Form{ Q_OBJECTpublic: for_a th_a; for_b th_b; for_c th_c; QThread *id; thread(QWidget *parent = 0); ~thread();public slots: void slot_a(); void slot_b(); void slot_c();};#endif // THREAD_H#include "thread.h"thread::thread(QWidget *parent) : QWidget(parent){ setupUi(this); connect(p_b,SIGNAL(clicked()),SLOT(slot_b())); connect(p_a,SIGNAL(clicked()),SLOT(slot_a())); connect(p_c,SIGNAL(clicked()),SLOT(slot_c())); id=NULL;}thread::~thread(){}void thread::slot_a(){ if(id==NULL) { id=&th_a; th_a.start(); setWindowTitle("ok_a id=NULL"); } else if(id->isFinished()) { id=&th_a; th_a.start(); setWindowTitle("ok_a"); } else setWindowTitle(id->objectName()+" is working");}void thread::slot_b(){ if(id==NULL) { id=&th_b; th_b.start(); setWindowTitle("ok_b id=NULL"); } else if(id->isFinished()) { id=&th_b; th_b.start(); setWindowTitle("ok_b"); } else setWindowTitle(id->objectName()+" is working");}void thread::slot_c(){ if(id==NULL) { id=&th_c; th_c.start(); setWindowTitle("ok_c id=NULL"); } else if(id->isFinished()) { id=&th_c; th_c.start(); setWindowTitle("ok_c"); } else setWindowTitle(id->objectName()+" is working");}#include <QtGui/QApplication>#include "thread.h"int main(int argc, char *argv[]){ QApplication a(argc, argv); thread w; w.show(); return a.exec();}
if(id==NULL || id->isFinished()) { id=&th_a; th_a.start(); setWindowTitle("ok_a"); } else setWindowTitle(id->objectName()+" is working");
if(id==NULL) { id=&th_a; th_a.start(); setWindowTitle("ok_a id=NULL"); } else if(id->isFinished()) { id=&th_a; th_a.start(); setWindowTitle("ok_a"); } else setWindowTitle(id->objectName()+" is working");