2015-06-23 55 views
1

我的代码:如何查看Qt中的网络接口类型?

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()); 
} 

的内容的QTextEdit与无线(WLAN)网络卡(赢8.1 64位):

----------------- 
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&amp;ei=66-JVb_aJc7AULSAgcgE">here</A>. 
</BODY></HTML> 

中的QTextEdit内容与有线(LAN)网络卡(Win7的64位/ Win10 32位):

----------------- 
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&amp;ei=ghGJVYecFcq50wX_u4CICg">here</A>. 
</BODY></HTML> 

我的问题是,我没有办法检查网络接口的类型,如果是“有线或无线的”是“在线”,以防止网络管理器自动启动,如果用户没有因为此验证连接的连接网络(QNetworkConfigurationManager :: CanStartAndStopInterfaces):

network_configuration_manager->defaultConfiguration(); 
if(!network_configuration_manager->isOnline()){   
     network_configuration_manager->deleteLater(); 
     return; 
} 

这完全适用于无线连接,但是因为没有为连接ca找到默认配置通过此检查,网络管理员无法继续......换句话说,如果网络连接处于活动状态(不是ping或查询诸如Google之类的服务器),如何检查连接电缆。我也读过一些以前的版本存在类似的bug。在这个版本的Qt中,例如,他的邮箱中有错误,那么它会是一个错误?会有人的信息?谢谢

回答

1

的过程应该是,我想:

  1. 获取从管理所有网络配置。

  2. 对于每个活动配置,创建会话。

  3. 看看会话的界面。检查QNetworkInterface::flags()QNetworkInterface::IsUp

这样,您也许能够检查非默认会话的接口。

+0

感谢您的反应和你的时间,但对于第1步你推荐我QT发现没有配置用于“有线”类型的接口。使用defautlconfiguration()或allconfiguration()可以返回任何类似于我上面的textEdit的内容(我指定我根本不想查询任何内容),只是没有为所有类型的“有线”网络配置找到配置。我终于决定使用回复来处理错误(QNetworkReply,使用他的错误信号,似乎我的无线类型接口不再自动连接时执行我的QNetworkManager的实例) –

+0

为此我想这样做就足够了,但是我我想知道这个问题在哪里,我认为QT最终会产生内部问题。纠正我或给我一个地方,看看我是否错了我可能不是因为它应该。再次感谢你 –

+0

这就是我开始做的,它在Linux中对我有用,但是当我在Windows中测试它时,它失败了!QNetworkSession.interface()。isValid()在Windows中是False这样的! – BuvinJ

2

你应该能够检查承载类型,看它是否是WiFi连接,即:

configuration.bearerType() == QNetworkConfiguration::BearerWLAN