QT多线程的实现有两种方法,一种是继承QThread的多线程使用方法,另外一种是使用QObject实现多线的方法。传统的方式是继承QTread,但是这种方式比较的容易出错,QT官方推荐使用的是第二种方式。这里介绍这两种方式的最简单的一种创建方式。

最简方法一:

    (1)创建一个QT应用

    (2)创建线程类,继承QThread 

simplethreadone.h

#ifndef SIMPLETHREADONE_H
#define SIMPLETHREADONE_H
#include <QThread>

class SimpleThreadOne : public QThread
{
public:
    SimpleThreadOne();
};

#endif // SIMPLETHREADONE_H

simplethreadone.cpp

#include "simplethreadone.h"
#include <QDebug>

SimpleThreadOne::SimpleThreadOne()
{

}

(3)重写QThread的run方法

simplethreadone.h

#ifndef SIMPLETHREADONE_H
#define SIMPLETHREADONE_H
#include <QThread>

class SimpleThreadOne : public QThread
{
public:
    SimpleThreadOne();
    void run();
};

#endif // SIMPLETHREADONE_H

 simplethreadone.cpp

#include "simplethreadone.h"
#include <QDebug>

SimpleThreadOne::SimpleThreadOne()
{

}

void SimpleThreadOne::run()
{
    while (true)
    {
        qDebug()<<"SimpleThreadOne run!";
        sleep(2);
    }
}

(4)主线程中创建线程对象,使用start()方法启动线程。

#include "mainwindow.h"
#include <QApplication>
#include "simplethreadone.h"

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    MainWindow w;
    w.show();

    SimpleThreadOne *SThread = new SimpleThreadOne();
    SThread->start();

    return a.exec();
}

最简方法二:

(1)创建一个QT应用

(2)创建线程类,继承QObject

simplethreadtwo.h

#ifndef SIMPLETHREADTWO_H
#define SIMPLETHREADTWO_H

#include <QObject>

class SimpleThreadTwo : public QObject
{
    Q_OBJECT
public:
    explicit SimpleThreadTwo(QObject *parent = nullptr);

public slots:
    void doSomething();
};

#endif // SIMPLETHREADTWO_H

simplethreadtwo.cpp 

#include "simplethreadtwo.h"
#include <QDebug>
#include <unistd.h>


SimpleThreadTwo::SimpleThreadTwo(QObject *parent) : QObject(parent)
{

}

void SimpleThreadTwo::doSomething()
{
    while (true)
    {
        qDebug()<<"SimpleThreadTwo run!";
        sleep(2);
    }
}

doSomething 是一个槽函数,通过信号调用该槽函数启动循环函数。该信号由主线程发送。

(3)主线程创建线程,拉起新建的线程。

    我QT里的主线程是mainwindow。mainwindow.h代码如下:

#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
#include "simplethreadtwo.h"
namespace Ui {
class MainWindow;
}

class MainWindow : public QMainWindow
{
    Q_OBJECT
public:
    explicit MainWindow(QWidget *parent = 0);
    ~MainWindow();
    void SendStart(void);
signals:
    void Signal1();
private:
    Ui::MainWindow *ui;
};

#endif // MAINWINDOW_H

主线程通过SendStart函数发送自定义的信号Signal1,从而触发 SimpleThreadTwo 线程的循环函数。

mainwindow.cpp

#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QThread>
MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);

    /**thread two**/
    QThread* m_objThread;
    SimpleThreadTwo* m_ThreadTwo;
    m_objThread= new QThread();
    m_ThreadTwo = new SimpleThreadTwo();
    m_ThreadTwo->moveToThread(m_objThread);
    connect(m_objThread,&QThread::finished,m_objThread,&QObject::deleteLater);
    connect(this,&MainWindow::Signal1,m_ThreadTwo,&SimpleThreadTwo::doSomething);
    m_objThread->start();
    /**start**/
    SendStart();

}
void MainWindow::SendStart()
{
    emit Signal1();
}
MainWindow::~MainWindow()
{
    delete ui;
}

运行效果如下:

qt多线程与python多线程_多线程

从上图可以看到两种方式创建的线程都正常的跑起来了。


说明:

    上面两种方式都是比较主流的创建线程的方式,但是上面的代码只是简单的创建了一个线程。没有涉及到线程的正常退出和线程安全问题。更详细的线程操作可以参考其他的资料:

  QT开发(三十四)——QT多线程编程