Download Data from URL: Difference between revisions

From Qt Wiki
Jump to navigation Jump to search
(Corrected Layout after Wiki Conversion)
No edit summary
Line 1: Line 1:
{{DISPLAYTITLE:Download Data from url}}
[[Category:HowTo]]
[[Category:HowTo]]
[[Category:Snippets]]
[[Category:Snippets]]
Line 8: Line 9:
'''English''' [[Download_Data_from_URL_Bulgarian|Български]] [[Download_Data_from_URL_Korean|한국어]][[Download_Data_from_URL_Persian|فارسی]]
'''English''' [[Download_Data_from_URL_Bulgarian|Български]] [[Download_Data_from_URL_Korean|한국어]][[Download_Data_from_URL_Persian|فارسی]]


== Download Data from URL ==
== Introduction ==


The following code snippet demonstrates how to download data as {{DocLink|QBteArray}} from URL. The downloaded data can be saved as a file or converted to appropriate object. For example if an image is downloaded it can be converted to {{DocLink|QPixmap}} or {{DocLink|QImage}} using method {{DocLinkAnchor|QImage|loadFromData}}.
The following code snippet demonstrates how to download data as {{DocLink|QBteArray}} from URL. The downloaded data can be saved as a file or converted to appropriate object. For example if an image is downloaded it can be converted to {{DocLink|QPixmap}} or {{DocLink|QImage}} using method {{DocLinkAnchor|QImage|loadFromData}}.

Revision as of 21:36, 2 March 2015


English Български 한국어فارسی

Introduction

The following code snippet demonstrates how to download data as QBteArray from URL. The downloaded data can be saved as a file or converted to appropriate object. For example if an image is downloaded it can be converted to QPixmap or QImage using method loadFromData.

Please note that although the name of the class is FileDownloader the downloaded data is not saved on the disk as file!

Important Classes

.pro File

QT += network

If you are targeting Symbian devices remember to add the capability for network services.

symbian:TARGET.CAPABILITY += NetworkServices

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();

 private slots:
  void fileDownloaded(QNetworkReply* pReply);

 private:
  QNetworkAccessManager m_WebCtrl;
  QByteArray m_DownloadedData;
};

#endif // FILEDOWNLOADER_H

filedownloader.cpp

#include "filedownloader.h"

FileDownloader::FileDownloader(QUrl imageUrl, QObject *parent) :
 QObject(parent)
{
 connect(
  &m_WebCtrl, SIGNAL (finished(QNetworkReply*)),
  this, SLOT (fileDownloaded(QNetworkReply*))
  );

 QNetworkRequest request(imageUrl);
 m_WebCtrl.get(request);
}

FileDownloader::~FileDownloader() { }

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

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

Usage

Load Pixmap from URL

Declare slot

private slots:
 void loadImage();

Connect signal downloaded() to the slot

QUrl imageUrl("http://qt.digia.com/Documents/1/QtLogo.png");
m_pImgCtrl = new FileDownloader(imageUrl, this);

connect(m_pImgCtrl, SIGNAL (downloaded()), this, SLOT (loadImage()));

Load QPixmap from the downloaded data

void MainWindow::loadImage()
{
 QPixmap buttonImage;
 buttonImage.loadFromData(m_pImgCtrl->downloadedData());
}