QT 创建线程的三种方法

方式一:派生于QThread

      派生于QThread,这是Qt创建线程中最常用的方法,重写void QThread::run(),在run写具体的内容,外部通过start调用,即可执行线程体run();

注意:

   派生于QThread的类,构造函数属于主线程,run函数属于子线程,可以通过打印线程id判断。

方式二:派生于QRunable

   派生于QRunable,重写run()方法里处理其他任务,调用时需要借助线程池。

mythread* pth = new Mythread();
QThreadPool::globalinstance()->start(pth);

   注意:

     这种创建线程的方法的最大缺点就是:不能使用QT的信号-槽机制,因为QRunnable不会继承QObject。但是这种方法的好处就是,可以让QThreadPool来管理线程,QThreadPool会自动的清理我们新建的QRunnable对象

方式三:moveToThread

派生于QObject,使用moveToThread方法,将QThread对象作为私有成员,在构造函数里moveToThread,然后启动线程。

this->moveToThread(&m_th);

m_th.start();

方式一的实现:

     thread01.h

#ifndef THREAD01_H
#define THREAD01_H

#include <QThread>

class Thread01 : public QThread
{
     Q_OBJECT;
public:
    Thread01();

    void run() override;
};

#endif // THREAD01_H

    thread01.cpp

#include "thread01.h"
#include <QDebug>
#include <iostream>

using namespace std;
Thread01::Thread01()
{
     cout<< "Thread01 construct fun" << QThread::currentThread() << endl;
}

void Thread01::run()
{
      cout<< "Thread01::run" << QThread::currentThread() << endl;
      int index  = 0;
      while(1)
      {
          qDebug() << index++;
          QThread::msleep(500);
      }

}

   main.cpp

#include <QCoreApplication>
#include "thread01.h"
#include <iostream>

using namespace std;


int main(int argc, char *argv[])
{
    QCoreApplication a(argc, argv);

    cout<< "main thread" << QThread::currentThread() << endl;

    Thread01 th;
    th.start();

    cout<< "main thread end" << endl;

    return a.exec();
}

运行:

 方式二的实现:

      thread02.h

#ifndef THREAD02_H
#define THREAD02_H

#include <QRunnable>

class Thread02 : public QRunnable
{
public:
    Thread02();
    ~Thread02();
    void run() override;
};

#endif // THREAD02_H

   thread02.cpp

#include "thread02.h"
#include <QThread>

#include <iostream>

using namespace std;
Thread02::Thread02()
{
    cout<< "Thread01 construct fun" << QThread::currentThread() << endl;
}

Thread02::~Thread02()
{
      cout<< "Thread01 destructor fun" << QThread::currentThread() << endl;
}

void Thread02::run()
{
     cout<< "Thread02 construct fun" << QThread::currentThread() << endl;
}

main.cpp

#include <QCoreApplication>
#include "thread01.h"
#include "thread02.h"
#include <iostream>
#include <QThreadPool>
using namespace std;


int main(int argc, char *argv[])
{
    QCoreApplication a(argc, argv);

    cout<< "main thread" << QThread::currentThread() << endl;

   /* Thread01 th;
    th.start();*/

   Thread02 *th = new Thread02();
   QThreadPool::globalInstance()->start(th);

    cout<< "main thread end" << endl;

    return a.exec();
}

运行:

方式三的实现  

   thread03.h

#ifndef THREAD03_H
#define THREAD03_H
#include <QObject>
#include <QThread>
class thread03 : public QObject
{
    Q_OBJECT
public:
    explicit thread03(QObject *parent = nullptr);
public slots:
    void fun();
signals:

private:
     QThread m_th;

};

#endif // THREAD03_H

thread03.cpp

#include "thread03.h"
#include <QDebug>
#include <iostream>

using namespace std;
thread03::thread03(QObject *parent)
    : QObject{parent}
{

    this->moveToThread(&m_th);
    m_th.start();
    cout<< "Thread03 construct fun" << QThread::currentThread() << endl;


}

void thread03::fun()
{
   cout<< "Thread03  fun" << QThread::currentThread() << endl;
   qDebug() << "func";
   int index = 0;
   while(1)
   {
       qDebug() << index++;
       QThread::msleep(300);
   }
}

widget.h

#ifndef WIDGET_H
#define WIDGET_H

#include <QWidget>
#include "thread03.h"

QT_BEGIN_NAMESPACE
namespace Ui { class Widget; }
QT_END_NAMESPACE

class Widget : public QWidget
{
    Q_OBJECT

public:
    Widget(QWidget *parent = nullptr);
    ~Widget();

private slots:
    void on_pushButton_clicked();

signals:
    void sig_fun();

private:
    Ui::Widget *ui;
    thread03* m_pTh03 = nullptr;
};
#endif // WIDGET_H

widget.cpp

#include "widget.h"
#include "ui_widget.h"
#include <iostream>

using namespace std;

Widget::Widget(QWidget *parent)
    : QWidget(parent)
    , ui(new Ui::Widget)
{
    ui->setupUi(this);
    m_pTh03 = new thread03();
    connect(this,&Widget::sig_fun,m_pTh03,&thread03::fun);
}

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


void Widget::on_pushButton_clicked()
{
     cout<< "on_pushButton_clicked" << QThread::currentThread() << endl;
     emit sig_fun();
}

界面和运行结果:

 

 

  • 9
    点赞
  • 72
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

水火汪

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值