Qt Scanning Wi-Fi by QNetworkAccessManager

nightrayy picture nightrayy · Aug 3, 2017 · Viewed 9.7k times · Source

Welcome I have a problem with scanning Wi-Fi to get all available connecting in Wi-Fi. I have writed so far this code:

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

}

I have a problem with shows allConfigurations. I have read documentation Qt Network Configuration Manager but I do not know how to do that.

Answer

Farhad picture Farhad · Aug 3, 2017

Scanning Wi-Fi using QNetworkAccessManager.

I use QNetworkConfigurationManager class to get all WiFi s availables and show all of them into QTreeWidget.

QNetworkConfigurationManager ncm; netcfgList = ncm.allConfigurations();

App Screenshot

.pro file:

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 file:

#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 file:

#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