title: Problems with file download
body: <p>I have this QT script using webkit. I can download files no problem but I can't get the progress bar moving in the file dialog. I think the network reply has already been sent before I call the progress dialog as there is a delay from clicking the download link and then then <code>qDebug() &lt;&lt; "Left click - download!";</code>  being echo's out into console. How can I intercept the netwrok reply before it has  finished and the unsupportedContent() method is called?</p>
  
  <p><strong>EDIT:</strong>
  I could strip it out and use <code>reply = manager.get(QNetworkRequest(url));</code> but I don't actually know the URL it could be any link the user clicks, there is no predefined URL?</p>
  
  <pre><code>void MainWindow::unsupportedContent(QNetworkReply *reply) {
  
      qDebug() &lt;&lt; "Left click - download!";
      qDebug() &lt;&lt; "Bytes to download: " &lt;&lt; reply-&gt;bytesAvailable();
  
      QString str = reply-&gt;rawHeader("Content-Disposition");
  
      QString end = str.mid(21);
      end.chop(1);
  
      qDebug() &lt;&lt; "File name: " &lt;&lt; end;
      qDebug() &lt;&lt; "File type: " &lt;&lt; reply-&gt;rawHeader("Content-Type");
      qDebug() &lt;&lt; "File size (bytes): " &lt;&lt; reply-&gt;bytesAvailable();
      QString defaultFileName = QFileInfo(end).fileName();
      QString fileName = QFileDialog::getSaveFileName(this, tr("Save File"), defaultFileName);
      if (fileName.isEmpty()) return;
  
      file = new QFile(fileName);
      if(!file-&gt;open(QIODevice::WriteOnly))
      {
          QMessageBox::information(this, "Downloader",
              tr("Unable to save the file %1: %2.")
              .arg(fileName).arg(file-&gt;errorString()));
          delete file;
          file = NULL;
          return;
      }
  
      downloadRequestAborted = false;
  
      connect(reply, SIGNAL(finished()), this, SLOT(downloadFinished()));
          connect(reply, SIGNAL(readyRead()), this, SLOT(downloadReadyRead()));
          connect(reply, SIGNAL(downloadProgress(qint64,qint64)), this, SLOT(downloadProgress(qint64,qint64)));
          connect(progressDialog, SIGNAL(canceled()), this, SLOT(cancelDownload()));
          progressDialog-&gt;setLabelText(tr("Downloading %1...").arg(fileName));
          //downloadButton-&gt;setEnabled(false);
          progressDialog-&gt;exec();
  
  
      //QFile file(fileName);
      //file.open( QIODevice::WriteOnly );
      //file.write(reply-&gt;read(reply-&gt;bytesAvailable()));
      //file.close();
  }
  
  void MainWindow::downloadProgress(qint64 bytesReceived, qint64 bytesTotal)
  {
      qDebug() &lt;&lt; bytesReceived &lt;&lt; bytesTotal;
      if(downloadRequestAborted)
          return;
      progressDialog-&gt;setMaximum(bytesTotal);
      progressDialog-&gt;setValue(bytesReceived);
  }
  
  void MainWindow::downloadReadyRead()
  {
      if(file)
          file-&gt;write(reply-&gt;read(reply-&gt;bytesAvailable()));
  }
  
  void MainWindow::downloadFinished()
  {
      qDebug() &lt;&lt; "Download finished!";
      if(downloadRequestAborted)
      {
          if(file)
          {
              file-&gt;close();
              file-&gt;remove();
              delete file;
              file = NULL;
          }
          reply-&gt;deleteLater();
          progressDialog-&gt;hide();
          //downloadButton-&gt;setEnabled(true);
          return;
      }
  
      downloadReadyRead();
      progressDialog-&gt;hide();
      //downloadButton-&gt;setEnabled(true);
      file-&gt;flush();
      file-&gt;close();
  
      if(reply-&gt;error())
      {
          //Download failed
          QMessageBox::information(this, "Download failed", tr("Failed: %1").arg(reply-&gt;errorString()));
      }
  
      reply-&gt;deleteLater();
      reply = NULL;
      delete file;
      file = NULL;
  }
  
  void MainWindow::cancelDownload()
  {
      downloadRequestAborted = true;
      reply-&gt;abort();
      progressDialog-&gt;hide();
      //downloadButton-&gt;setEnabled(true);
  }
  </code></pre>