My code:
CheckNetwork.h
#include <QMainWindow>
#include <QDebug>
#include <QNetworkInterface>
#include <QNetworkAccessManager>
#include <QNetworkConfiguration>
#include <QNetworkConfigurationManager>
#include <QNetworkReply>
#include <QNetworkRequest>
#include <QNetworkSession>
namespace Ui {
class CheckNetwork;
}
class CheckNetwork : public QMainWindow
{
Q_OBJECT
public:
explicit CheckNetwork(QWidget *parent = 0);
~CheckNetwork();
protected:
void showEvent(QShowEvent *event);
private slots:
void reply_finished();
void network_configuration_manager_updateCompleted();
private:
Ui::CheckNetwork *ui;
QNetworkInterface *network_interface;
QNetworkConfigurationManager *network_configuration_manager;
QNetworkSession *network_session;
QNetworkReply *reply;
};
CheckNetwork.cpp
#include "checknetwork.h"
#include "ui_checknetwork.h"
/*
Qt Creator 3.3.1 (opensource)
Qt 5.4.1 (MSVC 2010, 32 bit)
*/
CheckNetwork::CheckNetwork(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::CheckNetwork)
{
ui->setupUi(this);
}
CheckNetwork::~CheckNetwork()
{
delete ui;
}
void CheckNetwork::showEvent(QShowEvent *event){
ui->textEdit->append("-----------------");
ui->textEdit->append("NetworkInterfaces");
ui->textEdit->append("-----------------");
ui->textEdit->append("UP AND RUNNING:");
network_interface = new QNetworkInterface();
foreach (QNetworkInterface interface, network_interface->allInterfaces()) {
if((interface.flags() & QNetworkInterface::IsUp) && (interface.flags() & QNetworkInterface::IsRunning))
ui->textEdit->append("\t" + interface.name() + " " + interface.humanReadableName() + " " + interface.hardwareAddress());
}
ui->textEdit->append("-----------------------------");
ui->textEdit->append("Network Configuration Manager");
ui->textEdit->append("-----------------------------");
ui->textEdit->append("ACTIVE:");
network_configuration_manager = new QNetworkConfigurationManager(this);
QObject::connect(network_configuration_manager, SIGNAL(updateCompleted()), this, SLOT(network_configuration_manager_updateCompleted()));
network_configuration_manager->updateConfigurations();
}
void CheckNetwork::network_configuration_manager_updateCompleted(){
foreach (QNetworkConfiguration configuration, network_configuration_manager->allConfigurations(QNetworkConfiguration::Active)) {
ui->textEdit->append("\t" + configuration.name() + " " + configuration.bearerTypeName() + " " + configuration.identifier());
}
ui->textEdit->append("DEFAULT:");
ui->textEdit->append("\t" + network_configuration_manager->defaultConfiguration().name() + " " + network_configuration_manager->defaultConfiguration().bearerTypeName());
ui->textEdit->append("---------------");
ui->textEdit->append("Network Session");
ui->textEdit->append("---------------");
network_session = new QNetworkSession(network_configuration_manager->defaultConfiguration(), this);
network_session->open();
if(network_session->isOpen()){
ui->textEdit->append("\tisOpen");
}else{
ui->textEdit->append("\tnoOpen");
}
ui->textEdit->append("---------------");
ui->textEdit->append("Network Manager");
ui->textEdit->append("---------------");
QNetworkAccessManager *network_access_manager = new QNetworkAccessManager(this);
reply = network_access_manager->get(QNetworkRequest(QUrl("http://www.google.com")));
QObject::connect(reply, SIGNAL(finished()), this, SLOT(reply_finished()));
}
void CheckNetwork::reply_finished(){
ui->textEdit->append("REPLY: " + QString::number(reply->bytesAvailable()) + " byte(s)");
ui->textEdit->append("REPLY DATAS: \n\n" + reply->readAll());
}
The QTextEdit content with a Wireless (WLAN) network card(win 8.1 64bit):
-----------------
NetworkInterfaces
-----------------
UP AND RUNNING:
{UUID} Wi-Fi XX:XX:XX:XX:XX:XX
{UUID} VirtualBox Host-Only Network XX:XX:XX:XX:XX:XX
{UUID} Loopback Pseudo-Interface 1
{UUID} Local Network Connexion* 2 XX:XX:XX:XX:XX:XX:XX:XX
-----------------------------
Network Configuration Manager
-----------------------------
ACTIVE:
NetworkWireslessName WLAN XXXXXXXXX
Local Network Connexion* 2 Unknown XXXXXXXXX
VirtualBox Host-Only Network Ethernet XXXXXXXXX
Wi-Fi Ethernet XXXXXXXXX
DEFAULT:
VirtualBox Host-Only Network Ethernet
---------------
Network Session
---------------
isOpen
---------------
Network Manager
---------------
REPLY: 256 byte(s)
REPLY DATAS:
<HTML><HEAD><meta http-equiv="content-type" content="text/html;charset=utf-8">
<TITLE>302 Moved</TITLE></HEAD><BODY>
<H1>302 Moved</H1>
The document has moved
<A HREF="http://www.google.fr/?gfe_rd=cr&ei=66-JVb_aJc7AULSAgcgE">here</A>.
</BODY></HTML>
The QTextEdit content with a Wired (LAN) network card (Win7 64bit / Win10 32bit):
-----------------
NetworkInterfaces
-----------------
UP AND RUNNING:
{UUID} Local Network Connexion XX:XX:XX:XX:XX:XX
{UUID} Loopback
-----------------------------
Network Configuration Manager
-----------------------------
ACTIVE:
DEFAULT:
---------------
Network Session
---------------
noOpen
---------------
Network Manager
---------------
REPLY: 258 byte(s)
REPLY DATAS:
<HTML><HEAD><meta http-equiv="content-type" content="text/html;charset=utf-8">
<TITLE>302 Moved</TITLE></HEAD><BODY>
<H1>302 Moved</H1>
The document has moved
<A HREF="http://www.google.fr/?gfe_rd=cr&ei=ghGJVYecFcq50wX_u4CICg">here</A>.
</BODY></HTML>
My problem is that I have no way to check the type of the network interface, if it is a " wired or wireless " is in " is online" to prevent networkmanager automatically start the connection network (QNetworkConfigurationManager::CanStartAndStopInterfaces) if the user is not connected because with this verification:
network_configuration_manager->defaultConfiguration();
if(!network_configuration_manager->isOnline()){
network_configuration_manager->deleteLater();
return;
}
this works perfectly for wireless connections , but as no default configuration is found for a connection cable with this check, the networkmanager can not continue ...in other words , how to check for a connection cable, if a network connection is active (not ping or query a server such as Google ). I read too that for some previous version similar bugs exist. In this version of Qt I have bugs in his box of messages , for example , so would it be a bug? Would anyone information ? thank you
The process should be, I think:
Get all network configurations from the manager.
For each Active configuration, create the session.
Look at the session's interface. Check
QNetworkInterface::flags()
forQNetworkInterface::IsUp
.This way you might be able to check for interfaces from non-default sessions as well.