QT 多线程(QThread)里调用线程池(QThreadPool )与主界面进行通讯

在最近的一个项目中,遇到了一个问题,就是主界面调用一个线程,然后再线程中开启一个线程池进行数据生成,线程池调用的线程对象必须继承自QRunable类,这个类有个缺点,就是因为它无法继承QObject,所以不能向外面发送信号,但是我们需要在主界面显示它输出的信息。怎么办呢?

  • 编写一个QRunable子类
  • 编写一个QThread子类
  • 调用QThread子类

编写一个QRunable子类

编写一个QRunable子类MyRunable

MyRunable.h代码,如下:

#ifndef MYRUNNABLE_H
#define MYRUNNABLE_H

#include 
#include 

class MyRunnable : public QRunnable
{
public:
    MyRunnable(QObject *parent = 0);
    ~MyRunnable();

    void run();

    void setID(const int &id);
    //向外传送消息
    void requestMsg(const QString &msg);
private:
    //父对象
    QObject *mParent;

    int runnableID;
};

#endif // MYRUNNABLE_H
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26

MyRunable.cpp代码,如下:

#include "myrunnable.h"
#include 
#include 
MyRunnable::MyRunnable(QObject *parent)
    : QRunnable()
{
    mParent = parent;
}

MyRunnable::~MyRunnable()
{
    runnableID = 0;
}

void MyRunnable::setID(const int &id)
{
    runnableID = id;
}

void MyRunnable::requestMsg(const QString &msg)
{
    QMetaObject::invokeMethod(mParent, "requestMsg", Qt::QueuedConnection, Q_ARG(QString, msg));
}

void MyRunnable::run()
{
    for(int i = 0;i < 10;i++)
    {
        requestMsg(QString("this is a MyRunnable %1").arg(runnableID));
        QThread::sleep(1);
    }
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33

编写一个QThread子类

编写一个QThread子类MyThread

MyThread.h代码,如下:

#ifndef MYTHREAD_H
#define MYTHREAD_H

#include 

class MyThread : public QThread
{
    Q_OBJECT
public:
    explicit MyThread(QObject *parent = 0);

protected:
    void run();

signals:
    void requestMsg(const QString &msg);

public slots:

};

#endif // MYTHREAD_H
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23

MyThread.cpp代码,如下:

#include "mythread.h"
#include 
#include "myrunnable.h"
#include 
MyThread::MyThread(QObject *parent) :
    QThread(parent)
{
}

void MyThread::run()
{
    qDebug() << "MyThread";

    QThreadPool myPool;
    myPool.setMaxThreadCount(4);
    for(int i = 0;i < 4;i++)
    {
        MyRunnable *subThread = new MyRunnable(this);
        subThread->setID(i);
        myPool.start(subThread);
    }

    myPool.waitForDone();
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24

调用QThread子类

在MainWindow中调用MyThread并关联信号槽

MainWindow .h代码,如下:

#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include 
#include "mythread.h"

namespace Ui {
class MainWindow;
}

class MainWindow : public QMainWindow
{
    Q_OBJECT

public:
    explicit MainWindow(QWidget *parent = 0);
    ~MainWindow();
private slots:
    void showMsg(const QString &msg);
private:
    Ui::MainWindow *ui;

    MyThread myThread;
};

#endif // MAINWINDOW_H
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26

MainWindow .cpp代码,如下:

#include "mainwindow.h"
#include "ui_mainwindow.h"
#include 
#include 

#include 
#include 


MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    connect(&myThread,SIGNAL(requestMsg(const QString&)),this,SLOT(showMsg(const QString&)));
    myThread.start();
}

void MainWindow::showMsg(const QString &msg)
{
    qDebug()<< msg;
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21

运行结果

MyThread
"this is a MyRunnable 0"
"this is a MyRunnable 1"
"this is a MyRunnable 2"
"this is a MyRunnable 3"
"this is a MyRunnable 0"
"this is a MyRunnable 1"
"this is a MyRunnable 2"
"this is a MyRunnable 3"
"this is a MyRunnable 0"
"this is a MyRunnable 1"
"this is a MyRunnable 2"
"this is a MyRunnable 3"
"this is a MyRunnable 0"
"this is a MyRunnable 1"
"this is a MyRunnable 2"
"this is a MyRunnable 3"
"this is a MyRunnable 0"
"this is a MyRunnable 1"
"this is a MyRunnable 2"
"this is a MyRunnable 3"
"this is a MyRunnable 0"
"this is a MyRunnable 1"
"this is a MyRunnable 2"
"this is a MyRunnable 3"
"this is a MyRunnable 0"
"this is a MyRunnable 1"
"this is a MyRunnable 2"
"this is a MyRunnable 3"
"this is a MyRunnable 0"
"this is a MyRunnable 1"
"this is a MyRunnable 2"
"this is a MyRunnable 3"
"this is a MyRunnable 0"
"this is a MyRunnable 1"
"this is a MyRunnable 2"
"this is a MyRunnable 3"
"this is a MyRunnable 0"
"this is a MyRunnable 1"
"this is a MyRunnable 2"
"this is a MyRunnable 3"
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41

欢迎大家指正…

你可能感兴趣的:(Qt,C/C++)