I am looking for a way to fire the downloadProgress(qint64,qint64) signal to display download progress before all the file bytes are received and the unsupportedContent() is emitted. I get a pause and then it goes straight to unsupportedContent() but I need to intercept the reply as it's happening.

There is a QWebPage::downloadRequested() signal but that only works on right click -> save link as...

Any suggestions?

Qt Code:
  1. void MainWindow::unsupportedContent(QNetworkReply *reply) {
  2.  
  3. qDebug() << "Left click - download!";
  4. qDebug() << "Bytes to download: " << reply->bytesAvailable();
  5.  
  6. QString str = reply->rawHeader("Content-Disposition");
  7.  
  8. QString end = str.mid(21);
  9. end.chop(1);
  10.  
  11. qDebug() << "File name: " << end;
  12. qDebug() << "File type: " << reply->rawHeader("Content-Type");
  13. qDebug() << "File size (bytes): " << reply->bytesAvailable();
  14. QString defaultFileName = QFileInfo(end).fileName();
  15. QString fileName = QFileDialog::getSaveFileName(this, tr("Save File"), defaultFileName);
  16. if (fileName.isEmpty()) return;
  17.  
  18. file = new QFile(fileName);
  19. if(!file->open(QIODevice::WriteOnly))
  20. {
  21. QMessageBox::information(this, "Downloader",
  22. tr("Unable to save the file %1: %2.")
  23. .arg(fileName).arg(file->errorString()));
  24. delete file;
  25. file = NULL;
  26. return;
  27. }
  28.  
  29. downloadRequestAborted = false;
  30.  
  31. connect(reply, SIGNAL(finished()), this, SLOT(downloadFinished()));
  32. connect(reply, SIGNAL(readyRead()), this, SLOT(downloadReadyRead()));
  33. connect(reply, SIGNAL(downloadProgress(qint64,qint64)), this, SLOT(downloadProgress(qint64,qint64)));
  34. connect(progressDialog, SIGNAL(canceled()), this, SLOT(cancelDownload()));
  35. progressDialog->setLabelText(tr("Downloading %1...").arg(fileName));
  36. //downloadButton->setEnabled(false);
  37. progressDialog->exec();
  38.  
  39.  
  40. //QFile file(fileName);
  41. //file.open( QIODevice::WriteOnly );
  42. //file.write(reply->read(reply->bytesAvailable()));
  43. //file.close();
  44. }
  45.  
  46. void MainWindow::downloadProgress(qint64 bytesReceived, qint64 bytesTotal)
  47. {
  48. qDebug() << bytesReceived << bytesTotal;
  49. if(downloadRequestAborted)
  50. return;
  51. progressDialog->setMaximum(bytesTotal);
  52. progressDialog->setValue(bytesReceived);
  53. }
  54.  
  55. void MainWindow::downloadReadyRead()
  56. {
  57. if(file)
  58. file->write(reply->read(reply->bytesAvailable()));
  59. }
  60.  
  61. void MainWindow::downloadFinished()
  62. {
  63. qDebug() << "Download finished!";
  64. if(downloadRequestAborted)
  65. {
  66. if(file)
  67. {
  68. file->close();
  69. file->remove();
  70. delete file;
  71. file = NULL;
  72. }
  73. reply->deleteLater();
  74. progressDialog->hide();
  75. //downloadButton->setEnabled(true);
  76. return;
  77. }
  78.  
  79. downloadReadyRead();
  80. progressDialog->hide();
  81. //downloadButton->setEnabled(true);
  82. file->flush();
  83. file->close();
  84.  
  85. if(reply->error())
  86. {
  87. //Download failed
  88. QMessageBox::information(this, "Download failed", tr("Failed: %1").arg(reply->errorString()));
  89. }
  90.  
  91. reply->deleteLater();
  92. reply = NULL;
  93. delete file;
  94. file = NULL;
  95. }
  96.  
  97. void MainWindow::cancelDownload()
  98. {
  99. downloadRequestAborted = true;
  100. reply->abort();
  101. progressDialog->hide();
  102. //downloadButton->setEnabled(true);
  103. }
To copy to clipboard, switch view to plain text mode