先来给出每个文件的相关代码然后再加以分析
-
-
-
- #ifndef DIALOG_H
- #define DIALOG_H
- #include <QDialog>
- #define MAXSIZE 5 //最大的线程数
- class QDialogButtonBox;
- class QProgressBar;
- class QPushButton;
- class WorkThread;
- class ThreadDlg : public QDialog
- {
- Q_OBJECT
- public:
- ThreadDlg(QWidget *parent = 0);
- public slots:
- void start();
- void stop();
- private:
- QPushButton *startButton;
- QPushButton *quitButton;
- QPushButton *stopButton;
- QDialogButtonBox *buttonBox;
- WorkThread* threadVector[MAXSIZE];
- };
- #endif
-
-
-
-
- #ifndef WORKTHREAD_H
- #define WORKTHREAD_H
- #include <QThread>
- class WorkThread : public QThread
- {
- protected:
- void run();
- };
-
- #endif
-
-
-
-
- #include <QtGui>
- #include "workThread.h"
- #include "dialog.h"
-
- ThreadDlg::ThreadDlg(QWidget *parent)
- : QDialog(parent)
- {
- startButton = new QPushButton(tr("开始"));
- quitButton = new QPushButton(tr("退出"));
- stopButton = new QPushButton(tr("停止"));
- stopButton->setEnabled(false);
- buttonBox = new QDialogButtonBox;
- buttonBox->addButton(startButton, QDialogButtonBox::ActionRole);
- buttonBox->addButton(stopButton, QDialogButtonBox::ActionRole);
- buttonBox->addButton(quitButton, QDialogButtonBox::RejectRole);
- connect(startButton, SIGNAL(clicked()), this, SLOT(start()));
- connect(quitButton, SIGNAL(clicked()), this, SLOT(close()));
- connect(stopButton, SIGNAL(clicked()), this, SLOT(stop()));
- QVBoxLayout *mainLayout = new QVBoxLayout;
- mainLayout->addWidget(buttonBox);
- setLayout(mainLayout);
- setWindowTitle(tr("启动线程"));
- }
- void ThreadDlg::start()
- {
- for(int i=0;i<MAXSIZE;i++)
- {
- threadVector[i] = new WorkThread();
- }
-
- for(int i=0;i<MAXSIZE;i++)
- {
- threadVector[i]->start(QThread::LowestPriority);
- }
- stopButton->setEnabled(true);
- startButton->setEnabled(false);
- }
- void ThreadDlg::stop()
- {
- for(int i=0;i<MAXSIZE;i++)
- {
- threadVector[i]->terminate();
- threadVector[i]->wait();
- }
- startButton->setEnabled(true);
- stopButton->setEnabled(false);
- }
-
-
-
-
-
- #include "workThread.h"
- #include "dialog.h"
- #include <QTextEdit>
- #include <QDebug>
- #include <stdio.h>
- void WorkThread::run()
- {
- while(true)
- for (int n = 0; n < 10;++n) {
- printf("%d\n",n);
- }
- }
-
-
-
-
-
-
- #include <QApplication>
- #include <QtCore>
- #include "dialog.h"
- int main(int argc, char *argv[])
- {
- QApplication app(argc, argv);
- QTextCodec::setCodecForTr( QTextCodec::codecForName("gb2312"));
- ThreadDlg dialog;
- dialog.show();
- return dialog.exec();
- }
-
以上代码简单,没有必要做过多的讲解,但是其中的“threadVector[i]->terminate();
”有必要讲解下,terminate()函数的调用便不会立刻终止线程,因为线程的何时终止取决于系统的调度策略,所在在之后又调用了wait()函数是线程阻塞等待直到退出或者超时。
最后加以一点就是在.pro文件中加入一行代码才能成功运行:
CONFIG+=thread
Qt学习之如何启动和终止一个线程,布布扣,bubuko.com
Qt学习之如何启动和终止一个线程
原文:http://www.cnblogs.com/lvdongjie/p/3758139.html