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. Is it possible to control download speed using QNetworkAccessManager

Is it possible to control download speed using QNetworkAccessManager

Scheduled Pinned Locked Moved General and Desktop
9 Posts 2 Posters 5.8k 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.
  • A Offline
    A Offline
    adnan
    wrote on last edited by
    #1

    Can we restrict QNetworkACCessManager from consuming whole bandwidth, by restricting the download speed, as we do see such options available with almost every download manager

    1 Reply Last reply
    0
    • L Offline
      L Offline
      lgeyer
      wrote on last edited by
      #2

      What about reducing the "read buffer size":http://doc.qt.nokia.com/4.7-snapshot/qnetworkreply.html#setReadBufferSize and clearing the buffer at the desired download rate?

      1 Reply Last reply
      0
      • A Offline
        A Offline
        adnan
        wrote on last edited by
        #3

        Thanks for the reply!
        Do you mean that if i set the buffer size to suppose 512kB, it won't cause any data loss.

        1 Reply Last reply
        0
        • L Offline
          L Offline
          lgeyer
          wrote on last edited by
          #4

          The download should just stall, which means it stops receiving data after 512 kB and waits for the buffer to be emptied. It then continous receiving the next 512 kB of data once done so.

          I haven't verified this, but if you set your read buffer to for example 512 kB an you empty it once a second (using read()), you should theoretically end up with an download rate of (approx.) 512 kB a second.

          1 Reply Last reply
          0
          • A Offline
            A Offline
            adnan
            wrote on last edited by
            #5

            How can we empty the buffer exactly after one second, as it is done through signal/slot mechanism?

            1 Reply Last reply
            0
            • L Offline
              L Offline
              lgeyer
              wrote on last edited by
              #6

              Use a QTimer and read() once a second.

              1 Reply Last reply
              0
              • A Offline
                A Offline
                adnan
                wrote on last edited by
                #7

                When readyRead() signal is emitted then the following slot is invoked
                @void Download::saveToDisk()
                {

                qint64 bytes;
                bytes=replySegment->bytesAvailable();
                fileSegment.write(replySegment->read(bytes));
                

                }@

                How, can i control it using QTimer, it is completely determined by the readyRead() signal. Can you plz explain it more

                1 Reply Last reply
                0
                • L Offline
                  L Offline
                  lgeyer
                  wrote on last edited by
                  #8

                  No, it isn't. ;-)
                  Just ignore the readyRead() signal and use a QTimer, which periodically empties the read buffer.
                  @
                  class Downloader : public QObject
                  {
                  Q_OBJECT

                  public:
                  Downloader(QObject *object) : QObject(parent)
                  {
                  timer.setInterval(1000);
                  connect(&timer, SIGNAL(timeout()), this, SLOT(processData());
                  }

                  bool download(const QUrl &url, const QString &fileName, downloadRate = -1)
                  {
                      file.setFileName(fileName);
                      if (file.open(QIODevice::WriteOnly | QIODevice::Truncate) == false)
                          return false;
                  
                      reply = manager.get(QNetworkRequest(url));
                      if (downloadRate > 0)
                          reply->setReadBufferSize(downloadRate);
                  
                      timer.start();
                  
                      return true;
                  }
                  

                  signals:
                  void finished();

                  private:
                  QNetworkAccessManager manager;
                  QNetworkReply *reply;
                  QTimer timer;

                  private slots:
                  void processData()
                  {
                  file.write(reply->read(reply->bytesAvailable()));

                      if (reply->isFinished() == true)
                      {
                          timer.stop();
                  
                          file.close();
                  
                          reply->deleteLater();
                          reply = nullptr;
                  
                          emit finished();
                      }
                  }
                  

                  @
                  Brain to terminal. Not tested. Exemplary.

                  1 Reply Last reply
                  0
                  • A Offline
                    A Offline
                    adnan
                    wrote on last edited by
                    #9

                    Thanks a lot for such a detailed answer!

                    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