PDA

View Full Version : Downloading multiple files using QNetworkAccessManager



aurorius
6th August 2009, 13:20
Hi..

I got these errors when I tried to use QObject in my class



moc_Download.cpp:42: error: `staticMetaObject' is not a member of `QRunnable'
moc_Download.cpp:58: error: `qt_metacast' is not a member of `QRunnable'
moc_Download.cpp:63: error: `qt_metacall' is not a member of `QRunnable'




#ifndef DOWNLOAD_H
#define DOWNLOAD_H

#include <QtCore>
#include <QtGui>
#include <QtNetwork>

class Download : public QRunnable, public QObject
{

Q_OBJECT

public:
Download(QString url, int size);
void run();

void updateDataReadProgress(int bytesRead, int totalBytes);

QLabel *label;
QProgressBar *progressBar;

QFile *file;
QHttp *http;

private:
QString url;
int size;

QNetworkAccessManager *manager;

bool httpRequestAborted;
int httpGetId;

private slots:
void replyFinished(QNetworkReply* reply);
};

#endif // DOWNLOAD_H




#include "Download.h"

Download::Download(QString url, int size) : QRunnable()
{
this->url = url;
this->size = size;

manager = new QNetworkAccessManager(new QObject);
connect(manager, SIGNAL(finished(QNetworkReply*)), this, SLOT(replyFinished(QNetworkReply*)));
}


void Download::run()
{
qDebug() << size;
progressBar->setValue(size*2);
}

void Download::updateDataReadProgress(int bytesRead, int totalBytes)
{
if (httpRequestAborted)
{
return;
}

progressBar->setMaximum(totalBytes);
progressBar->setValue(bytesRead);
}

void Download::replyFinished(QNetworkReply *reply)
{
qDebug() << "I'm in the reply";
}




#include "MainWindow.h"
#include "Download.h"

MainWindow::MainWindow(QWidget *parent) : QDialog(parent)
{
setupUi(this);

QVector<QString> files;

files.append("http://www.google.com");
files.append("http://www.google.com");
files.append("http://www.google.com");
files.append("http://www.google.com");

int size = files.size();

QProgressBar *progressBar[size];
QLabel *label[size];

for (int i=0; i<size; i++)
{
progressBar[i] = new QProgressBar(scrollAreaWidgetContents);
progressBar[i]->setTextVisible(false);

label[i] = new QLabel(scrollAreaWidgetContents);

layoutScroll->addWidget(label[i]);
layoutScroll->addWidget(progressBar[i]);
}

QList<Download *> runners;

for (int i=0; i<size; i++)
{
Download *d = new Download("a", i);

d->label = label[i];
d->label->setText(tr("Fail: %1").arg(i));

d->progressBar = progressBar[i];

d->setAutoDelete(false);

QThreadPool::globalInstance()->start(d);
runners << d;
}
}


If you guys need anymore information, I'll post more, just let me know, thanks in advance

yogeshgokul
6th August 2009, 13:34
class Download : public QRunnable, public QObject


Change this line as :


class Download : public QObject, public QRunnable

aurorius
6th August 2009, 15:20
^

Thanks, that changes clear up the errors. So, can I assume QObject must be the first one in the parents list ?

However, I got another error regarding QThread. Maybe I should explain what I'm trying to achieve here first. I'm actually trying to create an updater for my applications, it operates almost the same as Java Webstart which download changed files from the server after the application fetches its XML configuration from the server.

My latest codes look like this:



#ifndef DOWNLOAD_H
#define DOWNLOAD_H

#include <QtCore>
#include <QtGui>
#include <QtNetwork>

class Download : public QObject, public QRunnable
{
Q_OBJECT

public:
Download(QString url, int size);
void run();

QLabel *label;
QProgressBar *progressBar;

QFile *file;
QHttp *http;

private:
QString url;
int size;

QNetworkAccessManager *manager;

bool httpRequestAborted;
int httpGetId;

private slots:
void replyFinished(QNetworkReply* reply);
void httpRequestFinished(int requestId, bool error);
void updateDataReadProgress(qint64 bytesRead, qint64 totalBytes);
void downloadFinished();
};

