Time for action – downloading a file
First, create an instance of QNetworkAccessManager
:
QNetworkAccessManager *m_nam = new QNetworkAccessManager(this);
Since QNetworkAccessManager
inherits QObject
, it takes a pointer to QObject
, which is used as a parent. Thus, you do not have to take care of deleting the manager later on. Furthermore, one single instance of QNetworkAccessManager
is enough for an entire application. So, either pass a pointer to the network access manager in your game or, for ease of use, create a singleton pattern and access the manager through that.
Tip
A singleton pattern ensures that a class is instantiated only once. The pattern is useful for accessing application-wide configurations or—as in our case—an instance of QNetworkAccessManager
. On the wiki pages for http://www.qtcentre.org and http://www.qt-project.org, you will find examples for different singleton patterns. A simple template-based approach would look like this (as a header file):
template <class T> class...