Results 1 to 4 of 4

Thread: Downloading multiple files using QNetworkAccessManager

  1. #1
    Join Date
    May 2009
    Posts
    3
    Thanks
    2
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Downloading multiple files using QNetworkAccessManager

    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'
    Qt Code:
    1. #ifndef DOWNLOAD_H
    2. #define DOWNLOAD_H
    3.  
    4. #include <QtCore>
    5. #include <QtGui>
    6. #include <QtNetwork>
    7.  
    8. class Download : public QRunnable, public QObject
    9. {
    10.  
    11. Q_OBJECT
    12.  
    13. public:
    14. Download(QString url, int size);
    15. void run();
    16.  
    17. void updateDataReadProgress(int bytesRead, int totalBytes);
    18.  
    19. QLabel *label;
    20. QProgressBar *progressBar;
    21.  
    22. QFile *file;
    23. QHttp *http;
    24.  
    25. private:
    26. QString url;
    27. int size;
    28.  
    29. QNetworkAccessManager *manager;
    30.  
    31. bool httpRequestAborted;
    32. int httpGetId;
    33.  
    34. private slots:
    35. void replyFinished(QNetworkReply* reply);
    36. };
    37.  
    38. #endif // DOWNLOAD_H
    To copy to clipboard, switch view to plain text mode 

    Qt Code:
    1. #include "Download.h"
    2.  
    3. Download::Download(QString url, int size) : QRunnable()
    4. {
    5. this->url = url;
    6. this->size = size;
    7.  
    8. manager = new QNetworkAccessManager(new QObject);
    9. connect(manager, SIGNAL(finished(QNetworkReply*)), this, SLOT(replyFinished(QNetworkReply*)));
    10. }
    11.  
    12.  
    13. void Download::run()
    14. {
    15. qDebug() << size;
    16. progressBar->setValue(size*2);
    17. }
    18.  
    19. void Download::updateDataReadProgress(int bytesRead, int totalBytes)
    20. {
    21. if (httpRequestAborted)
    22. {
    23. return;
    24. }
    25.  
    26. progressBar->setMaximum(totalBytes);
    27. progressBar->setValue(bytesRead);
    28. }
    29.  
    30. void Download::replyFinished(QNetworkReply *reply)
    31. {
    32. qDebug() << "I'm in the reply";
    33. }
    To copy to clipboard, switch view to plain text mode 

    Qt Code:
    1. #include "MainWindow.h"
    2. #include "Download.h"
    3.  
    4. MainWindow::MainWindow(QWidget *parent) : QDialog(parent)
    5. {
    6. setupUi(this);
    7.  
    8. QVector<QString> files;
    9.  
    10. files.append("http://www.google.com");
    11. files.append("http://www.google.com");
    12. files.append("http://www.google.com");
    13. files.append("http://www.google.com");
    14.  
    15. int size = files.size();
    16.  
    17. QProgressBar *progressBar[size];
    18. QLabel *label[size];
    19.  
    20. for (int i=0; i<size; i++)
    21. {
    22. progressBar[i] = new QProgressBar(scrollAreaWidgetContents);
    23. progressBar[i]->setTextVisible(false);
    24.  
    25. label[i] = new QLabel(scrollAreaWidgetContents);
    26.  
    27. layoutScroll->addWidget(label[i]);
    28. layoutScroll->addWidget(progressBar[i]);
    29. }
    30.  
    31. QList<Download *> runners;
    32.  
    33. for (int i=0; i<size; i++)
    34. {
    35. Download *d = new Download("a", i);
    36.  
    37. d->label = label[i];
    38. d->label->setText(tr("Fail: %1").arg(i));
    39.  
    40. d->progressBar = progressBar[i];
    41.  
    42. d->setAutoDelete(false);
    43.  
    44. QThreadPool::globalInstance()->start(d);
    45. runners << d;
    46. }
    47. }
    To copy to clipboard, switch view to plain text mode 

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

  2. #2
    Join Date
    Dec 2007
    Posts
    628
    Thanks
    3
    Thanked 89 Times in 87 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Downloading multiple files using QNetworkAccessManager

    Quote Originally Posted by aurorius View Post
    Qt Code:
    1. class Download : public QRunnable, public QObject
    To copy to clipboard, switch view to plain text mode 
    Change this line as :
    Qt Code:
    1. class Download : public QObject, public QRunnable
    To copy to clipboard, switch view to plain text mode 

  3. The following user says thank you to yogeshgokul for this useful post:

    aurorius (6th August 2009)

  4. #3
    Join Date
    May 2009
    Posts
    3
    Thanks
    2
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Downloading multiple files using QNetworkAccessManager

    ^

    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:

    Qt Code:
    1. #ifndef DOWNLOAD_H
    2. #define DOWNLOAD_H
    3.  
    4. #include <QtCore>
    5. #include <QtGui>
    6. #include <QtNetwork>
    7.  
    8. class Download : public QObject, public QRunnable
    9. {
    10. Q_OBJECT
    11.  
    12. public:
    13. Download(QString url, int size);
    14. void run();
    15.  
    16. QLabel *label;
    17. QProgressBar *progressBar;
    18.  
    19. QFile *file;
    20. QHttp *http;
    21.  
    22. private:
    23. QString url;
    24. int size;
    25.  
    26. QNetworkAccessManager *manager;
    27.  
    28. bool httpRequestAborted;
    29. int httpGetId;
    30.  
    31. private slots:
    32. void replyFinished(QNetworkReply* reply);
    33. void httpRequestFinished(int requestId, bool error);
    34. void updateDataReadProgress(qint64 bytesRead, qint64 totalBytes);
    35. void downloadFinished();
    36. };
    37.  
    38. #endif // DOWNLOAD_H
    To copy to clipboard, switch view to plain text mode 

    Qt Code:
    1. #include "Download.h"
    2.  
    3. Download::Download(QString url, int size) : QRunnable()
    4. {
    5. this->url = url;
    6. this->size = size;
    7. }
    8.  
    9. void Download::run()
    10. {
    11. qDebug() << "File: " << url;
    12.  
    13. manager = new QNetworkAccessManager(this);
    14. }
    15.  
    16. void Download::updateDataReadProgress(qint64 bytesRead, qint64 totalBytes)
    17. {
    18. if (httpRequestAborted) {
    19. return;
    20. }
    21.  
    22. progressBar->setMaximum(totalBytes);
    23. progressBar->setValue(bytesRead);
    24. }
    25.  
    26. void Download::replyFinished(QNetworkReply *reply)
    27. {
    28. qDebug() << "I'm in the replyFinished";
    29. }
    30.  
    31. void Download::httpRequestFinished(int requestId, bool error)
    32. {
    33. qDebug() << "I'm in the httpRequestFinished";
    34. }
    35.  
    36. void Download::downloadFinished()
    37. {
    38. qDebug() << "I'm in Download Finished";
    39. }
    To copy to clipboard, switch view to plain text mode 

    Qt Code:
    1. #ifndef MAINWINDOW_H
    2. #define MAINWINDOW_H
    3.  
    4. #include "ui_MainWindow.h"
    5.  
    6. class MainWindow : public QDialog, private Ui::MainWindow {
    7.  
    8. Q_OBJECT
    9.  
    10. public:
    11. MainWindow(QWidget *parent = 0);
    12.  
    13. };
    14.  
    15. #endif // MAINWINDOW_H
    To copy to clipboard, switch view to plain text mode 

    Qt Code:
    1. #include "MainWindow.h"
    2. #include "Download.h"
    3.  
    4. MainWindow::MainWindow(QWidget *parent) : QDialog(parent)
    5. {
    6. setupUi(this);
    7.  
    8. QVector<QString> files;
    9.  
    10. files.append("http://localhost/f1.exe");
    11. files.append("http://localhost/f2.exe");
    12. files.append("http://localhost/f3.exe");
    13. files.append("http://localhost/f4.exe");
    14.  
    15. int size = files.size();
    16.  
    17. QProgressBar *progressBar[size];
    18. QLabel *label[size];
    19.  
    20. for (int i=0; i<size; i++)
    21. {
    22. progressBar[i] = new QProgressBar(scrollAreaWidgetContents);
    23. progressBar[i]->setTextVisible(false);
    24.  
    25. label[i] = new QLabel(scrollAreaWidgetContents);
    26.  
    27. layoutScroll->addWidget(label[i]);
    28. layoutScroll->addWidget(progressBar[i]);
    29. }
    30.  
    31. QList<Download *> runners;
    32.  
    33. for (int i=0; i<size; i++)
    34. {
    35. Download *d = new Download(files.at(i), i);
    36.  
    37. d->label = label[i];
    38. d->label->setText(tr("Fail: %1").arg(files.at(i)));
    39.  
    40. d->progressBar = progressBar[i];
    41.  
    42. d->setAutoDelete(false);
    43.  
    44. QThreadPool::globalInstance()->start(d);
    45. runners << d;
    46. }
    47. }
    To copy to clipboard, switch view to plain text mode 

    Qt Code:
    1. #include <QtGui>
    2.  
    3. #include "MainWindow.h"
    4.  
    5. int main(int argc, char *argv[])
    6. {
    7. QApplication a(argc, argv);
    8. MainWindow w;
    9. w.show();
    10. return a.exec();
    11. }
    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 ?

  5. #4
    Join Date
    Jul 2009
    Posts
    139
    Thanks
    13
    Thanked 59 Times in 52 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Downloading multiple files using QNetworkAccessManager

    Qt Code:
    1. manager = new QNetworkAccessManager(this);
    To copy to clipboard, switch view to plain text mode 
    '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.

Similar Threads

  1. loging to multiple files using Log4Qt
    By zhongzhu in forum Qt Programming
    Replies: 0
    Last Post: 17th June 2009, 10:07
  2. Open multiple files with one program instance
    By Ginsengelf in forum Qt Programming
    Replies: 6
    Last Post: 16th March 2009, 14:08
  3. Select multiple files from QFileDialog
    By jiveaxe in forum Qt Programming
    Replies: 6
    Last Post: 16th February 2009, 14:57
  4. Multiple project files in a single directory
    By jogeshwarakundi in forum Qt for Embedded and Mobile
    Replies: 5
    Last Post: 17th July 2008, 07:03
  5. Error: Using Multiple files
    By 3nc31 in forum Qt Programming
    Replies: 1
    Last Post: 22nd November 2007, 09:23

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.