Hands on mobile and embedded development with QT 5 learning notes - QNetwork

QNetworkConfiguration indicates network connection configuration, such as Wi Fi connection to a specific access point, and its service set identifier (SSID) is used as the configuration name.

Network configuration can be one of the following types:

QNetworkConfiguration::InternetAccessPoint:
This type is a typical access point, such as Wi Fi access point (AP), and can also represent Ethernet or mobile network.

QNetworkConfiguration::ServiceNetwork:
A ServiceNetwork type is a set of access points called service network access points (SNAPs). The system will determine the most suitable service network for connection according to standards such as cost, speed and availability. The configuration of QNetworkConfiguration::ServiceNetwork type can also roam between its children QNetworkConfiguration::InternetAccessPoint.
QNetworkConfiguration::UserChoice:
This type can represent the user's preferred configuration. It is used by Nokia's Maemo and Symbian platforms. The system can pop up a dialog box and ask users to select the best AP. The current hosting backend does not use this type of QNetworkConfiguration.
Usually, we need to know the type of bearer, that is, what communication protocol the connection uses. Let's look at BearerType.

QNetworkConfiguration::BearerType

This is an enumeration that specifies the underlying technology of QNetworkConfiguration. It can be one of the following:

BearerEthernet
BearerWLAN
Bearer2G
BearerCDMA2000
BearerWCDMA
BearerHSPA
BearerBluetooth
BearerWiMAX
BearerEVDO
BearerLTE
Bearer3G
Bearer4G
This can be determined by calling the bearerType() function of the QNetworkConfiguration object, as follows:

QNetworkConfiguration configuration; You can open or connect.

QNetworkConfiguration config;
if (config.bearerType() == QNetworkConfiguration::Bearer4G)
    qWarning() << "Config is using 4G";

QNetworkConfiguration::StateFlags

StateFlags is a combination of OR'd 𞓜, StateFlag values, as shown below:

Defined: the system is known but not configured
Discovered: known and configured for open()
Active: currently online
QNetworkConfiguration with Active flag will also have Discovered and Defined flags. You can check whether the configuration is Active by doing the following:

QNetworkConfiguration config;
if (config.testFlag(QNetworkConfiguration::Active))
    qWarning() << "Config is active";

Network configuration manager

QNetworkConfigurationManager allows you to obtain the QNetworkConfigurations of the system, as shown below:

QNetworkConfigurationManager manager;
QNetworkConfiguration default = manager.defaultConfiguration();

It is always wise to wait for the updateCompleted signal from the qnetwork configuration manager before use to ensure that the configuration settings are correct.
The default configuration is the default configuration defined by the system. Its status may be Active or just Discovered.

If you need to simply judge whether the system is currently online, manager - > isonline(); Returns true if the system is considered online. Online means that it connects to another device through a network that may or may not be the Internet and may or may not be routed correctly. So it can be online, but it can't access the Internet.

You may need to call updateConfigurations(), which will ask the system to update the configuration list, and then you need to listen for the updateCompleted signal before continuing.

You can obtain all configurations known to the system by calling allConfigurations(), or use allConfigurations(QNetworkConfiguration::Discovered); Filter it to a configuration with a specific status.

In this case, it returns a list of discovered configurations.

CanStartAndStopInterfaces: the system allows users to start and stop connections
DirectConnectionRouting: the connection routing is directly bound to the specified device interface
SystemSessionSupport: the system keeps the connection open until all sessions are closed
Application level roaming: applications can control roaming / migration
ForcedRoaming: the system will reconnect during roaming / migration
Data statistics: the system provides information about transmitting and receiving data
NetworkSessionRequired: the system needs a session

NetworkSession provides a way to start and stop connections and provide connection session management. When QNetworkSession is instantiated with QNetworkConfiguration of ServiceNetwork type, it can provide roaming function. On most systems, roaming will require an actual disconnect and then a new interface and / or connection. In other cases, roaming can be seamless and does not interfere with the user's data flow.

If the function of qnetworkconfiguration manager reports that it supports CanStartAndStopInterfaces, you can use QNetworkSession to open() and stop() QNetworkConfigurations.

mainwindow.h

#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include <QMainWindow>
#include <QNetworkSession>
#include <QNetworkConfigurationManager>
#include <QNetworkReply>

namespace Ui {
class MainWindow;
}

class MainWindow :  public QMainWindow
{
    Q_OBJECT

public:
    explicit MainWindow(QWidget *parent = nullptr);
    ~MainWindow();

private:
    Ui::MainWindow *ui;
    QNetworkAccessManager *accessMan;
    //QNetworkConfiguration indicates network connection configuration, such as Wi Fi connection to a specific access point, and its service set identifier (SSID) is used as the configuration name.
    QNetworkConfigurationManager *configMan;
    QString stateToString(QNetworkSession::State state);
    void printCaps(QNetworkConfigurationManager::Capabilities);

private slots:
    void stateChanged(QNetworkSession::State state);
    void finished(QNetworkReply *reply);
    void opened();
    void updateCompleted();
};

