Is it possible to set a timeout for QNetworkReply
, so for a long time there is no reply from server, it could emit the finished()
signal?
It's not possible without using a timer, but you don't have to explicitly write timer code. The functionality can be neatly packaged into a static function that acts as if the timeout was a settable property of the reply - see example below. There are two issues: How to handle a timeout on a request that is in progress.
And since the QReplyTimeout class is parented to the QNetworkReply, it will be destroyed automatically. Review the code for any pitfalls, memory leaks, invalid casts and if it's generally in a good style. Show activity on this post. It's a simple class that allocates quite a few times on the heap.
Add a static helper method that uses this class to set a timeout on a network request. The fact that you need to allocate ReplyTimeout is an implementation detail of sorts that could be abstracted out that way. Check if the reply is running before you set a timeout on it.
A QObject does about two allocations when the first connection is added to it. Use Qt-5 style connections, but that doesn't apply anymore in light of #1 above. Add a static helper method that uses this class to set a timeout on a network request.
with Qt 5.15 it should be a built-in feature - just found out that a 10 yrs old bug is fixed :) https://codereview.qt-project.org/c/qt/qtbase/+/278064
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