I am having problems with QTcpSocket, it’s not emitting any signals :/
void NetworkInstance::run()
{
m_pSocket = new QTcpSocket();
connect(m_pSocket, SIGNAL(error(QAbstractSocket::SocketError)), this, SLOT(socketError()));
connect(m_pSocket, SIGNAL(hostFound()), this, SLOT(socketHostLookupDone()));
connect(m_pSocket, SIGNAL(connected()), this, SLOT(socketConnected()));
connect(m_pSocket, SIGNAL(disconnected()), this, SLOT(socketDisconnected()));
connect(m_pSocket, SIGNAL(readyRead()), this, SLOT(socketReadyRead()));
QSettings s;
s.beginGroup("network");
emit log(QString("Connection to: ").append(s.value("host").toString()).append(":").append(s.value("port").toString()));
m_pSocket->connectToHost(s.value("host").toString(), s.value("port").toInt());
s.endGroup();
exec();
}
This is my code, I don’t see any error in it, but non of the connected signal is emitting (hostFound, connected, etc.). On server I can see that connection is established and data sent, but nothing happens on client end. The NetworkInstance is extenting QThread.
Based on my earlier comment that subclassing QThread is wrong, what you need to do is create your class inherited from QObject and then move that to the new QThread.
So you'd have a class which looks something like this: -
class NetworkInstance : public QObject
{
Q_OBJECT
public:
NetworkInstance();
public slots:
void Run();
void socketConnected();
void socketError();
// etc for other slots...
private:
class QTCPSocket* m_pSocket;
}
Create your Network instance object and thread: -
QThread* pThread = new QThread;
NetworkInstance* pNetworkInstance = new NetworkInstance;
Create the QTCPSocket instance and connect the signals / slots in your NetworkInstance class and then create the QThread and move your class to the thread: -
pNetworkInstance->moveToThread(pThread);
Finally, start the thread running: -
pThread->start();
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With