环境:VS2008+Qt4.7
创建线程流程
1.创建子类继承QThread类
创建子类时不要勾选ui界面
#ifndef MYTHREAD_H
#define MYTHREAD_H
#include <QThread>
#include <QString>
class MyThread : public QThread
{
Q_OBJECT
public:
MyThread();
~MyThread();
void threadclose();//关闭线程函数
protected:
virtual void run();
signals:
void sendMsg(QString);//自定义信号,用于与主界面通信
private:
volatile bool stopped;//编写多线程的程序时,同一个变量可能被多个线程修改,而程序通过该变量同步各个线程
};
#endif // MYTHREAD_H
2.重写run函数
//QThread中的run函数是虚函数,线程中需要运行的所有复杂逻辑都在run函数里面执行
void MyThread::run()
{
while(1)
{
//逻辑代码...
}
}//与槽函数不一样的地方是run()函数中可以有死循环
3.需要创建线程的时候就实例化子类对象
MyThread thread1 = new MyThread();
4.运行线程
调用start()函数就会执行run()函数中的语句
thread1->start();
5.结束进程
run()中语句return
void MyThread::run()
{
while(1)
return;//结束线程
}
或者使用QThread::terminate()[slot]
connect(ui->pushButton,SIGNAL(clicked()),thread1,SLOT(terminate()));
//绑定信号和槽,点击按钮结束线程
线程与主界面的通信
Qt中线程间的通信有两种,一种是使用信号和槽机制通信,另一种是共享内存通信 本例使用的是第一种通信方式
线程类中自定义信号
signals:
void sendMsg(QString);//自定义信号,用于与主界面通信
主窗口类中构造函数添加
QObject::connect(thread1, SIGNAL(sendMsg(QString)), this, SLOT(recv_msg(QString)));
例子(线程不断发送字符串,主界面把线程发送的字符串显示到textEdit中):
void MyThread::run()
{
while(1)
{
if(stopped == true)
{
stopped = false;
return;
}
emit sendMsg("i love Lpx");
sleep(1);
}
}
void MyWidget::recv_msg(QString msg)
{
ui.textEdit->append(msg);
}
参考自 https://blog.csdn.net/hebbely/article/details/79428336?ops_request_misc=&request_id=&biz_id=102&utm_term=qt%E7%BA%BF%E7%A8%8B%E9%97%B4%E9%80%9A%E4%BF%A1%E6%96%B9%E5%BC%8F&utm_medium=distribute.pc_search_result.none-task-blog-2~all~sobaiduweb~default-0-79428336.first_rank_v2_pc_rank_v29&spm=1018.2226.3001.4187
实例源码
/*mythread.h*/
#ifndef MYTHREAD_H
#define MYTHREAD_H
#include <QThread>
#include <QString>
class MyThread : public QThread
{
Q_OBJECT
public:
MyThread();
~MyThread();
void threadclose();
protected:
virtual void run();
signals:
void sendMsg(QString);
private:
volatile bool stopped;
};
/*mythread.cpp*/
#include "mythread.h"
#include <cstdlib>
#include <QDebug>
MyThread::MyThread()
{
stopped = false;
}
MyThread::~MyThread()
{
}
void MyThread::run()
{
while(1)
{
if(stopped == true)
{
stopped = false;
return;
}
emit sendMsg("i love Lpx");
sleep(1);
}
}
void MyThread::threadclose()
{
stopped = true;
}
//主窗口类mywidget.h
#ifndef MYWIDGET_H
#define MYWIDGET_H
#include <QtGui/QWidget>
#include "ui_mywidget.h"
#include "mythread.h"
#include <QString>
class MyWidget : public QWidget
{
Q_OBJECT
public:
MyWidget(QWidget *parent = 0, Qt::WFlags flags = 0);
~MyWidget();
public slots:
void on_pushButton_clicked();
void recv_msg(QString);
void on_pushButton_2_clicked();
private:
Ui::MyWidgetClass ui;
MyThread *thread1;
};
#endif // MYWIDGET_H
//mywidget.cpp
#include "mywidget.h"
MyWidget::MyWidget(QWidget *parent, Qt::WFlags flags)
: QWidget(parent, flags)
{
ui.setupUi(this);
thread1 = new MyThread();
QObject::connect(ui.pushButton, SIGNAL(clicked()), this, SLOT(on_pushButton_clicked()));
QObject::connect(ui.pushButton_2, SIGNAL(clicked()), this, SLOT(on_pushButton_2_clicked()));
QObject::connect(thread1, SIGNAL(sendMsg(QString)), this, SLOT(recv_msg(QString)));
}
MyWidget::~MyWidget()
{
delete thread1;
}
void MyWidget::on_pushButton_clicked()
{
thread1->start();
}
void MyWidget::recv_msg(QString msg)
{
ui.textEdit->append(msg);
}
void MyWidget::on_pushButton_2_clicked()
{
thread1->threadclose();
}