QT学习之 如何启动一个线程

先给出相关代码,然后加以分析

//*************dialog.h**************//

 

#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

//***********end end end************//

 

//***********mainwindow.h************//

 

#ifndef WORKTHREAD_H
#define WORKTHREAD_H
#include <QThread>
class WorkThread : public QThread
{
 protected:
     void run();   //重新实现run()函数
};
 
#endif

//***********end end end************//

 

 

//***********dialog.cpp************//

 

#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);
}

 

//***********end end end************//

 

//***********mainwindow.cpp************//

 

 
#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);  //打印输出
     }
 }
 

//***********end end end************//

 

//***********main.cpp************//

 

#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();
}

//***********end end end************//

以上代码简单,没有必要做过多的讲解,但是其中的“threadVector[i]->terminate(); ”有必要讲解下,terminate()函数的调用便不会立刻终止线程,因为线程的何时终止取决于系统的调度策略,所在在之后又调用了wait()函数是的线程阻塞等待直到退出或者超时。

最后加以一点就是在.pro文件中加入一行代码才能成功运行:

CONFIG+=thread

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值