Network access
The low-level networking protocol negotiation is all handled internally by Qt, and we can easily get connected to the outside world via the QNetworkAccessManager
class. To be able to access this functionality, we need to add the network
module to cm-lib.pro
:
QT += sql network
One of Qt's weaknesses is the lack of interfaces, making unit testing difficult in some cases. If we just use QNetworkAccessManager
directly, we won’t be able to test our code without making real calls to the network, which is undesirable. However, a quick and easy solution to this problem is to hide the Qt implementation behind an interface of our own, and we will do that here.
For the purposes of this chapter, all we need to be able to do with the network is check that we have connectivity and send a HTTP GET request. With this in mind, create a header file i-network-access-manager.h
in a new folder cm-lib/source/networking
and implement the interface:
#ifndef INETWORKACCESSMANAGER_H #define INETWORKACCESSMANAGER_H...