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. QNetworkAccessManager connection loss
Forum Updated to NodeBB v4.3 + New Features

QNetworkAccessManager connection loss

Scheduled Pinned Locked Moved Solved General and Desktop
11 Posts 5 Posters 3.4k Views 4 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.
  • M Offline
    M Offline
    Mr Gisa
    wrote on last edited by
    #1

    I just noticed something while using QNetworkAccessManager. I made an application that loops over a list of urls and request using QNetworkAccessManager, my internet connection wasn't good that day and was oscillating, like closing the connection all of the sudden, everywhere, even when using the browser, when I tried to connect to a website it was partially downloaded and then it just stop loading, sometimes the images wasn't full downloaded or not at all.

    The thing with QNetworkAccessManager that I noticed is that it doesn't detect connection loss at all, after having this issue with the oscillating internet connection I decided to make a test again, now with a good connection, to unplug the internet cable to see the Qt's reaction, and... nothing happened, it just freezes on the last request and never calls the next one (due to timeout, idk) or not even show me an error.

    Can anyone confirm that for me? Or if it's confirmed already, is there a way to solve that problem?

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

      Hi,

      Did you connect the error signal ?

      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
      • M Offline
        M Offline
        Mr Gisa
        wrote on last edited by
        #3

        Yes, but it doesn't emit when I unplug the internet cable, nothing happens. I wanted for like 30 minutes in order to see if something happens and nop.

        Pablo J. RoginaP 1 Reply Last reply
        0
        • SGaistS Offline
          SGaistS Offline
          SGaist
          Lifetime Qt Champion
          wrote on last edited by
          #4

          What version of Qt are you using ?
          On what platform ?

          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
          • M Offline
            M Offline
            Mr Gisa
            wrote on last edited by
            #5

            Qt 5.10.1, MSVC 2015, Windows 10.

            1 Reply Last reply
            0
            • M Mr Gisa

              Yes, but it doesn't emit when I unplug the internet cable, nothing happens. I wanted for like 30 minutes in order to see if something happens and nop.

              Pablo J. RoginaP Offline
              Pablo J. RoginaP Offline
              Pablo J. Rogina
              wrote on last edited by
              #6

              @Mr-Gisa said in QNetworkAccessManager connection loss:

              Yes, but it doesn't emit when I unplug the internet cable,

              what happens the opposite way, I mean, with cable disconnect if you start your app does the QNAM error signal got fired?

              Upvote the answer(s) that helped you solve the issue
              Use "Topic Tools" button to mark your post as Solved
              Add screenshots via postimage.org
              Don't ask support requests via chat/PM. Please use the forum so others can benefit from the solution in the future

              1 Reply Last reply
              0
              • mrjjM Offline
                mrjjM Offline
                mrjj
                Lifetime Qt Champion
                wrote on last edited by
                #7

                Hi
                Just as a note.

                QNetworkConfigurationManager manager;
                pManager.setConfiguration(manager.defaultConfiguration());
                
                connect(&pManager, SIGNAL(networkAccessibleChanged(QNetworkAccessManager::NetworkAccessibility)), this, SLOT(networkAccessibleChanged(QNetworkAccessManager::NetworkAccessibility)));
                

                Allowed me to detect if wifi was turned off.

                With QNetworkAccessManager, i would have to write something to have it detect connection was lost.

                Not sure you situations is the same but no harm in mentioning QNetworkConfigurationManager

                1 Reply Last reply
                1
                • M Offline
                  M Offline
                  Mr Gisa
                  wrote on last edited by
                  #8

                  @Pablo-J-Rogina I only get HostNotFoundError.

                  @mrjj It didn't work for me, it just stops and shows nothing more.

                  MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent)
                  {
                      manager.setRedirectPolicy(QNetworkRequest::NoLessSafeRedirectPolicy);
                      QNetworkConfigurationManager config;
                      manager.setConfiguration(config.defaultConfiguration());
                  
                      connect(&manager, &QNetworkAccessManager::networkAccessibleChanged, this, &MainWindow::networkAccessibleChanged);
                  
                      for (int i = 0; i < 200; ++i) {
                          auto reply = manager.get(QNetworkRequest(QUrl("https://www.google.net/")));
                          connect(reply, &QNetworkReply::finished, this, &MainWindow::handle);
                          connect(reply, QOverload<QNetworkReply::NetworkError>::of(&QNetworkReply::error), this, &MainWindow::error);
                      }
                  }
                  
                  void MainWindow::handle()
                  {
                      auto reply = qobject_cast<QNetworkReply *>(sender());
                      reply->deleteLater();
                  
                      auto status = reply->attribute(QNetworkRequest::HttpStatusCodeAttribute).toInt();
                      qDebug() << status;
                  }
                  
                  void MainWindow::error(QNetworkReply::NetworkError error)
                  {
                      qDebug() << error;
                  }
                  
                  void MainWindow::networkAccessibleChanged(QNetworkAccessManager::NetworkAccessibility accessibility)
                  {
                      qDebug() << accessibility;
                  }
                  
                  1 Reply Last reply
                  0
                  • mrjjM Offline
                    mrjjM Offline
                    mrjj
                    Lifetime Qt Champion
                    wrote on last edited by mrjj
                    #9

                    @Mr-Gisa said in QNetworkAccessManager connection loss:

                    QNetworkConfigurationManager config;

                    And its not just because it runs out of scope before signal is actually emitted ?

                    Try to have it as a member.
                    It worked on both windows and linux for me.

                    Also be aware that
                    http://doc.qt.io/qt-5/qnetworkconfiguration.html#details
                    have types. so you might need to use QNetworkConfiguration::BearerEthernet
                    also use
                    http://doc.qt.io/qt-5/qnetworkconfigurationmanager.html#allConfigurations
                    to see what you have.
                    If you have both lan/wifi and more as possible access points.

                    it might not be defaultConfiguration() you want to monitor.

                    1 Reply Last reply
                    0
                    • M Offline
                      M Offline
                      Mr Gisa
                      wrote on last edited by
                      #10

                      How to monitor whatever the user is using, if it's wifi or ethernet? I tried and I could find anything.

                      I want to emit an error message to the user when the connection is lost.

                      jsulmJ 1 Reply Last reply
                      0
                      • M Mr Gisa

                        How to monitor whatever the user is using, if it's wifi or ethernet? I tried and I could find anything.

                        I want to emit an error message to the user when the connection is lost.

                        jsulmJ Offline
                        jsulmJ Offline
                        jsulm
                        Lifetime Qt Champion
                        wrote on last edited by
                        #11

                        @Mr-Gisa From what @mrjj wrote:
                        "http://doc.qt.io/qt-5/qnetworkconfigurationmanager.html#allConfigurations
                        to see what you have."

                        https://forum.qt.io/topic/113070/qt-code-of-conduct

                        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