//thread.h
#pragma once
#define QT3_SUPPORT
#include <iostream>
#include <cstdio>
#include <unistd.h>
#include <Qt/qapplication.h>
#include <Qt/qpushbutton.h>
#include <qmessagebox.h>
#include <Qt/qdialog.h>
#include <Qt/qthread.h>
#include <QString>
template<typename T>
class Thread : public QThread
{
public:
Thread();
~Thread();
void run();
void setTObj(T* pobj);
public:
int flag;
unsigned int pos;
T* pTObj;
};
template<typename T>
Thread<T>::Thread()
{
flag = 1;
pos =0;
}
template<typename T>
Thread<T>::~Thread()
{
}
template<typename T>
void Thread<T>::run()
{
//QMessageBox::information( NULL, "Application name","Started.\n");
QString strText;
while(flag)
{
strText.sprintf("flag:%d",pos);
pTObj->setWindowTitle(strText);
pos++;
QThread::msleep(500);
}
}
template<typename T>
void Thread<T>::setTObj(T* pobj)
{
pTObj = pobj;
}
//ProcessDialog.h
#pragma once
#define QT3_SUPPORT
#include <iostream>
#include <cstdio>
#include <unistd.h>
#include <Qt/qapplication.h>
#include <Qt/qpushbutton.h>
#include <qmessagebox.h>
#include <Qt/qdialog.h>
#include <Qt/qthread.h>
#include <QString>
#include "Thread.h"
class ProcessDialog :public QDialog
{
Q_OBJECT
public:
ProcessDialog(QWidget* parent=0)
:btnStop("Stop",this)
,btnWork("Work",this)
{
btnStop.setGeometry( 62, 40, 75, 30 );
btnWork.setGeometry( 162,140, 75, 30 );
threadA.setTObj(this);
connect(&btnWork,SIGNAL( clicked()),this,SLOT(start()) );
connect(&btnStop,SIGNAL( clicked()),this,SLOT(stop()) );
}
public:
QPushButton btnStop;
QPushButton btnWork;
private slots:
void start()
{
btnWork.setText("Working");
threadA.start();
threadA.flag = 1;
}
void stop()
{
btnWork.setText("Work");
threadA.flag = 0;
//threadA.pos = 0;
}
private:
Thread<ProcessDialog> threadA;
};
//main.cpp
#define QT3_SUPPORT
#include <iostream>
#include <cstdio>
#include <unistd.h>
#include <Qt/qapplication.h>
#include <Qt/qpushbutton.h>
#include <qmessagebox.h>
#include <Qt/qdialog.h>
#include <Qt/qthread.h>
#include <QString>
#include "Thread.h"
#include "ProcessDialog.h"
int main( int argc, char **argv )
{
QApplication app( argc, argv );
ProcessDialog dlg;
app.setMainWidget( &dlg );
dlg.resize(640,480);
dlg.setModal(true);
dlg.exec();
return app.exec();
}