Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. How to trigger internet connection on Desktop
Forum Updated to NodeBB v4.3 + New Features

How to trigger internet connection on Desktop

Scheduled Pinned Locked Moved Unsolved General and Desktop
6 Posts 3 Posters 2.0k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • W Offline
    W Offline
    William.Tran
    wrote on last edited by
    #1

    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.

    1 Reply Last reply
    0
    • SGaistS Offline
      SGaistS Offline
      SGaist
      Lifetime Qt Champion
      wrote on last edited by
      #2

      Hi,

      Isn't QNetworkConfigurationManager what you are looking for ?

      Interested in AI ? www.idiap.ch
      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

      1 Reply Last reply
      0
      • W Offline
        W Offline
        William.Tran
        wrote on last edited by
        #3

        @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 Reply Last reply
        0
        • thamT Offline
          thamT Offline
          tham
          wrote on last edited by tham
          #4

          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);
          ``
          1 Reply Last reply
          0
          • W Offline
            W Offline
            William.Tran
            wrote on last edited by
            #5

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

            thamT 1 Reply Last reply
            0
            • W William.Tran

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

              thamT Offline
              thamT Offline
              tham
              wrote on last edited by
              #6

              @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);
              
              1 Reply Last reply
              0

              • Login

              • Login or register to search.
              • First post
                Last post
              0
              • Categories
              • Recent
              • Tags
              • Popular
              • Users
              • Groups
              • Search
              • Get Qt Extensions
              • Unsolved