Hi, I got the error in the title running a class derived from QNetworkAccessManager to download and save files. It works fine except when requesting a large number of file (600~). Here the implementation (quite common):

Qt Code:
  1. FileDownloader::FileDownloader(QObject *parent) :
  2. QNetworkAccessManager(parent)
  3. {
  4. }
  5.  
  6. FileDownloader::~FileDownloader()
  7. {
  8. deleteLater();
  9. }
  10.  
  11. void FileDownloader::setPath(const QString &path)
  12. {
  13. mPath = path;
  14. }
  15.  
  16. void FileDownloader::setFileName(const QString &fileName)
  17. {
  18. mFileName = fileName;
  19. }
  20.  
  21. void FileDownloader::setUrl(const QString &url)
  22. {
  23. mUrl = url;
  24. }
  25.  
  26. void FileDownloader::download()
  27. {
  28. file = new QFile;
  29. file->setFileName(mPath + QDir::separator() + mFileName);
  30.  
  31. if(file->exists())
  32. if(!file->remove()) {
  33. qDebug() << file->errorString();
  34. return;
  35. }
  36.  
  37. file->open(QIODevice::WriteOnly);
  38.  
  39. QNetworkRequest request;
  40. request.setUrl(QUrl(mUrl));
  41. reply = get(request);
  42.  
  43. connect(reply, SIGNAL(downloadProgress(qint64,qint64)), this, SLOT(onDownloadProgress(qint64,qint64)));
  44. connect(this, SIGNAL(finished(QNetworkReply*)), this, SLOT(onFinished(QNetworkReply*)));
  45. connect(reply, SIGNAL(readyRead()), this, SLOT(onReadyRead()));
  46. connect(reply, SIGNAL(finished()), this, SLOT(onReplyFinished()));
  47. }
  48.  
  49.  
  50. void FileDownloader::onFinished(QNetworkReply *reply)
  51. {
  52. switch(reply->error()) {
  53. case QNetworkReply::NoError:
  54. qDebug() << "Downloaded:" << file->fileName();
  55. break;
  56.  
  57. default:
  58. qDebug(reply->errorString().toLatin1());
  59. }
  60.  
  61. if(file->isOpen()) {
  62. file->close();
  63. file->deleteLater();
  64. }
  65. }
  66.  
  67. void FileDownloader::onReadyRead()
  68. {
  69. file->write(reply->readAll());
  70. }
  71.  
  72. void FileDownloader::onReplyFinished()
  73. {
  74. if(file->isOpen()) {
  75. file->close();
  76. file->deleteLater();
  77. }
  78. }
  79.  
  80. void FileDownloader::onDownloadProgress(qint64, qint64)
  81. {
  82. qDebug(QString::number(bytesRead).toLatin1() + " - " + QString::number(bytesTotal).toLatin1());
  83. }
To copy to clipboard, switch view to plain text mode 

I'm calling it this way:

Qt Code:
  1. foreach(QString file, files) {
  2. FileDownloader * downloader = new FileDownloader(this);
  3. downloader->setUrl(/*...*/);
  4. downloader->setPath(/*...*/);
  5. downloader->setFileName(/*...*/);
  6. downloader->download();
  7. }
To copy to clipboard, switch view to plain text mode 

Searching the web I read that a solution should be QRunnable, but I'm using it in the wrong way:
* had subclassed QRunnable:
Qt Code:
  1. class FileDownloader : public QNetworkAccessManager, public QRunnable
To copy to clipboard, switch view to plain text mode 
* renamed FileDownloader::download() in FileDownloader::run()
* modified calling routine:
Qt Code:
  1. foreach(QString file, files) {
  2. FileDownloader * downloader = new FileDownloader(this);
  3. downloader->setUrl(/*...*/);
  4. downloader->setPath(/*...*/);
  5. downloader->setFileName(/*...*/);
  6. QThreadPool::globalInstance()->start(downloader); // <- new version
To copy to clipboard, switch view to plain text mode 

In console I got repetition of the following error:

QObject: Cannot create children for a parent that is in a different thread.
(Parent is FileDownloader(0x13297e0), parent's thread is QThread(0x118c420), current thread is QThread(0x13525d0)
Any help will be very appreciated. Thanks.