Single network manager per application

One single instance of QNetworkAccessManager is enough for an entire application. For example, you can create an instance of QNetworkAccessManager in your main window class and pass a pointer to it to all the other places where it's needed. For ease of use, you can also create a singleton and access the manager through that.

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.

A simple template-based approach to create a singleton will look like this (as a header file):

template <class T> class Singleton { public: static T& instance() { static T static_instance; return ...

Get Game Programming using Qt 5 Beginner's Guide - Second Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.