Results 1 to 15 of 15

Thread: Concurrent file downloading

Threaded View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Join Date
    Jun 2011
    Posts
    69
    Thanks
    13
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11

    Question Concurrent file downloading

    Download one file at the time with source code below is easy, and also download multiply file also isn't problem when using http://doc.qt.io/qt-4.8/QQueue, but downloading files in parallel i can't get it!
    Assume we have:
    I make code shorter and remove other codes for demonstration purpose ;)
    downloader.h
    Qt Code:
    1. #ifndef DOWNLOADER_H
    2. #define DOWNLOADER_H
    3.  
    4. #include <QObject>
    5. #include <QFile>
    6. #include <QFileInfo>
    7. #include <QNetworkAccessManager>
    8. #include <QNetworkRequest>
    9. #include <QNetworkReply>
    10. #include <QUrl>
    11.  
    12. class Dowloader : public QObject
    13. {
    14. Q_OBJECT
    15. public:
    16. explicit Dowloader(QObject *parent = 0);
    17.  
    18. public slots:
    19. void theDownload(const QString &urlLink);
    20.  
    21. private slots:
    22. void startRequest(const QUrl &url);
    23. void httpReadyRead();
    24. void httpFinished();
    25.  
    26. private:
    27. QNetworkAccessManager manager;
    28. QNetworkReply *reply;
    29. QFile *file;
    30. };
    31.  
    32. #endif // DOWNLOADER_H
    To copy to clipboard, switch view to plain text mode 
    downloader.cpp
    Qt Code:
    1. #include "dowloader.h"
    2.  
    3. Dowloader::Dowloader(QObject *parent) :
    4. QObject(parent)
    5. {}
    6.  
    7. void Dowloader::theDownload(const QString &urlLink)
    8. {
    9. QUrl url(urlLink);
    10. QFileInfo fInfo(url.path());
    11. QString fileName = fInfo.fileName();
    12. if(fileName.isEmpty())
    13. fileName = "download";
    14.  
    15. file = new QFile(fileName);
    16.  
    17. if(!file->open(QIODevice::WriteOnly)){
    18. delete file;
    19. file = 0;
    20. return;
    21. }
    22. startRequest(url);
    23. }
    24.  
    25. void Dowloader::replyMetaDataChanged()
    26. {}
    27.  
    28. void Dowloader::startRequest(const QUrl &url)
    29. {
    30. QNetworkRequest request(url);
    31. reply = manager.get(request);
    32. // conncet signals
    33. connect(reply, SIGNAL(metaDataChanged()), this, SLOT(replyMetaDataChanged()));
    34. connect(reply, SIGNAL(readyRead()), this, SLOT(httpReadyRead()));
    35. connect(reply, SIGNAL(finished()), this, SLOT(httpFinished()));
    36. }
    37.  
    38. void Dowloader::httpReadyRead()
    39. {
    40. if(file) file->write(reply->readAll());
    41. }
    42.  
    43. void Dowloader::httpFinished()
    44. {
    45. file->flush();
    46. file->close();
    47. reply->deleteLater();
    48. reply = 0;
    49. delete file;
    50. file = 0;
    51. }
    To copy to clipboard, switch view to plain text mode 
    The main problem here is, in the httpReadyRead() ans httpFinished() how can operate on the correct file, reply? as you see in the *.h file they are *pointer, it means when i add new file current QNetworkReply *reply, QFile *file, gonna re-initialize with new values!
    What changes need in this code to make Concurrent file downloading possible? // Doesn't mean provide some code or something, just give me a scenario about it!

    Thanks
    Last edited by Alir3z4; 20th February 2012 at 16:49.
    ...یه مرد هیچوقت زمین نمیخوره

Similar Threads

  1. QWebView page loading fails after file downloading
    By mentalmushroom in forum Qt Programming
    Replies: 1
    Last Post: 18th August 2011, 15:57
  2. downloading the file problems
    By migel in forum Newbie
    Replies: 0
    Last Post: 7th June 2011, 18:30
  3. Playing file with Phonon while stil downloading it.
    By alexandernst in forum Qt Programming
    Replies: 3
    Last Post: 10th April 2011, 12:25
  4. Downloading file over https with proxy
    By szraf in forum Qt Programming
    Replies: 0
    Last Post: 5th April 2011, 17:56
  5. File size of a remote file without downloading it
    By dirkdepauw in forum Qt Programming
    Replies: 5
    Last Post: 4th November 2010, 10:48

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.