Results 1 to 1 of 1

Thread: Downloading many files

  1. #1
    Join Date
    Aug 2012
    Posts
    1
    Qt products
    Qt4
    Platforms
    Windows

    Default Downloading many files

    Hi, I have a problem with downloadig many files using QNetworkAccessManager. I was trying to adjust this code http://doc.qt.nokia.com/4.7-snapshot...admanager.html, but my skill is too low. Could you help me to fix my code?

    downloader.cpp

    Qt Code:
    1. #include "downloader.h"
    2.  
    3. Downloader::Downloader(QStringList list, QObject *parent) :
    4. QObject(parent)
    5. {
    6.  
    7. size = list.size();
    8.  
    9. for(int it=0; it<size; it++) {
    10. QString str = "http://www.merriam-webster.com/dictionary/" + list.at(it);
    11. wordlist.push_back(str);
    12. }
    13.  
    14. append(wordlist);
    15.  
    16. }
    17.  
    18. void Downloader::append(const QUrl &url)
    19. {
    20.  
    21. if(downloadQueue.isEmpty())
    22. QTimer::singleShot(0, this, SLOT(do_Download()));
    23.  
    24. downloadQueue.enqueue(url);
    25.  
    26. }
    27.  
    28. void Downloader::append(const QStringList &urllist)
    29. {
    30. foreach(QString url, urllist)
    31. append(QUrl(url));
    32.  
    33. }
    34.  
    35. void Downloader::do_Download()
    36. {
    37.  
    38.  
    39. QUrl url = downloadQueue.dequeue();
    40. QString filename = "G:/Documents and Settings/Olek/Desktop/downloader" + url.path() + ".txt";
    41.  
    42.  
    43. qDebug() << filename;
    44. filetxt = new QFile(filename);
    45.  
    46. if(!filetxt->open(QIODevice::WriteOnly))
    47. {
    48.  
    49. qDebug() << "Unable to save the file";
    50.  
    51. delete filetxt;
    52. filetxt = NULL;
    53. return;
    54. }
    55.  
    56.  
    57. reply = manager.get(QNetworkRequest(url));
    58.  
    59. connect(reply, SIGNAL(finished()), this, SLOT(downloadFinished()));
    60. connect(reply, SIGNAL(readyRead()), this, SLOT(downloadReadyRead()));
    61.  
    62. qDebug() << "I'm in downloadStarted()";
    63.  
    64. }
    65.  
    66. void Downloader::downloadReadyRead()
    67. {
    68.  
    69. if(file.isEmpty()) file = reply->readAll();
    70.  
    71. if(!file.isEmpty()) {
    72.  
    73. // qDebug() << file;
    74. qDebug() << "I'm in downloadReadyRead()";
    75.  
    76. filetxt->write(reply->readAll());
    77. }
    78.  
    79. }
    80.  
    81. void Downloader::downloadFinished()
    82. {
    83.  
    84. if(reply->error())
    85. {
    86.  
    87. qDebug() << "Download failed";
    88. }
    89.  
    90. // qDebug() << file;
    91.  
    92. if(filetxt)
    93. {
    94. filetxt->flush();
    95. filetxt->close();
    96.  
    97. }
    98.  
    99.  
    100. reply->deleteLater();
    101. reply = NULL;
    102.  
    103.  
    104. qDebug() << "I'm in downloadFinished()";
    105.  
    106. do_Download();
    107.  
    108. }
    To copy to clipboard, switch view to plain text mode 


    downloader.h

    Qt Code:
    1. #ifndef DOWNLOADER_H
    2. #define DOWNLOADER_H
    3.  
    4. #include <QObject>
    5. #include <QtNetwork>
    6. #include <QDebug>
    7. #include <wavdownloader.h>
    8.  
    9. class Downloader : public QObject
    10. {
    11. Q_OBJECT
    12. public:
    13. explicit Downloader(QStringList list, QObject *parent = 0);
    14. void downloadStarted();
    15. QString file;
    16. QFile *filetxt;
    17. QString startName;
    18. //QString from_Wav_Filename;
    19. QStringList wordlist;
    20.  
    21.  
    22. void append(const QUrl &url);
    23. void append(const QStringList &urllist);
    24.  
    25. void doDownload();
    26.  
    27.  
    28. int size;
    29.  
    30. signals:
    31. // void finished();
    32.  
    33. public slots:
    34. void downloadFinished();
    35. void downloadReadyRead();
    36. void do_Download();
    37.  
    38.  
    39. private:
    40. QNetworkAccessManager manager;
    41. QNetworkReply *reply;
    42. QQueue<QUrl> downloadQueue;
    43.  
    44. };
    45.  
    46. #endif // DOWNLOADER_H
    To copy to clipboard, switch view to plain text mode 
    Last edited by ajaskier; 20th August 2012 at 23:12.

Similar Threads

  1. Playing an mp3 while it is downloading
    By bittuthegr8 in forum Newbie
    Replies: 6
    Last Post: 7th March 2011, 11:37
  2. About FTP uploading and downloading
    By prats in forum Qt Programming
    Replies: 2
    Last Post: 1st March 2011, 12:28
  3. Downloading files
    By MTK358 in forum Newbie
    Replies: 3
    Last Post: 23rd June 2010, 01:08
  4. Replies: 1
    Last Post: 17th April 2010, 01:12
  5. Downloading multiple files using QNetworkAccessManager
    By aurorius in forum Qt Programming
    Replies: 3
    Last Post: 7th August 2009, 11:51

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.