0
点赞
收藏
分享

微信扫一扫

QT moveToThread线程理解

小月亮06 2022-02-22 阅读 351

一、moveToThread创建开启线程步骤:

(1)创建继承自QObject类,实现槽函数。

(2)将QObject类通过moveToThread方法移到QThread线程中,使QObject类依附于线程。

(3)连接信号槽,槽必须是QObject类中函数。

(4)QThread调用start开启线程。

二、程序代码:

#ifndef MYOBJECT_H
#define MYOBJECT_H
 
#include <QObject>
#include <QTimer>
class MyObject : public QObject
{
    Q_OBJECT
public:
    explicit MyObject(QObject *parent = nullptr);
    ~MyObject();
 
    QTimer *timer;
 
public slots:
    void slotStart();
 
 
signals:
 
};
 
#endif // MYOBJECT_H
#include "myobject.h"
#include <QDebug>
#include <QThread>
#include <QDateTime>
MyObject::MyObject(QObject *parent) : QObject(parent)
{
 
}
 
MyObject::~MyObject()
{
    qDebug()<<"执行了MyObject的析构";
}
 
void MyObject::slotStart()
{
    qDebug()<<"子线程 id"<<QThread::currentThreadId()<<QDateTime::currentDateTime();
    timer = new QTimer(this);
    connect(timer,&QTimer::timeout,this,[=](){
        for(int i = 0;i<5;i++)
        {
            QThread::sleep(1);
        }
        qDebug()<<"计时器执行了"<<QDateTime::currentDateTime();
    });
    timer->start(2000);   
}
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
 
#include <QMainWindow>
#include "myobject.h"
 
QT_BEGIN_NAMESPACE
namespace Ui { class MainWindow; }
QT_END_NAMESPACE
 
class MainWindow : public QMainWindow
{
    Q_OBJECT
 
public:
    MainWindow(QWidget *parent = nullptr);
    ~MainWindow();
 
    QThread *thread;
    MyObject *object;
 
private slots:
    void on_pushButton_clicked();
 
    void on_pushButton_2_clicked();
 
signals:
    void signalStart();
 
private:
    Ui::MainWindow *ui;
};
#endif // MAINWINDOW_H
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QDebug>
#include <QThread>
#include <QDateTime>
 
MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
    , ui(new Ui::MainWindow)
{
    ui->setupUi(this);
}
 
MainWindow::~MainWindow()
{
    delete ui;
}
 
void MainWindow::on_pushButton_clicked()
{
    qDebug()<<"点击了开始,主线程id"<<QThread::currentThreadId();
    thread = new QThread;
    object = new MyObject;
    object->moveToThread(thread);
    connect(this,&MainWindow::signalStart,object,&MyObject::slotStart);
    connect(thread,&QThread::finished,object,&QObject::deleteLater);
    thread->start();
 
    emit signalStart();
}
 
void MainWindow::on_pushButton_2_clicked()
{
    qDebug()<<"点击了停止,线程停止了"<<QDateTime::currentDateTime();
    thread->quit();
    thread->wait();
    qDebug()<<"释放了thread";
    delete thread;
    thread = NULL;
}

ui界面

 输出结果

 三、补充:

1. 如果线程已经start运行,重复调用start不会进行任何处理。
2. 槽函数只在一个线程中,如果当前槽函数未执行完,下一次槽函数又要进来,会阻塞槽函数的执行。
3. 停止线程时,如果线程中槽函数未执行完,不会立刻停止线程,会等待槽函数执行完成之后停止线程。
4. 关闭线程一般先quit,然后在wait,原因是在执行quit()后,调用wait()来等待QThread子线程的结束,这样就能保证在清除QThread时,其子线程是停止运行的。
5. 使用线程的finished信号,QObject的deleteLater槽函数,会在线程quit后,发送finished信号,释放QObject类。
以上均为自己的理解,如果有不正确的地方,欢迎指出。

举报

相关推荐

0 条评论