If I want to write data to a remote side and wait for its answer, I need at least a waitForReadyRead
. But before calling that, do I need to manually flush the output queue using waitForBytesWritten
, or does Qt automatically flush the write queue for me? I am operating synchronously (blocking) and therefore in this function I am unable to use the event loop or a local event loop.
When using std::cin
, we can be sure that previously written bytes by std::cout
will have been flushed. That's the analoguous situation - does it apply to Qt sockets aswell?
If you look at the source code, being an abstract base class, QIODevice does very little in waitForReadyRead and it's up to the implementation of inherited classes:
As you've stated that you're operating synchronously, I assume you've chosen this for a reason and are aware that on the main thread, any GUI present will freeze during a call to
waitForReadyRead
. Usually, asynchronous usage for QIODevice is preferred, with Qt being an event-driven framework.However, the Qt docs state:
Therefore, if your QIODevice is a socket such as QTcpSocket, then no, you should not be required to call
waitForBytesWritten
when callingwaitForReadyRead
.In the case of a non-synchronous device, it would be required.