简体   繁体   English

C ++ / Qt-从一个线程到另一线程插槽的信号

[英]C++/Qt - Signal from one thread to another threads slot

I am setting up a client working with QTcpSocket . 我正在设置使用QTcpSocket的客户端。 The part where the connection to the server is set is to implement as a thread. 设置到服务器的连接的部分是作为线程实现的。

This is how my class looks like, which implements the connection functions (.hpp): 这是我的类的样子,它实现了连接功能(.hpp):

class Connector: public QObject {
Q_OBJECT
public:
Connector(QString hexHost);
virtual ~Connector();

// Start to establish a connection
void startConnection();
// End thread
void stopConnection();

signals:
// Emitted, if connector should get to work
void startTransforming();
// Emitted, if transformation from hex to dig is complete
void transformationFinished();
// Emitted, if connection is established
void connectionEstd();
// Emitted, if connection failed
void connectionFailed();
// Emitted, if work is done
void doneConnecting();

public slots:
// Connect to server
void connectToServer();

private:
// IP of the server
QString addressHost;
// Socket
QTcpSocket aQTcpSocket;
};

And this is how I implemented this class (.cpp); 这就是我实现此类(.cpp)的方式;

Connector::Connector(QString hexHost)
{
// Save user data
addressHost = hexHost;

// Start thread
bool result = QObject::connect(this, SIGNAL(startTransforming()), this, SLOT(transformIP()));
Q_ASSERT(result);

// Start trying to establish a connection
result = QObject::connect(this, SIGNAL(transformationFinished()), this, SLOT(connectToServer()));
Q_ASSERT(result);

// End thread
result = QObject::connect(this, SIGNAL(doneConnecting()), this, SLOT(deleteLater()));
Q_ASSERT(result);
Q_UNUSED(result);
}

Connector::~Connector()
{
QObject::disconnect(this, SIGNAL(startTransforming()), this, SLOT(transformIP()));
QObject::disconnect(this, SIGNAL(transformationFinished()), this, SLOT(connectToServer()));
QObject::disconnect(this, SIGNAL(doneConnecting()), this, SLOT(deleteLater()));
}

void Connector::transformIP()
{
[...]

// Emit ready signal
emit transformationFinished();
}

void Connector::connectToServer()
{
aQTcpSocket.connectToHost(addressHost, port);
    result = aQTcpSocket.waitForConnected(5000);

    if(result)
{
    emit connectionFailed();
    emit doneConnecting();
        std::clog << "Connection failed!" << std::endl;   // This debug message is printed during runtime
    return;
    }

// Emit signal
emit connectionEstd();
}

void Connector::startConnection()
{
emit startTransforming();
}

void Connector::stopConnection()
{
emit doneConnecting();
}

I start the thread by: 我通过以下方式启动线程:

[...]

// Create new thread
QThread *conThread = new QThread();
// Create connector object
aConnector = new Connector(hexHost);
aConnector->moveToThread(conThread);
conThread->start();

// Clean up thread
bool result = QObject::connect(aConnector, SIGNAL(destroyed()), conThread, SLOT(quit()));
Q_ASSERT(result);

result = QObject::connect(conThread, SIGNAL(finished()), conThread, SLOT(deleteLater()));
Q_ASSERT(result);

// Connect signals
result = QObject::connect(aConnector, SIGNAL(connectionFailed()), this, SLOT(onConnectionFailed()));
Q_ASSERT(result);

result = QObject::connect(aConnector, SIGNAL(connectionEstd()), this, SLOT(onConnectionEstd()));
Q_ASSERT(result);
Q_UNUSED(result);

// Get connector to work
aConnector->startConnection();

[...]

When i test the program, the connector is created correctly. 当我测试程序时,将正确创建连接器。 He starts the function transformIP() and connectToServer(). 他启动了函数transformIP()和connectToServer()。 Currently i do not have a server running, so the client is not able to connect. 目前我没有正在运行的服务器,因此客户端无法连接。 This should result in emitting the signal connectionFailed() The client class, which starts the thread with the connector object, should receive this signal and should react on it. 这应该导致发出信号connectionFailed(),以连接器对象启动线程的客户端类应接收该信号并对它作出反应。

The problem is: The signal does not seem to be emitted, because the client class does not react on it. 问题是:信号似乎没有发出,因为客户端类没有对此作出反应。 Here is the part in the client class, where i connect the signal to a certain slot: 这是客户端类中的部分,我将信号连接到某个插槽:

 // Connect signals
result = QObject::connect(aConnector, SIGNAL(connectionFailed()), this, SLOT(onConnectionFailed()));
Q_ASSERT(result);

Would be great, if someone had an idea how to solve this, thanks :) 如果有人知道如何解决这个问题,那就太好了,谢谢:)

It looks like QTcpSocket.connectToHost return value is void and the call is asynchronous. 看起来QTcpSocket.connectToHost返回值是void,并且调用是异步的。 What you need to do, is to call QTcpSocket.connectToHost() and wait for the connection to finish using QTcpSocket.waitForConnected( CONNECT_TIME_OUT ) and have else handle the bad connection 您需要做的是调用QTcpSocket.connectToHost()并使用QTcpSocket.waitForConnected( CONNECT_TIME_OUT )等待连接完成,然后让其他人处理错误的连接

Example: 例:

QTcpSocket socket;
socket.connectToHost(QHostAddress("172.0.0.1", 8080);
if (!socket.waitForConnected(5000)) {
    emit connectionFailed();
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM