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
#include "downloader.h"
{
size = list.size();
for(int it=0; it<size; it++) {
QString str
= "http://www.merriam-webster.com/dictionary/" + list.
at(it
);
wordlist.push_back(str);
}
append(wordlist);
}
void Downloader
::append(const QUrl &url
) {
if(downloadQueue.isEmpty())
QTimer::singleShot(0,
this,
SLOT(do_Download
()));
downloadQueue.enqueue(url);
}
{
}
void Downloader::do_Download()
{
QUrl url
= downloadQueue.
dequeue();
QString filename
= "G:/Documents and Settings/Olek/Desktop/downloader" + url.
path() + ".txt";
qDebug() << filename;
filetxt
= new QFile(filename
);
{
qDebug() << "Unable to save the file";
delete filetxt;
filetxt = NULL;
return;
}
reply = manager.get(QNetworkRequest(url));
connect(reply, SIGNAL(finished()), this, SLOT(downloadFinished()));
connect(reply, SIGNAL(readyRead()), this, SLOT(downloadReadyRead()));
qDebug() << "I'm in downloadStarted()";
}
void Downloader::downloadReadyRead()
{
if(file.isEmpty()) file = reply->readAll();
if(!file.isEmpty()) {
// qDebug() << file;
qDebug() << "I'm in downloadReadyRead()";
filetxt->write(reply->readAll());
}
}
void Downloader::downloadFinished()
{
if(reply->error())
{
qDebug() << "Download failed";
}
// qDebug() << file;
if(filetxt)
{
filetxt->flush();
filetxt->close();
}
reply->deleteLater();
reply = NULL;
qDebug() << "I'm in downloadFinished()";
do_Download();
}
#include "downloader.h"
Downloader::Downloader(QStringList list, QObject *parent) :
QObject(parent)
{
size = list.size();
for(int it=0; it<size; it++) {
QString str = "http://www.merriam-webster.com/dictionary/" + list.at(it);
wordlist.push_back(str);
}
append(wordlist);
}
void Downloader::append(const QUrl &url)
{
if(downloadQueue.isEmpty())
QTimer::singleShot(0, this, SLOT(do_Download()));
downloadQueue.enqueue(url);
}
void Downloader::append(const QStringList &urllist)
{
foreach(QString url, urllist)
append(QUrl(url));
}
void Downloader::do_Download()
{
QUrl url = downloadQueue.dequeue();
QString filename = "G:/Documents and Settings/Olek/Desktop/downloader" + url.path() + ".txt";
qDebug() << filename;
filetxt = new QFile(filename);
if(!filetxt->open(QIODevice::WriteOnly))
{
qDebug() << "Unable to save the file";
delete filetxt;
filetxt = NULL;
return;
}
reply = manager.get(QNetworkRequest(url));
connect(reply, SIGNAL(finished()), this, SLOT(downloadFinished()));
connect(reply, SIGNAL(readyRead()), this, SLOT(downloadReadyRead()));
qDebug() << "I'm in downloadStarted()";
}
void Downloader::downloadReadyRead()
{
if(file.isEmpty()) file = reply->readAll();
if(!file.isEmpty()) {
// qDebug() << file;
qDebug() << "I'm in downloadReadyRead()";
filetxt->write(reply->readAll());
}
}
void Downloader::downloadFinished()
{
if(reply->error())
{
qDebug() << "Download failed";
}
// qDebug() << file;
if(filetxt)
{
filetxt->flush();
filetxt->close();
}
reply->deleteLater();
reply = NULL;
qDebug() << "I'm in downloadFinished()";
do_Download();
}
To copy to clipboard, switch view to plain text mode
downloader.h
#ifndef DOWNLOADER_H
#define DOWNLOADER_H
#include <QObject>
#include <QtNetwork>
#include <QDebug>
#include <wavdownloader.h>
{
Q_OBJECT
public:
void downloadStarted();
//QString from_Wav_Filename;
void append
(const QUrl &url
);
void doDownload();
int size;
signals:
// void finished();
public slots:
void downloadFinished();
void downloadReadyRead();
void do_Download();
private:
QNetworkAccessManager manager;
QNetworkReply *reply;
QQueue<QUrl> downloadQueue;
};
#endif // DOWNLOADER_H
#ifndef DOWNLOADER_H
#define DOWNLOADER_H
#include <QObject>
#include <QtNetwork>
#include <QDebug>
#include <wavdownloader.h>
class Downloader : public QObject
{
Q_OBJECT
public:
explicit Downloader(QStringList list, QObject *parent = 0);
void downloadStarted();
QString file;
QFile *filetxt;
QString startName;
//QString from_Wav_Filename;
QStringList wordlist;
void append(const QUrl &url);
void append(const QStringList &urllist);
void doDownload();
int size;
signals:
// void finished();
public slots:
void downloadFinished();
void downloadReadyRead();
void do_Download();
private:
QNetworkAccessManager manager;
QNetworkReply *reply;
QQueue<QUrl> downloadQueue;
};
#endif // DOWNLOADER_H
To copy to clipboard, switch view to plain text mode
Bookmarks