Connect to a Wifi network - without c++11 (without 'auto')



  • Hi all,

    I am currectly developping two applications, one of them runs on an android device, it activates the Wifi hotspot and waits for someone to connect to it.
    When someone is connected, it has to send a message (string) each time a button is clicked.

    The second application is a pretty big project that has been started a while ago (with Qt 4.8.4), I have to add the possibility to connect to the phone's hotspot and wait for a message to be received.
    I have found an easy way to connect to a Wifi network :

    QNetworkConfiguration cfg;
    QNetworkConfigurationManager ncm;
    auto nc = ncm.allConfigurations();
    
    for (auto &x : nc)
    {
    	if (x.bearerType() == QNetworkConfiguration::BearerWLAN)
    	{
    		if (x.name() == "YouDesiredNetwork")
    			cfg = x;
    	}
    }
    
    auto session = new QNetworkSession(cfg, this);
    session->open();
    

    Unfortunately this code uses c++11 syntax, and I can't get it working on my Qt 4.8.4.
    Adding "QMAKE_CXXFLAGX += -std=c++0x" causes "::swprintf and ::vswprintf has not been declared".
    Wrapping their declaration in 'cwchar' with

    #ifndef __STRICT_ANSI__
        using ::swprintf;
        using ::vswprintf;
    #endif
    

    Causes crash because of segmentation fault.

    I was wondering if it was possible to connect to a Wifi Network without c++11 syntax ?
    I guess it is, but I am not familiar with c++11 so I'm ,not sure how to modify the code.

    Any help would be appreciated

    Thank you in advance


  • Lifetime Qt Champion

    Hi,

    I don't remember if CONFIG += c++11 was already working for that version of Qt.

    In any case you should just replace auto with the correct classes name and use foreach from Qt

    QList<QNetworkConfiguration> ncs = ncm.allConfigurations();
    foreach(const QNetworkConfiguration &configuration, ncList) {
    // rest is the same as before
    

    Hope it helps



  • Hi,

    thanks for your help, it compiles without error now.

    Unfortunately, it crashes at runtime (I do have added 'CONFIG += network' in my .pro file).
    Weirdly, it doesn't crash on debug.

    If I comment this part :

    ncs = ncm.allConfigurations();
    foreach (const QNetworkConfiguration &configuration, ncs) {
        if(configuration.bearerType() == QNetworkConfiguration::BearerWLAN)
        {
            if(configuration.name() == "JBLiagre.com")
            {
                m_notif_ui->label_connexion->setStyleSheet("QLabel{color : green;}");
                m_notif_ui->label_connexion->setText("Connecté à 'JBLiagre.com'  !");
                m_notif_ui->label_connexion->setAlignment(Qt::AlignCenter);
                cfg = configuration;
            }
        }
    }
    

    It runs without any problem.
    I am waiting for the IT departement to give me a Wifi-card so for the moment I do not have Wifi on my computer, could this be related ?



  • @zarachbaal said:

    cfg = configuration;

    how and where is cfg defined and used??
    Have you able to collect the crash dump??



  • My full code related to QtNetwork is :

    #include <QtNetwork/QtNetwork>
    
    QNetworkConfiguration cfg;
    QNetworkConfigurationManager ncm;
    QList<QNetworkConfiguration> ncs;
    
    ncs = ncm.allConfigurations();
    foreach (const QNetworkConfiguration &configuration, ncs) {
        if(configuration.bearerType() == QNetworkConfiguration::BearerWLAN)
        {
            if(configuration.name() == "test.com")
            {
                cfg = configuration;
            }
        }
    }
    

    If I comment everything but :

    #include <QtNetwork/QtNetwork>
    
    QNetworkConfiguration cfg;
    QNetworkConfigurationManager ncm;
    QList<QNetworkConfiguration> ncs;
    
    ncs = ncm.allConfigurations();
    

    It crashes too.

    What do you mean by "collecting the crash dump" ?


  • Lifetime Qt Champion

    When you have a crash you generally get a stack trace (if you are using a debug build) so you can see where exactly the problem happened



  • Ok that's what I thought.

    The problem is that in Debug mode (via F5 in Qt Creator), my program doesn't crash so I won't be able to post the crash dump.

    I will try my program this evening on a computer that has a Wifi card to see if it changes anything.


  • Lifetime Qt Champion

    Is the crash happening in Qt Creator with a release build ?



  • Yes it is



  • Qt4.8 :

    Tier 1 Platforms

    All Tier 1 platforms are subjected to our unit test suite and other internal testing tools on a frequent basis (prior to new version releases, source tree branching, and at other significant period points in the development process). Errors or bugs discovered in these platforms are prioritized for correction by the development team. Significant errors discovered in Tier 1 platforms can impact release dates and Qt Development Frameworks strives to resolve all known high priority errors in Tier 1 platforms prior to new version releases.

    Platform Compilers
    Ubuntu Linux 10.04 (32-bit) As provided by Ubuntu
    Ubuntu Linux 11.10 (64-bit) As provided by Ubuntu
    Microsoft Windows XP SP3 (32-bit) MSVC 2008
    Microsoft Windows 7 (32-bit) MSVC 2008
    Microsoft Windows 7 (32-bit) MSVC 2010 SP1
    Apple Mac OS X 10.6 "Snow Leopard" (64-bit) As provided by Apple
    Apple Mac OS X 10.7 "Lion" (64-bit) As provided by Apple
    Tier 2 Platforms

    Tier 2 platforms are subject to ad hoc and internal testing. However, Qt users should note that errors may be present in released product versions for Tier 2 platforms and, subject to resource availability, known errors in Tier 2 platforms may or may not be corrected prior to new version releases.

    Platform Compilers
    Ubuntu Linux 10.04 QWS (x86 32-bit) As provided by Ubuntu
    Ubuntu Linux 10.04 (32-bit) Intel Compiler [version 12]
    Ubuntu Linux 10.04 (64-bit) As provided by Ubuntu
    Microsoft Windows XP SP3 (32-bit) GCC 4.4 (MinGW)
    Microsoft Windows XP SP3 (32-bit) MinGW-builds gcc 4.8.2 (32 bit)
    Microsoft Windows 7 (64-bit) MSVC 2010 SP1
    Apple Mac OS X 10.6 "Snow Leopard" Cocoa (32-bit) As provided by Apple

    I am not sure about other, but none of the Microsoft compilers listed provided full C++11
    support.

    I know only one way to debug release code print out information on the screen.



  • I just created a new project and added the code above.
    It works well, no errors or crashes.
    So the problem is in my project, I will look into it but I do not see what could cause the problem.



  • maybe you should also try finding the problem (in the release version) the old fashion way: just place cout<<"line X" statements here and there and pinpoint the line where the problem occurs


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.