#endif // MAINWINDOW_H

mainwindow.cpp

#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QNetworkAccessManager>
#include <QNetworkConfiguration>
#include <QNetworkConfigurationManager>
#include <QNetworkSession>
#include <QNetworkRequest>
#include <QNetworkReply>
#include <QObject>

MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    //QNAM will use QNetworkSession when making network requests in the background. You can use QNetworkSession to monitor connections, as shown below
    accessMan = new QNetworkAccessManager(this);
    QObject::connect(accessMan, &QNetworkAccessManager::finished,
                     this, &MainWindow::finished);
    qWarning() << "network accessible" << accessMan->networkAccessible();
    configMan = new QNetworkConfigurationManager(this);
    connect(configMan, &QNetworkConfigurationManager::updateCompleted,
            this, &MainWindow::updateCompleted);
	//If you need to simply judge whether the system is currently online, manager - > isonline(); Returns true if the system is considered online. Online means that it connects to another device through a network that may or may not be the Internet and may or may not be routed correctly. So it can be online, but it can't access the Internet.
    qWarning() << "isOnline?" << configMan->isOnline();
}

MainWindow::~MainWindow()
{
    delete ui;
}

void MainWindow::stateChanged(QNetworkSession::State state)
{
    qDebug() << Q_FUNC_INFO << stateToString(state);
}

QString MainWindow::stateToString(QNetworkSession::State state)
{
    switch (state) {
    case QNetworkSession::NotAvailable:
        return QStringLiteral("NotAvailable");
        break;
    case QNetworkSession::Connected:
        return QStringLiteral("Connected");
        break;
    case QNetworkSession::Connecting:
        return QStringLiteral("Connecting");
        break;
    case QNetworkSession::Closing:
        return QStringLiteral("Closing");
        break;
    case QNetworkSession::Disconnected:
        return QStringLiteral("Disconnected");
        break;
    case QNetworkSession::Roaming:
        return QStringLiteral("NotAvailable");
        break;
    default:
        break;
    };
    return "Invalid";
}

void MainWindow::printCaps(QNetworkConfigurationManager::Capabilities caps)
{
    if (caps.testFlag(QNetworkConfigurationManager::CanStartAndStopInterfaces))
        qWarning() << "CanStartAndStopInterfaces";
    if (caps.testFlag(QNetworkConfigurationManager::DirectConnectionRouting))
        qWarning() << "DirectConnectionRouting";
    if (caps.testFlag(QNetworkConfigurationManager::SystemSessionSupport))
        qWarning() << "SystemSessionSupport";
    if (caps.testFlag(QNetworkConfigurationManager::ApplicationLevelRoaming))
        qWarning() << "ApplicationLevelRoaming";
    if (caps.testFlag(QNetworkConfigurationManager::ForcedRoaming))
        qWarning() << "ForcedRoaming";
    if (caps.testFlag(QNetworkConfigurationManager::DataStatistics))
        qWarning() << "DataStatistics";
    if (caps.testFlag(QNetworkConfigurationManager::NetworkSessionRequired))
        qWarning() << "NetworkSessionRequired";
    if (caps.testFlag(QNetworkConfigurationManager::CanStartAndStopInterfaces))
        qWarning() << "CanStartAndStopInterfaces";
}

void MainWindow::finished(QNetworkReply *reply)
{
    if (reply->error() == QNetworkReply::NoError)
        qWarning() << Q_FUNC_INFO;//<< reply->readAll();
    else
        qWarning() << Q_FUNC_INFO << reply->errorString();
}

void MainWindow::opened()
{
    qWarning() << Q_FUNC_INFO;
    QNetworkRequest request(QUrl("http://example.com"));

    accessMan->get(request);
}

void MainWindow::updateCompleted()
{
    qWarning() << "isOnline?" << configMan->isOnline();
    qWarning() << "network accessible" << accessMan->networkAccessible();

    printCaps(configMan->capabilities());

    QNetworkConfiguration configuration = accessMan->configuration();

    qWarning() << configuration.name() << configuration.state();
    qDebug() <<"activeConfiguration"<< accessMan->activeConfiguration().name();
    qDebug() <<"defaultConfiguration"<< configMan->defaultConfiguration().name();

    QNetworkSession *session = new QNetworkSession(configuration, this);

    qWarning() << stateToString(session->state());
    connect(session, &QNetworkSession::stateChanged, this, &MainWindow::stateChanged);
    connect(session, &QNetworkSession::opened, this, &MainWindow::opened);

    if (session->isOpen()) {
        opened();
    } else {
        qWarning() << "opening";
           session->open();
    }
}

Added by Firestorm ZERO on Sun, 02 Jan 2022 19:50:51 +0200