QT线程

//线程函数后台运行 ,中不能操作图形类
mythread.h文件
#ifndef MYTHREAD_H
#define MYTHREAD_H

#include <QObject>

class mythread : public QObject
{
    Q_OBJECT      //使用信号和槽必须使用这个宏
public:
    explicit mythread(QObject *parent = 0);

    //线程处理函数
    void mytimeout();

    //标志位while循环
    void setFlag(bool flag =true);
signals:
    void mySignal();

public slots:

private:
    bool isStop;
};

#endif // MYTHREAD_H
mythread.cpp文件
#include "mythread.h"
#include <QThread>
#include <QDebug>

mythread::mythread(QObject *parent) : QObject(parent)
{
    isStop = true;
}

void mythread::setFlag(bool flag)
{
    isStop = flag;
}

void mythread::mytimeout()
{
    //线程函数后台运行 ,中不能操作图形类
    while(isStop)
    {
        QThread::sleep(1);
        if(!isStop)
        {
            break;
        }
        emit mySignal();

        qDebug()<<"子线程号:" <<QThread::currentThread();

    }
}
mainwindow.h文件
#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include <QMainWindow>

#include "mythread.h"

namespace Ui {
class MainWindow;
}

class MainWindow : public QMainWindow
{
    Q_OBJECT

public:
    explicit MainWindow(QWidget *parent = 0);
    ~MainWindow();

    void dealSignal();

signals:
    void startThread();  //启动子线程信号

private:
    Ui::MainWindow *ui;

    mythread *myT;
    QThread *thread;   //指定成员
};

#endif // MAINWINDOW_H
mainwindow.cpp文件
#include "mainwindow.h"
#include "ui_mainwindow.h"

#include <QThread>
#include <QDebug>

#include "mythread.h"

MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    resize(800,600);

    //动态分配空间,不能指定父对象,否则就会报错,Cannot move objects with a parent
    myT = new mythread;

    //创建子线程
    thread = new QThread(this);

    //把自定义线程加入到子线程中
    myT->moveToThread(thread);

    connect(ui->pushButton_Start,&QPushButton::clicked,[=]()
    {
        qDebug()<<"点击了Start按钮";
         myT->setFlag(true);
        //启动线程,但是没有启动线程处理函数
        thread->start();
        //不能直接调用线程处理函数,
        //直接调用,导致,线程处理函数和主线程是在同一个函数

        //只能通过signal -slot方式调用
        emit startThread(); //发送启动信号

    });
    //接收到线程中发出的&mythread::mySignal信号,连接到槽函数
    connect(myT,&mythread::mySignal,this,&MainWindow::dealSignal);

     qDebug()<<"主线程号:" <<QThread::currentThread();

     //连接启动子线程信号与函数
     connect(this, &MainWindow::startThread,myT, &mythread::mytimeout);

     connect(ui->pushButton_Stop,&QPushButton::clicked,[=]()
     {
         qDebug()<<"点击了STOP按钮";

         myT->setFlag(false);
         thread->quit(); //退出函数要等到子线程运行完毕才能退出
         thread->wait();
     });

     //直接关闭窗口时
     connect(this,&MainWindow::destroyed,[=]()
     {
         qDebug()<<"点击了关闭窗口";

         myT->setFlag(false);
         thread->quit(); //退出函数要等到子线程运行完毕才能退出
         thread->wait();
         
         delete myT;
     });


}
//槽函数,处理收到的信号
void MainWindow::dealSignal()
{
    static int i =0;
    i++;
    ui->lcdNumber->display(i);
}

MainWindow::~MainWindow()
{
    delete ui;
}





原文地址:https://www.cnblogs.com/qq376142178/p/15399230.html