问题描述:
简单的一个定时器,有开始和停止的功能,但是不断的在执行一个复杂的数据(需要两秒)
这个是线程处理函数
void myThread::myTimerout()
{
while(1)
{
if(isStop)
{
break;
}
QThread::sleep(2);
emit mySingal();
qDebug()<<"子线程号"<<QThread::currentThread();
}
}
每次两秒后就会触发mySingal信号
connect(mt,&myThread::mySingal,this,&myWidget::dealSingnal);
出发后就会调用槽函数:
void myWidget::dealSingnal()
{
static int i=0;
i++;
ui->lcdNumber->display(i);
}
Start按钮的槽函数:
void myWidget::on_startbutton_clicked()
{
if(thread->isRunning()==true)//判断线程是不是在运行中
{
return;
}
mt->setisStop(false);
//启动线程,但是没有启动线程处理函数
thread->start();
//只能通过信号槽
emit startThread();
}
要打开定时器,必须利用信号和槽 来启动线程:
connect(this,&myWidget::startThread,mt,&myThread::myTimerout);
点击按钮----->触发信号------>调用线程处理函数
停止的槽函数:
void myWidget::on_stopbutton_clicked()
{
if(thread->isRunning()==false)
{
return;
}
mt->setisStop(true);
thread->quit();
thread->wait();
}
因为线程处理函数是一个死循环,我们必须通过一个isStop来让他跳出循环
总代码:
myThread.h
#ifndef MYTHREAD_H
#define MYTHREAD_H
#include <QObject>
class myThread : public QObject
{
Q_OBJECT
public:
explicit myThread(QObject *parent = nullptr);
void myTimerout();
void setisStop(bool flag);
signals:
void mySingal();
public slots:
private:
bool isStop;
};
#endif // MYTHREAD_H
myThread.cpp
#include "mythread.h"
#include<QThread>
#include<QDebug>
myThread::myThread(QObject *parent) : QObject(parent)
{
isStop=false;
}
void myThread::myTimerout()
{
while(1)
{
if(isStop)
{
break;
}
QThread::sleep(2);
emit mySingal();
qDebug()<<"子线程号"<<QThread::currentThread();
}
}
void myThread::setisStop(bool flag)
{
isStop=flag;
}
mywidget.h
#ifndef MYWIDGET_H
#define MYWIDGET_H
#include <QWidget>
#include"mythread.h"
#include<QThread>
namespace Ui {
class myWidget;
}
class myWidget : public QWidget
{
Q_OBJECT
public:
explicit myWidget(QWidget *parent = 0);
void dealSingnal();
void dealClose();
~myWidget();
signals:
void startThread();
private slots:
void on_startbutton_clicked();
void on_stopbutton_clicked();
private:
Ui::myWidget *ui;
myThread *mt;
QThread *thread;
};
#endif // MYWIDGET_H
mywidget.cpp
#include "mywidget.h"
#include "ui_mywidget.h"
#include<QThread>
#include<QDebug>
myWidget::myWidget(QWidget *parent) :
QWidget(parent),
ui(new Ui::myWidget)
{
ui->setupUi(this);
//动态分配,不能指定父对象
mt=new myThread;
//创建子线程
thread=new QThread(this);
//把自定义的线程加入到子线程中
mt->moveToThread(thread);
connect(mt,&myThread::mySingal,this,&myWidget::dealSingnal);
qDebug()<<"主线程号:"<<QThread::currentThread();
connect(this,&myWidget::startThread,mt,&myThread::myTimerout);
connect(this,&myWidget::destroyed,this,&myWidget::dealClose);
//线程处理函数里面不能处理图形界面
}
void myWidget::dealClose()
{
on_stopbutton_clicked();
delete mt;
}
void myWidget::dealSingnal()
{
static int i=0;
i++;
ui->lcdNumber->display(i);
}
myWidget::~myWidget()
{
delete ui;
}
void myWidget::on_startbutton_clicked()
{
if(thread->isRunning()==true)
{
return;
}
mt->setisStop(false);
//启动线程,但是没有启动线程处理函数
thread->start();
//只能通过信号槽
emit startThread();
}
void myWidget::on_stopbutton_clicked()
{
if(thread->isRunning()==false)
{
return;
}
mt->setisStop(true);
thread->quit();
thread->wait();
}
重 点:
1.myThread *mt; 和 QThread *thread; 我们必须:
//动态分配,不能指定父对象
mt=new myThread;
//创建子线程
thread=new QThread(this);
//把自定义的线程加入到子线程中
mt->moveToThread(thread);
这样来添加线程
2.一般的推出线程必须:
thread->quit();
thread->wait();
3.线程处理函数不能处理UI界面,比如必能弹出一个提示框
4.启动线程用start,但是要调用线程处理函数必须利用信号和槽