Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to set QNetworkReply timeout without external timer?

Tags:

c++

qt

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?

like image 784
Nyaruko Avatar asked May 25 '16 18:05

Nyaruko


People also ask

Is it possible to set a timeout without a timer?

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.

What is the qreplytimeout class in qnetworkreply?

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.

How do I set a timeout on a network request?

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.

How to set a timeout on a QObject request?

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.


1 Answers

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

like image 75
ekkescorner Avatar answered Oct 25 '22 13:10

ekkescorner