1

I'd like to add some QT example code to my simple project. The sample code is here: https://wiki.qt.io/Download_Data_from_URL

It consists of filedownloader.cpp and filedownloader.h -- this code downloads a graphic from a supplied URL.

I've added these files to my project and get a clean compile. I think I understand the code ok (I'm mainly a c coder, not c++) but I don't understand how I can pass the QUrl created by my project to filedownloader.cpp

The "project" is just a simple main.cpp/mainwindow.cpp/mainwindow.ui that offers a button to be pressed. Pressing the button calls the routine below:

void MainWindow::on_pushButton_clicked()
{
// pass to filedownloader to process
QUrl fileloc("http://www.test.com/test.jpg");
}

How do I feed the QUrl fileloc to filedownload.cpp?

1 Answer 1

1

You have to add a new method to FileDownloader, that accepts QUrl and starts the download.

filedownloader.h:

#ifndef FILEDOWNLOADER_H
#define FILEDOWNLOADER_H

#include <QObject>
#include <QByteArray>
#include <QNetworkAccessManager>
#include <QNetworkRequest>
#include <QNetworkReply>

class FileDownloader : public QObject
{
 Q_OBJECT
 public:
  explicit FileDownloader(QUrl imageUrl, QObject *parent = 0);
  virtual ~FileDownloader();
  QByteArray downloadedData() const;

 signals:
  void downloaded();

 public slots:
  void download(QUrl url);   // <------ Here it is

 private slots:
  void fileDownloaded(QNetworkReply* pReply);

 private:
  QNetworkAccessManager m_WebCtrl;
  QByteArray m_DownloadedData;
};

#endif // FILEDOWNLOADER_H

filedownloader.cpp:

#include "filedownloader.h"

FileDownloader::FileDownloader(QObject *parent) :
 QObject(parent)
{
 connect(
  &m_WebCtrl, SIGNAL (finished(QNetworkReply*)),
  this, SLOT (fileDownloaded(QNetworkReply*))
  );
 // <------ Notice, that i've removed downloading code from here
}

FileDownloader::~FileDownloader() { }

void FileDownloader::fileDownloaded(QNetworkReply* pReply) {
 m_DownloadedData = pReply->readAll();
 //emit a signal
 pReply->deleteLater();
 emit downloaded();
}

void FileDownloader::download(QUrl url) { // <------ And its definition
 QNetworkRequest request(url);
 m_WebCtrl.get(request);
}

QByteArray FileDownloader::downloadedData() const {
 return m_DownloadedData;
}

And then your on_pushButton_clicked will look like this:

void MainWindow::on_pushButton_clicked()
{
// pass to filedownloader to process
QUrl fileloc("http://www.test.com/test.jpg");
m_filedownloader.download(fileloc);
}
Sign up to request clarification or add additional context in comments.

1 Comment

The idea of the class seems rather to create one FileDownloader object per download and pass the url as ctor argument. I don’t see any need for your modification. It just adds complexity. E.g., if download() is called twice, it’s impossible to find out which download() call downloaded() refers to and one download replaces the other.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.