^
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:
void run();
private:
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
#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
To copy to clipboard, switch view to plain text mode
#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";
}
#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";
}
To copy to clipboard, switch view to plain text mode
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include "ui_MainWindow.h"
class MainWindow
: public QDialog,
private Ui
::MainWindow {
Q_OBJECT
public:
};
#endif // MAINWINDOW_H
#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
To copy to clipboard, switch view to plain text mode
#include "MainWindow.h"
#include "Download.h"
{
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();
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 "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;
}
}
To copy to clipboard, switch view to plain text mode
#include <QtGui>
#include "MainWindow.h"
int main(int argc, char *argv[])
{
MainWindow w;
w.show();
return a.exec();
}
#include <QtGui>
#include "MainWindow.h"
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
MainWindow w;
w.show();
return a.exec();
}
To copy to clipboard, switch view to plain text mode
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 ?
Bookmarks