Qt:多线程连接不工作
Qt: Multithreading connect does not work
我想连接两个线程。一个线程是我的应用程序的主线程,另一个是工作线程。我的代码基于以下示例doc.qt.io/qt-5/。对我来说,这并不完全有效。我正在发送一个QString到我的WorkerThread(这工作),并希望将其发送回主线程(不工作)。在问我为什么这么做之前,这只是一个非常简单的例子。真正的代码要复杂得多,但我有完全相同的问题。如果这些例子能运行,我很确定复杂的例子也能运行。下面的代码是:
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
谢谢你的帮助。
就像你在header中定义Slot/Signal一样:
void ReceiveData(QString aString);
空白SendToController (QString);
你也应该像这样在你的connect中使用它们:
connect(Worker, SIGNAL(SendToController(QString)), this, SLOT(ReceiveData(QString)));`
相关文章:
- C++ TCP 套接字通信 - 连接按预期工作,几秒钟后失败,没有收到新数据,read() 和 recv() 块
- 将信号从工作线程类连接到控制器类 - QThreads
- Live555MediaServer 在每个新连接时重新启动流。为什么将"reuseSource"设置为 true 无法按预期工作?
- Visual Studio 中的套接字 c++ 应用程序无法通过两台计算机之间的以太网连接工作
- QSqlDatabasePrivate::removeDatabase:连接'myConnectionName'仍在使用中,所有查询将停止工作
- 与OPC UA服务器的连接通过思科工作,但不通过Sinema RC客户端
- 连接四个C 下降功能无法正常工作
- C#客户端与C 服务器连接时停止,但是在与C#服务器连接时工作正常
- 将工作线程信号与主窗口插槽 (Qt5) 连接时出错
- winsock连接调用崩溃如果多个线程运行,则在一个线程中正常工作
- 检查连接4的获胜功能对角线是否不工作
- 在 c++ 中,如何远程设置注册表项(连接工作正常,但设置不起作用!
- mysql 远程连接 - mysql CL 客户端工作,C++应用程序不工作
- 来自 boost::asio::async_write 的 WriteHandler 在连接断开时无法正常工作(防火墙/手动断开网络)
- 连接传感器后,Arduino驱动的伺服电机停止工作
- 425 无法在我的程序上打开数据连接,而 Filezilla 工作
- 在Recv()上拒绝连接,而发送似乎工作正常
- 使Qt5连接式工作在Qt4中
- c++宏连接在gcc下不能工作
- 连接不匹配的字符串在VC2015工作-如何