#endif // DOWNLOAD_H




#include "Download.h"

Download::Download(QString url, int size) : QRunnable()
{
this->url = url;
this->size = size;
}

void Download::run()
{
qDebug() << "File: " << url;

manager = new QNetworkAccessManager(this);
}

void Download::updateDataReadProgress(qint64 bytesRead, qint64 totalBytes)
{
if (httpRequestAborted) {
return;
}

progressBar->setMaximum(totalBytes);
progressBar->setValue(bytesRead);
}

void Download::replyFinished(QNetworkReply *reply)
{
qDebug() << "I'm in the replyFinished";
}

void Download::httpRequestFinished(int requestId, bool error)
{
qDebug() << "I'm in the httpRequestFinished";
}

void Download::downloadFinished()
{
qDebug() << "I'm in Download Finished";
}




#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include "ui_MainWindow.h"

class MainWindow : public QDialog, private Ui::MainWindow {

Q_OBJECT

public:
MainWindow(QWidget *parent = 0);

};

#endif // MAINWINDOW_H




#include "MainWindow.h"
#include "Download.h"

MainWindow::MainWindow(QWidget *parent) : QDialog(parent)
{
setupUi(this);

QVector<QString> files;

files.append("http://localhost/f1.exe");
files.append("http://localhost/f2.exe");
files.append("http://localhost/f3.exe");
files.append("http://localhost/f4.exe");

int size = files.size();

QProgressBar *progressBar[size];
QLabel *label[size];

for (int i=0; i<size; i++)
{
progressBar[i] = new QProgressBar(scrollAreaWidgetContents);
progressBar[i]->setTextVisible(false);

label[i] = new QLabel(scrollAreaWidgetContents);

layoutScroll->addWidget(label[i]);
layoutScroll->addWidget(progressBar[i]);
}

QList<Download *> runners;

for (int i=0; i<size; i++)
{
Download *d = new Download(files.at(i), i);

d->label = label[i];
d->label->setText(tr("Fail: %1").arg(files.at(i)));

d->progressBar = progressBar[i];

d->setAutoDelete(false);

QThreadPool::globalInstance()->start(d);
runners << d;
}
}




#include <QtGui>

#include "MainWindow.h"

int main(int argc, char *argv[])
{
QApplication a(argc, argv);
MainWindow w;
w.show();
return a.exec();
}


I'm trying to queue the download using QRunnable, but right now, I'm getting these errors



File: "http://localhost/f1.exe"
QObject: Cannot create children for a parent that is in a different thread.
(Parent is Download(0x1db2bc8), parent's thread is QThread(0x616c78), current thread is QThread(0x1db4308)
File: "http://localhost/f3.exe"
QObject: Cannot create children for a parent that is in a different thread.
(Parent is Download(0x1db5500), parent's thread is QThread(0x616c78), current thread is QThread(0x1db4308)
File: "http://localhost/f4.exe"
QObject: Cannot create children for a parent that is in a different thread.
(Parent is Download(0x1db55f0), parent's thread is QThread(0x616c78), current thread is QThread(0x1db4308)
File: "http://localhost/f2.exe"
QObject: Cannot create children for a parent that is in a different thread.
(Parent is Download(0x1db5690), parent's thread is QThread(0x616c78), current thread is QThread(0x1db4338)


Anyone knows why ?

numbat
7th August 2009, 10:51
manager = new QNetworkAccessManager(this);

'this' is the Download class which is created and owned by thread 1. Manager is owned and created by thread 2. You can not have a parent and a child with different threads. Just create manager without a parent and delete it yourself when you are finished with it.

Other notes: You haven't included the code that connects signals to your slots. You also haven't actually called manager.get. You should be aware that Download's slots will all be called in thread 1 (the main thread) as that is the owner of the Download class. The only function that will run in the new thread is run. You can call moveToThread(this) in the run method to change this, but I don't know if this is recommended.