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: Qt 6 and networkAccessible() replacement?
Forum Update on Monday, May 27th 2025

QNetworkAccessManager: Qt 6 and networkAccessible() replacement?

Scheduled Pinned Locked Moved Solved General and Desktop
11 Posts 3 Posters 1.5k Views
  • 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.
  • R Offline
    R Offline
    Robert Hairgrove
    wrote on last edited by
    #1

    I am implementing a dialog which can send error reports to an email address as an attachment. Actually, it does not send the email directly, but via a special PHP script I have installed on the server; QNetworkAccessManager only posts the data to it, and the script sends the email.

    How do I know if I can connect to the internet in Qt 6? In Qt 5, we could call QNetworkAccessManager::networkAccessible() and enable or disable certain controls depending on the status.

    I know that there is some discussion about it in the Qt bug reports, but there still seems to be no suitable replacement. Do we just try to send requests and wait for them to time out?

    sierdzioS 1 Reply Last reply
    0
    • R Robert Hairgrove

      I am implementing a dialog which can send error reports to an email address as an attachment. Actually, it does not send the email directly, but via a special PHP script I have installed on the server; QNetworkAccessManager only posts the data to it, and the script sends the email.

      How do I know if I can connect to the internet in Qt 6? In Qt 5, we could call QNetworkAccessManager::networkAccessible() and enable or disable certain controls depending on the status.

      I know that there is some discussion about it in the Qt bug reports, but there still seems to be no suitable replacement. Do we just try to send requests and wait for them to time out?

      sierdzioS Offline
      sierdzioS Offline
      sierdzio
      Moderators
      wrote on last edited by
      #2

      @Robert-Hairgrove said in QNetworkAccessManager: Qt 6 and networkAccessible() replacement?:

      QNetworkAccessManager::networkAccessible()

      This method is / was totally completely not what people intuitively believe it to be. It checked network status (LAN or WiFi), but that does not have any bearing on the state of connection to the Internet.

      In order to know if Internet is available, do not use networkAccessible(). Use native methods (if available: for example on Android) or manually check by pinging well-known server like your mail server or google.com.

      (Z(:^

      R 1 Reply Last reply
      2
      • sierdzioS sierdzio

        @Robert-Hairgrove said in QNetworkAccessManager: Qt 6 and networkAccessible() replacement?:

        QNetworkAccessManager::networkAccessible()

        This method is / was totally completely not what people intuitively believe it to be. It checked network status (LAN or WiFi), but that does not have any bearing on the state of connection to the Internet.

        In order to know if Internet is available, do not use networkAccessible(). Use native methods (if available: for example on Android) or manually check by pinging well-known server like your mail server or google.com.

        R Offline
        R Offline
        Robert Hairgrove
        wrote on last edited by
        #3

        @sierdzio Thanks, once again. I think ping will be the simplest to implement since there are a few open-source libraries out there that can do this on Windows and Linux.

        But some servers can be configured to ignore ping requests; is there any server that is guaranteed NOT to ignore? For example, google.com answers, but microsoft.com doesn't (have to ping www.microsoft.com to get a response). The server I use to send the email also doesn't respond. I would like to make this as robust as possible.

        1 Reply Last reply
        0
        • sierdzioS Offline
          sierdzioS Offline
          sierdzio
          Moderators
          wrote on last edited by
          #4

          Google always responds to pings, but using it regularly in a commercial application - I think it's a bit rude ;-) If you have any control over your target mail server, make it respond to ping. Or, add a small web service there which will respond to some simple GET request. This way you won't open yourself to pings from random people but will still be able to check.

          (Z(:^

          R 1 Reply Last reply
          1
          • sierdzioS sierdzio

            Google always responds to pings, but using it regularly in a commercial application - I think it's a bit rude ;-) If you have any control over your target mail server, make it respond to ping. Or, add a small web service there which will respond to some simple GET request. This way you won't open yourself to pings from random people but will still be able to check.

            R Offline
            R Offline
            Robert Hairgrove
            wrote on last edited by
            #5

            @sierdzio Thanks again. I think I will just send a HEAD request every 10 seconds or so to the actual PHP resource I will need, then I won't need to install yet another library.

            If the internet is down, what kind of response would I have to expect from the call to QNetworkAccessManager::head()? A NULL QNetworkReply pointer? Or a 404 HTTP response? The documentation about this is not clear, but would seem to indicate that a valid object/pointer is returned in any case. In that case, I suppose I should call the QNetworkReply::error() function...

            1 Reply Last reply
            0
            • sierdzioS Offline
              sierdzioS Offline
              sierdzio
              Moderators
              wrote on last edited by
              #6

              No idea, to be honest, I've never used head().

              Heh, that sounds a bit funny now that I read it out loud :-)

              (Z(:^

              R 1 Reply Last reply
              0
              • sierdzioS sierdzio

                No idea, to be honest, I've never used head().

                Heh, that sounds a bit funny now that I read it out loud :-)

                R Offline
                R Offline
                Robert Hairgrove
                wrote on last edited by
                #7

                @sierdzio said in QNetworkAccessManager: Qt 6 and networkAccessible() replacement?:

                Heh, that sounds a bit funny now that I read it out loud :-)

                LOL ... neither have I! :)

                From just a little reading about it, it is similar to GET but without any text in the reply, just the HTTP headers.

                A 1 Reply Last reply
                0
                • R Robert Hairgrove

                  @sierdzio said in QNetworkAccessManager: Qt 6 and networkAccessible() replacement?:

                  Heh, that sounds a bit funny now that I read it out loud :-)

                  LOL ... neither have I! :)

                  From just a little reading about it, it is similar to GET but without any text in the reply, just the HTTP headers.

                  A Offline
                  A Offline
                  agha
                  wrote on last edited by
                  #8

                  @Robert-Hairgrove
                  How did you solve your problem?

                  R 1 Reply Last reply
                  0
                  • A agha

                    @Robert-Hairgrove
                    How did you solve your problem?

                    R Offline
                    R Offline
                    Robert Hairgrove
                    wrote on last edited by
                    #9

                    @agha I send a HEAD request to the script and see if it is successful. Otherwise, I get a timeout, and I assume that there is no internet connection.

                    Really it's pretty simple, much better than the way I was trying to do it first.

                    A 1 Reply Last reply
                    0
                    • R Robert Hairgrove

                      @agha I send a HEAD request to the script and see if it is successful. Otherwise, I get a timeout, and I assume that there is no internet connection.

                      Really it's pretty simple, much better than the way I was trying to do it first.

                      A Offline
                      A Offline
                      agha
                      wrote on last edited by
                      #10

                      @Robert-Hairgrove can you share sample code?

                      R 1 Reply Last reply
                      0
                      • A agha

                        @Robert-Hairgrove can you share sample code?

                        R Offline
                        R Offline
                        Robert Hairgrove
                        wrote on last edited by
                        #11

                        @agha You need to connect a slot to the QNetworkAccessManager::finished() signal and check for an error code in the slot function, that's all. Call the QNetworkAccessManager::head() method, passing the QNetworkRequest object you have initialized with the required URL and any custom HTTP headers, etc. If no error is returned, you are good to go. If there is no internet connection, you will get a timeout.

                        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