Qt通过QNetworkAccessManager扫描Wi-Fi

时间:2017-08-03 14:19:41

标签: c++ qt wifi qnetworkaccessmanager

欢迎我在扫描Wi-Fi时遇到问题,无法通过Wi-Fi连接。到目前为止,我已经写了这段代码:

#include <QCoreApplication>
#include <QNetworkConfigurationManager>
#include <QNetworkConfiguration>
#include <QDebug>
#include <QNetworkSession>

int main(int argc, char *argv[])

{
    QCoreApplication a(argc, argv);
    QNetworkConfigurationManager ncm;
    QNetworkConfiguration cfg;
    QNetworkConfiguration::StateFlags flags;

    int count = QNetworkConfiguration::Active;

   qDebug() << "Amount available connect in Wi-Fi :" << count;

   qDebug() << ncm.allConfigurations(flags = 0);

   return a.exec();

}

我在显示allConfiguration时遇到问题。我看过文档 Qt Network Configuration Manager 但我不知道该怎么做。

1 个答案:

答案 0 :(得分:1)

使用QNetworkAccessManager扫描Wi-Fi。

我使用QNetworkConfigurationManager类来获取所有WiFi的可用内容并将它们全部显示到QTreeWidget中。

QNetworkConfigurationManager ncm;     netcfgList = ncm.allConfigurations();

App Screenshot

.pro文件:

QT       += core gui network

greaterThan(QT_MAJOR_VERSION, 4): QT += widgets

TARGET = WiFi
TEMPLATE = app


SOURCES += main.cpp\
        mainwindow.cpp

HEADERS  += mainwindow.h

FORMS    += mainwindow.ui

.cpp文件:

#include "mainwindow.h"
#include "ui_mainwindow.h"

MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);

    findTimer = new QTimer();
    findTimer->setInterval(1000);
    connect(findTimer,&QTimer::timeout,this,&MainWindow::findActiveWirelesses);
    findTimer->start();
    foundCount = 0;
    ui->treeWidgetWiFis->setColumnWidth(0,50);
    ui->treeWidgetWiFis->setColumnWidth(1,200);
    findActiveWirelesses();
}

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

void MainWindow::findActiveWirelesses()
{
    QNetworkConfigurationManager ncm;
    netcfgList = ncm.allConfigurations();
    WiFisList.clear();
    for (auto &x : netcfgList)
    {
        if (x.bearerType() == QNetworkConfiguration::BearerWLAN)
        {
            if(x.name() == "")
                WiFisList << "Unknown(Other Network)";
            else
                WiFisList << x.name();

            qDebug() << x.type();
        }
    }
    for(int i=0; i<WiFisList.size(); i++)
    {
        bool exist = false;
        QTreeWidgetItem * item = new QTreeWidgetItem();
        for(int j=0; j<ui->treeWidgetWiFis->topLevelItemCount(); j++)
        {
            QTreeWidgetItem *index = ui->treeWidgetWiFis->topLevelItem(j);
            QString str = index->text(1);
            if(str == WiFisList[i])
            {
                exist = true;
                break;
            }
        }
        if(!exist)
        {
            item->setTextAlignment(0,Qt::AlignVCenter);
            item->setTextAlignment(1,Qt::AlignHCenter);
            item->setText(0,QString::number(++foundCount));
            item->setText(1,WiFisList[i]);
            ui->treeWidgetWiFis->addTopLevelItem(item);
        }
    }
}

.h文件:

#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include <QMainWindow>
#include <QTimer>
#include <QList>
#include <QInputDialog>
#include <QStandardItem>
#include <QStandardItemModel>
#include <QNetworkConfiguration>
#include <QNetworkConfigurationManager>
#include <QNetworkSession>

namespace Ui {
class MainWindow;
}

class MainWindow : public QMainWindow
{
    Q_OBJECT

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

    int foundCount;
    QNetworkConfiguration netcfg;
    QStringList WiFisList;
    QList<QNetworkConfiguration> netcfgList;

public slots:
    void findActiveWirelesses();


private:
    Ui::MainWindow *ui;
    QTimer *findTimer;
    QStandardItemModel* listModel;
    QNetworkSession *session;
};

#endif // MAINWINDOW_H