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. Cancel all requests being made by QNetworkAccessManager
Qt 6.11 is out! See what's new in the release blog

Cancel all requests being made by QNetworkAccessManager

Scheduled Pinned Locked Moved Solved General and Desktop
4 Posts 2 Posters 2.0k Views 2 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
    QList<QUrl> urls = {
        QUrl("http://www.google.com"),
        QUrl("http://www.google.com")
    };
    
    for (const auto &url : urls) {
        auto reply = nam.get(QNetworkRequest(url));
        connect(reply, &QNetworkReply::finished, this, &MainWindow::finished);
    }
    

    Downloader::finished:

    auto reply = qobject_cast<QNetworkReply *>(sender());
    auto content = reply->readAll();
    reply->deleteLater();
    

    I wanted to create a button for the user and when the user clicks on that button it will cancel all the requests being made by QNetworkAccessManager. How can I do that?

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

      I did that way:

      for (const auto &url : urls) {
          auto reply = nam.get(QNetworkRequest(url));
          connect(reply, &QNetworkReply::finished, this, &MainWindow::finished);
          connect(ui->pushButton, &QPushButton::clicked, reply, &QNetworkReply::abort);
      }
      

      and inside the replyFinished:

          auto reply = qobject_cast<QNetworkReply *>(sender());
          reply->deleteLater();
      
          if (! reply->isReadable()) {
              return;
          }
      

      Is that right or am I doing something wrong?

      Paul ColbyP 1 Reply Last reply
      0
      • M Mr Gisa

        I did that way:

        for (const auto &url : urls) {
            auto reply = nam.get(QNetworkRequest(url));
            connect(reply, &QNetworkReply::finished, this, &MainWindow::finished);
            connect(ui->pushButton, &QPushButton::clicked, reply, &QNetworkReply::abort);
        }
        

        and inside the replyFinished:

            auto reply = qobject_cast<QNetworkReply *>(sender());
            reply->deleteLater();
        
            if (! reply->isReadable()) {
                return;
            }
        

        Is that right or am I doing something wrong?

        Paul ColbyP Offline
        Paul ColbyP Offline
        Paul Colby
        wrote on last edited by
        #3

        Hi @Mr-Gisa,

        connect(ui->pushButton, &QPushButton::clicked, reply, &QNetworkReply::abort);

        That should work, although it does create some overhead (no idea how much) by having to connect every network reply to the button (depends on how many concurrent replies you expect), and also means that it will only abort requests that you remembered to connect (in case, for example, you start network requests in multiple places) - this might be desirable, or undesirable depending on your application.

        Another option, which will abort all requests started from that particular network access manager, and only requiring one connection, would be something like this in the MainWindow's initialisation:

            connect(ui->pushButton, &QPushButton::clicked, [&nam]() {
                foreach (auto &reply, nam.findChildren<QNetworkReply *>()) {
                    reply->abort();
                }
            });
        

        Cheers.

        1 Reply Last reply
        2
        • M Offline
          M Offline
          Mr Gisa
          wrote on last edited by Mr Gisa
          #4
          if (! reply->isReadable()) {
                  return;
          }
          

          I put that in the replyFInished cause that is normally where we call reply->readAll() the thing is that when you call abort it will also call the replyFinished anyway, so I had to put that in the top to avoid going forward and trying to read something that doesn't exist. If I don't use that piece of code in the slot it shows me a message:

          QIODevice::read (QNetworkReplyHttpImpl): device not open

          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