Search code examples
c++qtsignals-slotsqueued-connection

singleHow does Qt::QueuedConnection work in a single thread application?


I'm doing:

connect(tcpSocket,SIGNAL(readyRead()), this, SLOT(onTCPDataArrived()), Qt::QueuedConnection);

But the times the slot gets called is much less than it should have.

It seems like it missed a lot of signals, probably because the slot takes a long time(it does).

I added a 2ms delay on the transmission side between tcp writes and it gets better: the slot get called more.

Question: if the signal and slot are in the same thread, it the receiver still queuing incoming signals when the slot is already running?


Solution

  • With TCP/IP there are never any guarantees as to how the data is chopped up into packets at the receiving end. So, you might send 10 bytes "all at once", and the receiving end is allowed to receive anywhere between 10 notifications (one for every byte) and 0 notifications. Why zero? Because you possibly could send another 10 bytes soon thereafter, and there will be one notification for all 20 of them. This has nothing to do with Qt at all.

    So, when readyRead() fires, you must read all the data that is available for reading. You will not be notified about this data again.