2016-09-08 108 views
0

我想連接兩個線程。一個線程是我的應用程序的主線程,另一線程是一個工作線程。我將我的代碼基於以下示例doc.qt.io/qt-5/。對我來說它並不完全工作。我發送一個QString到我的WorkerThread(這工作),並希望將它發回MainThread(不起作用)。在問我爲什麼要這樣做之前,這只是一個非常簡單的例子。真正的代碼要複雜得多,但我有完全相同的問題。如果這些例子能夠運行,我相信複雜的例子也能起作用。下面的代碼:Qt:多線程連接不起作用

Main.cpp的

#include "Controller_C.h" 
#include <QtWidgets/QApplication> 

int main(int argc, char *argv[]) 
{ 
    QApplication a(argc, argv); 
    Controller_C Controller; 
    Controller.SendData("Hello World!"); 
    return a.exec(); 
} 

Controller_C.cpp

#include "Controller_C.h" 
#include <qmessagebox.h> 

Controller_C::Controller_C(QWidget *parent) 
    : QMainWindow(parent), 
     Worker(new Worker_C()) 
{ 
    ui.setupUi(this); 

    Worker->moveToThread(&WorkerThread); 
    connect(&WorkerThread, SIGNAL(started()), this, SLOT(ThreadStarted())); 
    connect(this, SIGNAL(SendToWorker(QString)), Worker, SLOT(DoWork(QString))); 
    connect(Worker, SIGNAL(SendToController()), this, SLOT(ReceiveData())); 
    WorkerThread.start(); 
} 

Controller_C::~Controller_C() 
{ 

} 

void Controller_C::SendData(QString aString) 
{ 
    QThread* Controller = QThread::currentThread(); 
    QMessageBox::information(this, "Info", QString("We have send the following to the Worker Thread: %1").arg(aString)); 
    emit SendToWorker(aString); 
} 

void Controller_C::ReceiveData(QString aString) 
{ 
    QThread* Controller = QThread::currentThread(); 
    QMessageBox::information(this, "Info", QString("The Controller received the following: %1").arg(aString)); 
} 

Controller_C.h

#ifndef CONTROLLER_C_H 
#define CONTROLLER_C_H 

#include <QtWidgets/QMainWindow> 
#include "ui_Controller_C.h" 
#include "Worker_C.h" 
#include <qthread.h> 

class Controller_C : public QMainWindow 
{ 
    Q_OBJECT 

public: 
    Controller_C(QWidget *parent = 0); 
    ~Controller_C(); 

    void SendData(QString aString); 

private: 
    Ui::Qt_TestEnvironmentClass  ui; 
    Worker_C*      Worker; 
    QThread       WorkerThread; 

signals: 

    void SendToWorker(QString); 

public slots: 

    void ReceiveData(QString aString); 
}; 

#endif // CONTROLLER_C_H 

Worker_C.cpp

#include "Worker_C.h" 
#include <qmessagebox.h> 
#include <qthread.h> 

Worker_C::Worker_C() 
{ 

} 

Worker_C::~Worker_C() 
{ 

} 

void Worker_C::DoWork(QString aString) 
{ 
    QThread* Worker = QThread::currentThread(); 
    emit SendToController(aString); 
} 

Worker_C.h

#ifndef WORKER_C_H 
#define WORKER_C_H 

#include <QObject> 

class Worker_C : public QObject 
{ 
    Q_OBJECT 

public: 
    Worker_C(); 
    ~Worker_C(); 

public slots: 

    void DoWork(QString aString); 

signals: 

    void SendToController(QString); 

}; 

#endif // WORKER_C_H 

感謝您的幫助。

+1

在你的'connect'呼籲工人信號時,'QString'參數丟失 –

回答

0

就像你definied插槽的方式/信號在你的頭:

無效receiveData的(即QString ASTRING);
void SendToController(QString);

你也應該在你的連接使用它們像這樣:

connect(Worker, SIGNAL(SendToController(QString)), this, SLOT(ReceiveData(QString)));` 


+0

「我建議你在更復雜的代碼中使用QApplication :: processEvents(),如果你發出很多信號的話。「這是不好的建議。只有你的代碼已經損壞纔有意義。如果你認爲你需要'processEvents',那麼你做錯了什麼。 –

+0

您需要在某些UI應用程序中使用'processEvents',這個應用程序具有實際的待處理事件(用於更新GUI)的長時間運行的進程。我認爲'processEvents'有幾個應用程序,但我也必須承認,調用'processEvents'不應該是第一個選項。我將把它從我的帳篷中取出。 – Drayke

+0

GUI線程中沒有長時間運行的進程。如果有的話,它已經壞了。 –