Can't connect to wifi network using QT

594 Views Asked by At

I'm trying to connect to wifi network using the following code but it is not working also not giving any error

int main(int argc, char *argv[])
{
    QCoreApplication a(argc, argv);
    QNetworkConfiguration cfg;
    QNetworkConfigurationManager ncm;
    auto nc = ncm.allConfigurations();

    for (auto &x : nc)
    {
        if (x.bearerType() == QNetworkConfiguration::BearerWLAN)
        {
            if (x.name() == "Desired Network")
                qDebug() <<"Connecting to "<< x.name();
                cfg = x;
        }
    }
    auto session = new QNetworkSession(cfg);
    session->open();
    return a.exec();
}

It is showing the output in the terminal with "desired network's name".

Connecting to Desired Network

1

There are 1 best solutions below

1
Swift - Friday Pie On

Actual NI names in your OS may be different from ones you expect. Try debug it more verbosely:

for (auto &x : nc)
{
    if (x.bearerType() == QNetworkConfiguration::BearerWLAN)
    {
        qDebug() << x.name() << " is a WIFI";
        if (x.name() == "Desired Network")
            qDebug() <<"Connecting to "<< x.name();
            cfg = x;
    }
    else
        qDebug() << x.name() << "\n";
}

E.g. on Windows those names would be enclosed in quotes, most likely because of WMIC backend used by Qt to obtain them. If your NI is called "Desired Network" in GUI, it is actually "\"Desired Network\""