PDA

View Full Version : QTcpSocket connected signal problem



probine
22nd December 2006, 14:31
I have a QTcpSocket that connects to a server.

I instantiate the tcpSocket, and connectToHost.

Everything is fine, then the "connected" signal should be emitted, but it is not.



tcpSocket->connectToHost(QHostAddress("localhost"), quint16(35310));
connect(tcpSocket, SIGNAL(connected()), this, SLOT(connectedToServer()));
connect(tcpSocket, SIGNAL(readyRead()), this, SLOT(read()));
connect(tcpSocket, SIGNAL(disconnected()), this, SLOT(disconnected()));


What is wrong ?
Why the "connected" signal is emitted when I close the window ?

wysota
19th January 2007, 11:29
We'd have to see more code to be able to answer that... Maybe you're blocking the flow somewhere?

jpn
19th January 2007, 12:13
Sure, might not be the case in this one, but it is generally a good idea to create the signal-slot connection first and invoke the method emitting signals afterwards..

In this case the connectToHost() is not blocking, and the socket actually starts connecting as soon as it's invoked. If the connection is fast enough, you can easily miss a state change or two (during the beginning of the connection).

wysota
19th January 2007, 12:18
But why then does he get the "connected" signal later when he closes the dialog? Of course your remark is true, but I doubt that's the thing causing trouble in this particular situation.