Zelid
Zelid

Reputation: 7195

QNetworkAccessManager to download image previews from the Internet crashes

I have QListWidget each custom widget in it contains a label that gets it's QPixmap from the Internet using QNetworkAccessManager defined in each custom widget.

Everything works fine when there are 10, 20, 30 widgets added to QListWidget.

When amount of widgets (each of them has QNetworkAccessManager to load an image from the Internet) reached 300/400 the application became broken. Thread errors arrive.

How to avoid threading errors when there are a lot (300-2000) QNetworkAccessManagers in application which load images from the Internet the same time?

I'm using Qt 5.5

Upvotes: 1

Views: 191

Answers (1)

Orest Hera
Orest Hera

Reputation: 6776

Normally only one QNetworkAccessManagers is needed per application. It is quite heavy object that is able simultanenously manage many parallel connections. It also manages sockets and sessions, so all HTTP requests to the same server can be maintained by single (or few; it is configurable) TCP connection (once a request is finished the TCP connection is not closed and the next request is started in the same connection).

The usual reason for creation many instances of QNetworkAccessManagers is the trouble of hanling its asynchronous signals from separate connections. Note that it is possible to connect signals not from but from QNetworkReply to approriate slot.

The functions QNetworkAccessManager::get() and QNetworkAccessManager::post() return pointer to QNetworkReply object that is responsible for that request. The signals of that QNetworkReply object can be connected to the slots of appropriate widget. So, once that reply is finished it will trigger only one specific widget slot, see for example Singleton QNetworkAccessManager not directing the response to desired form

Upvotes: 2

Related Questions