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 properly cancel QNetworkAccessManager request?
Forum Updated to NodeBB v4.3 + New Features

How to properly cancel QNetworkAccessManager request?

Scheduled Pinned Locked Moved General and Desktop
8 Posts 2 Posters 17.9k 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.
  • G Offline
    G Offline
    Gourmand
    wrote on last edited by
    #1

    In application I need sometimes stop working QNetworkAccessManager to prevent it emit finished() signal and free network resources. Of course it is possible just temporary disconnect this signal but it is not useful in current code. There are several managers in application and I have to track which one was disconnected to reconnect it again when needed. And signal disconnection won't free resources and close open connections. I'd better just send a signal or something like QNetworkAccessManager::cancel(); but I do not see this function. I do not even see how to do this with QNetworkRequest or QNetworkSession. Is there any way? Anybody help?

    1 Reply Last reply
    0
    • G Offline
      G Offline
      Gourmand
      wrote on last edited by
      #2

      Looks like solved by myself...

      @ QNetworkSession session( manager->configuration() );
      session.stop();@

      is that correct?

      1 Reply Last reply
      0
      • S Offline
        S Offline
        shanek
        wrote on last edited by
        #3

        You should abort the active QNetworkReply using the abort() slot.

        http://doc-snapshot.qt-project.org/5.0/qnetworkreply.html#abort

        Using QNetworkSession::stop() will disconnect you from the internet, if allowed. (which resets all connections).

        1 Reply Last reply
        0
        • G Offline
          G Offline
          Gourmand
          wrote on last edited by
          #4

          Does this mean? - I have to save QNetworkReplay* reply; returned by QNetworkAccessManager::get() and call replay->abort() for this reply when needed

          stop() will disconnect only for one QNetworkAccessManager or for all managers? Exactly I need disconnect one manager from it's connections.

          BTW: I use Qt 4.7

          1 Reply Last reply
          0
          • S Offline
            S Offline
            shanek
            wrote on last edited by
            #5

            You maybe using its readyRead and finished signals already?
            abort() is a slot, so you could just connect a signal from your own class to the object.

            e.g.
            QNetworkReply *nr = manager.get(request);
            QObject::connect(this, SIGNAL(cancelDownload()), nr, SLOT(abort()));

            And then when you want to cancel downloads:
            emit cancelDownload();

            QNetworkSession is for handling the low level network adaptor.
            stop() is equivalent to unplugging the network cable.

            1 Reply Last reply
            1
            • G Offline
              G Offline
              Gourmand
              wrote on last edited by
              #6

              bq. You maybe using its readyRead and finished signals already?

              no, just only finished()

              bq. stop() is equivalent to unplugging the network cable

              I did not find such a behavior... Why it should turn off Inet connection if it is called for QNetwork Session but not for "connection"?

              1 Reply Last reply
              0
              • S Offline
                S Offline
                shanek
                wrote on last edited by
                #7

                http://qt-project.org/doc/qt-4.7/qnetworksession.html states:
                "Invalidates all open sessions against the network interface and therefore stops the underlying network interface. This function always changes the session's state() flag to Disconnected."

                (emphasis mine)

                1 Reply Last reply
                0
                • G Offline
                  G Offline
                  Gourmand
                  wrote on last edited by
                  #8

                  I read this but understood "underlying network interface" not as "network adapter system interface" but as "Qt network interface lying under QNetworkSession" instead. I cannot believe that application can so easy stop all network connections for entire system. I would expect that system-level functions were described with specific warnings. Member stop() would be described clearly as "This will stop all your network connections on entire system". I do not see it stops any one but open in application.

                  Can anybody else familiar with QNetworkSession class comment 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