线程在使用时,需要判断线程的状态。isRunning()。
thread->quit();//退出线程,会等待线程执行完毕。
thread->wait();//回收资源
子线程是while(1)一般使用设置标志位来实现线程的退出。
myThread->setFlag(true);
代码如下:
1.pro文件
#-------------------------------------------------
#
# Project created by QtCreator 2019-10-29T14:52:07
#
#-------------------------------------------------
QT += core gui
greaterThan(QT_MAJOR_VERSION, 4): QT += widgets
TARGET = ThreadUI2
TEMPLATE = app
# The following define makes your compiler emit warnings if you use
# any feature of Qt which has been marked as deprecated (the exact warnings
# depend on your compiler). Please consult the documentation of the
# deprecated API in order to know how to port your code away from it.
DEFINES += QT_DEPRECATED_WARNINGS
# You can also make your code fail to compile if you use deprecated APIs.
# In order to do so, uncomment the following line.
# You can also select to disable deprecated APIs only up to a certain version of Qt.
#DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000 # disables all the APIs deprecated before Qt 6.0.0
CONFIG += c++11
SOURCES += \
main.cpp \
widget.cpp \
mythread.cpp
HEADERS += \
widget.h \
mythread.h
FORMS += \
widget.ui
# Default rules for deployment.
qnx: target.path = /tmp/$${TARGET}/bin
else: unix:!android: target.path = /opt/$${TARGET}/bin
!isEmpty(target.path): INSTALLS += target
2.mythread.h文件
#ifndef MYTHREAD_H
#define MYTHREAD_H
#include
class MyThread : public QObject
{
Q_OBJECT
public:
explicit MyThread(QObject *parent = nullptr);
~MyThread();
//线程处理函数
void MyTimeout();
void setFlag(bool flag = true);
signals:
void mySignal();//定义一个信号,在该子线程中每秒发射一次该信号
private:
bool isStop;
public slots:
};
#endif // MYTHREAD_H
3.mythread.h文件
#include "mythread.h"
#include
#include
MyThread::MyThread(QObject *parent) : QObject(parent)
{
isStop=false;
}
MyThread::~MyThread()
{
}
void MyThread::MyTimeout()
{
while(isStop==false)
{
QThread::sleep(1);
emit mySignal();
qDebug()<<"sub thread :"<
4.widget.h文件
#ifndef WIDGET_H
#define WIDGET_H
#include
#include"mythread.h"
#include
namespace Ui {
class Widget;
}
class Widget : public QWidget
{
Q_OBJECT
public:
explicit Widget(QWidget *parent = nullptr);
~Widget();
signals:
void startThread();//启动子线程的信号
private slots:
void on_pushButtonStart_clicked();
void on_pushButtonStop_clicked();
void dealSignal();
void dealClose();
private:
Ui::Widget *ui;
MyThread *myT;
QThread *thread;
};
#endif // WIDGET_H
5.widget.cpp文件
#include "widget.h"
#include "ui_widget.h"
#include
Widget::Widget(QWidget *parent) :
QWidget(parent),
ui(new Ui::Widget)
{
ui->setupUi(this);
myT=new MyThread;//不能指定父对象
thread=new QThread(this);//创建子线程
myT->moveToThread(thread);//把自定义的线程加入到子线程中
connect(myT,&MyThread::mySignal,this,&Widget::dealSignal);
qDebug()<<"main thread: "<lcdNumber->display(i);
}
void Widget::dealClose()
{
on_pushButtonStop_clicked();//停止线程
delete myT; //释放线程空间
}
//start按钮
void Widget::on_pushButtonStart_clicked()
{
if(thread->isRunning()==true)
{
return;
}
//启动线程 但是满意启动线程处理函数
thread->start();
myT->setFlag(false);
//不能直接调用线程处理函数,直接调用导致线程处理函数和主线程在同一个线程
// myT->myTimeout();/
//只能通过signal - slot方式
emit startThread();
}
//stop按钮
void Widget::on_pushButtonStop_clicked()
{
if(thread->isRunning()==false)
{
return;
}
myT->setFlag(true);
thread->quit();
thread->wait();
}
6.widget.ui文件
Widget
0
0
418
276
Widget
-
-
-
Start
-
Stop
7.UI界面