QTcpSocket - How do I know when it is disconnected ?
I have a thread that handles different connections from different computers.
I want to know when one of those connections (telnet) has been closed. I am trying to use the disconnect signal, but the method clientDisconnected is never called.
Code:
# include <QTcpSocket>
# include <iostream>
# include "client.h"
# include "server.h"
using namespace std;
Client
:: Client(int socketDescriptor, Server
*server,
QObject *parent
) : QThread(parent
){
this->socketDescriptor = socketDescriptor;
this->server = server;
connect(this, SIGNAL(finished()), this, SLOT(finished()));
}
void Client :: run()
{
cout << "void Client :: run()\n";
if(!tcpSocket->setSocketDescriptor(socketDescriptor))
{
cout << "Error - tcpSocket descriptor\n";
return;
}
connect(tcpSocket, SIGNAL(readyRead()), this, SLOT(readData()));
connect(tcpSocket, SIGNAL(disconnected()), this, SLOT(clientDisconnected()));
exec();
}
void Client :: readData()
{
cout << "void Client :: readData()\n";
server->addClient("12345", this);
}
void Client :: clientDisconnected()
{
cout << "void Client :: clientDisconnected()\n";
}
void Client :: finished()
{
cout << "The thread has finished\n";
}
Why ? What is wrong ?
Re: QTcpSocket - How do I know when it is disconnected ?
Why is the QTcpSocket disconnect signal never emitted ?
Re: QTcpSocket - How do I know when it is disconnected ?
I found the problem:
The signal was never emitted because I was only compiling the program with "make". In order to have the signal working I had to type "qmake" first, then "make".
Out of this test I assume that whenever I code something with SIGNALS and SLOTS, then I will "qmake" my program first.