Qt多线程实现思路二
阅读原文时间:2023年07月15日阅读:3
  1. 建立一个继承于Qobject的类myThread
  2. 在类myThread中定义线程处理函数不必是思路一里的run();
  3. 在窗口类中开辟一个自定义线程myThread的指针对象myT = new myThread;
  4. thread = new QThread(this); //创建QThread类子线程
  5. myT->moveToThread(thread); 把自定义线程加入到子线程中
  6. 自定义线程中的线程任务就可以按照线程执行
  7. 这样建立的线程只能通过信号和槽的方式来调用
  8. 一般将任务写在while()里面,在线程退出时,会等待线程任务结束再退出
  9. 线程处理函数内部,不允许操作图形界面

  • mythread.h文件

    #ifndef MYTHREAD_H
    #define MYTHREAD_H
    #include

    class myThread : public QObject
    {
    Q_OBJECT
    public:
    explicit myThread(QObject *parent = );
    //线程处理函数
    void myTimeout();

    void setFlag(bool flag = true);

    signals:
    void mySignal();

    public slots:

    private:
    bool isStop;
    };

    #endif // MYTHREAD_H

  • mythread.cpp文件

    #include "mythread.h"
    #include
    #include

    myThread::myThread(QObject *parent) : QObject(parent)
    {
    isStop = false;
    }

    void myThread::myTimeout()
    {
    while( !isStop )
    {

     QThread::sleep();  
     emit mySignal();
    
      qDebug() << "子线程号:" << QThread::currentThread();
    
      if(isStop)  
      {  
          break;  
      }  

    }
    }

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

  • widget.h文件

    #ifndef WIDGET_H
    #define WIDGET_H
    #include "mythread.h"
    #include
    #include
    #include
    #include
    namespace Ui {
    class Widget;
    }

    class Widget : public QWidget
    {
    Q_OBJECT

    public:
    explicit Widget(QWidget *parent = );
    ~Widget();
    void dealSignal();
    void dealClose();

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

    private slots:

    void on_start_clicked();

    void on_stop_clicked();

    private:
    Ui::Widget *ui;
    myThread *myT;
    QThread *thread;
    };

    #endif // WIDGET_H

  • widget.cpp文件

    #include "widget.h"
    #include "ui_widget.h"
    #include "mythread.h"
    Widget::Widget(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::Widget)
    {
    ui->setupUi(this);
    myT = new myThread;//不能初始化指定父类myThread(this)

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

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

    connect(myT, &myThread::mySignal, this, &Widget::dealSignal);

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

    connect(this, &Widget::startThread, myT, &myThread::myTimeout);

    //窗口关闭时调用线程关闭,前提是线程要运行完当前任务
    connect(this, &Widget::destroyed, this, &Widget::dealClose);

    }

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

    void Widget::dealClose()
    {
    on_stop_clicked();
    delete myT;
    }

    void Widget::dealSignal()
    {
    static int i = ;
    i++;
    ui->lcdNumber->display(i);
    }

    void Widget::on_start_clicked()
    {
    if(thread->isRunning() == true)
    {
    return;
    }

    //启动线程,但是没有启动线程处理函数
    thread->start();
    myT->setFlag(false);

    //不能直接调用线程处理函数,
    //直接调用,导致,线程处理函数和主线程是在同一个线程
    //只能通过 signal - slot 方式调用
    emit startThread();
    }

    void Widget::on_stop_clicked()
    {
    if(thread->isRunning() == false)
    {
    return;
    }

    myT->setFlag(true);
    thread->quit();
    thread->wait();
    }

手机扫一扫

移动阅读更方便

阿里云服务器
腾讯云服务器
七牛云服务器

你可能感兴趣的文章