QT 多線程(QThread)裡調用線程池(QThreadPool )與主界面進行通訊
在最近的一個項目中,遇到了一個問題,就是主界面調用一個線程,然後再線程中開啟一個線程池進行資料生成,線程池調用的線程對象必須繼承自QRunable類,這個類有個缺點,就是因為它無法繼承QObject,是以不能向外面發送信号,但是我們需要在主界面顯示它輸出的資訊。怎麼辦呢?
- 編寫一個QRunable子類
- 編寫一個QThread子類
- 調用QThread子類
編寫一個QRunable子類
編寫一個QRunable子類MyRunable
MyRunable.h代碼,如下:
#ifndef MYRUNNABLE_H
#define MYRUNNABLE_H
#include <QRunnable>
#include <QMetaObject>
class MyRunnable : public QRunnable
{
public:
MyRunnable(QObject *parent = );
~MyRunnable();
void run();
void setID(const int &id);
//向外傳送消息
void requestMsg(const QString &msg);
private:
//父對象
QObject *mParent;
int runnableID;
};
#endif // MYRUNNABLE_H
MyRunable.cpp代碼,如下:
#include "myrunnable.h"
#include <QDebug>
#include <QThread>
MyRunnable::MyRunnable(QObject *parent)
: QRunnable()
{
mParent = parent;
}
MyRunnable::~MyRunnable()
{
runnableID = ;
}
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 = ;i < ;i++)
{
requestMsg(QString("this is a MyRunnable %1").arg(runnableID));
QThread::sleep();
}
}
編寫一個QThread子類
編寫一個QThread子類MyThread
MyThread.h代碼,如下:
#ifndef MYTHREAD_H
#define MYTHREAD_H
#include <QThread>
class MyThread : public QThread
{
Q_OBJECT
public:
explicit MyThread(QObject *parent = );
protected:
void run();
signals:
void requestMsg(const QString &msg);
public slots:
};
#endif // MYTHREAD_H
MyThread.cpp代碼,如下:
#include "mythread.h"
#include <QThreadPool>
#include "myrunnable.h"
#include <QDebug>
MyThread::MyThread(QObject *parent) :
QThread(parent)
{
}
void MyThread::run()
{
qDebug() << "MyThread";
QThreadPool myPool;
myPool.setMaxThreadCount();
for(int i = ;i < ;i++)
{
MyRunnable *subThread = new MyRunnable(this);
subThread->setID(i);
myPool.start(subThread);
}
myPool.waitForDone();
}
調用QThread子類
在MainWindow中調用MyThread并關聯信号槽
MainWindow .h代碼,如下:
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
#include "mythread.h"
namespace Ui {
class MainWindow;
}
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
explicit MainWindow(QWidget *parent = );
~MainWindow();
private slots:
void showMsg(const QString &msg);
private:
Ui::MainWindow *ui;
MyThread myThread;
};
#endif // MAINWINDOW_H
MainWindow .cpp代碼,如下:
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QDebug>
#include <QDateTime>
#include <QThreadPool>
#include <myrunnable.h>
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;
}
運作結果
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"
歡迎大家指正…