How to trigger internet connection on Desktop



  • Hi All,

    I am using this script to trigger internet connection as blow:

    QNetworkAccessManager* mNetworkAccessManager = new QNetworkAccessManager();
    connect(mNetworkAccessManager, SIGNAL(networkAccessibleChanged(QNetworkAccessManager::NetworkAccessibility)), this, SLOT(handleNetworkAccessibleChanged(QNetworkAccessManager::NetworkAccessibility)));

    However, it is not working properly in all PC or Mac. I have also tried to use "onlineStateChanged" of QNetworkConfigurationManager But it did not help, Especially with computer has multiple Network Interface

    So can we use separate these components to detect internet connection OR we need to combine them OR are there any solution to trigger internet connection?

    Thanks.


  • Lifetime Qt Champion

    Hi,

    Isn't QNetworkConfigurationManager what you are looking for ?



  • @SGaist

    I am looking for a stable solution to trigger internet connection on my application. As i mentioned above with QNetworkAccessManager, It is not working well. It could not trigger an event on my MAC. I also tried to use this:

    networkConfigManager = new QNetworkConfigurationManager();
    connect(networkConfigManager, SIGNAL(onlineStateChanged(bool)), this, SLOT(onlineStateChanged(bool)));
    connect(networkConfigManager, SIGNAL(configurationChanged(QNetworkConfiguration)), this, SLOT(configurationChanged(QNetworkConfiguration)));

    But somehow it could not emit network event, especially when my computer has multi network interfaces. Could you please tell me a solution in code detail?

    Thanks.



  • 1 : your codes may have memory leak issue, you can avoid this by

    QNetworkAccessManager* mNetworkAccessManager = new QNetworkAccessManager(this);
    

    Or use smart pointer to handle your resource

    //type of mNetworkAccessManager need to be std::unique_ptr<QNetworkAccessManager>
    mNetworkAccessManager = std::make_unique<QNetworkAccessManager>();
    

    Or put it on stack

    QNetworkAccessManager mNetworkAccessManager
    

    Manage resource by c++ is super easy as long as you follow good practices, because we got RAII, Qt even provide us parent and child solution to handle the resources.

    StackOverflow has a good topic, Why should C++ programmers minimize use of 'new'?. I forgot who said this(Bjarne or Herb?).

    "If you ever find yourself using new in c++, you may doing something wrong". I guess what they want to express is,
    "make sure the resource will be release when you create it".

    2 : Do you set your configuration as following?

    QNetworkConfigurationManager manager;
    QNetworkConfiguration valid_config;
    
    //find valid configuration for your apps
    auto const configs = manger.allConfigurations()
    for(auto const &config : configs){
     if(config.isValid()){
        //call state and type to check the network configuration work for your apps
       //assign that configuration to valid_config
     }
    }
    networkAccessManager->setConfiguration(valid_config);
    ``


  • @tham
    yes, i tried but it does not help.



  • @William.Tran

    Could you try to reproduce your problem with minimum codes and post the error messages?Remember to connect your network reply

    connect(&QNetworkReply::error, network_manager, &main_window::network_error, this);
    

Log in to reply
 

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