Qt wiki will be updated on October 12th 2023 starting at 11:30 AM (EEST) and the maintenance will last around 2-3 hours. During the maintenance the site will be unavailable.

Download Data from URL: Difference between revisions

From Qt Wiki
Jump to navigation Jump to search
(Introduced Template DocLink)
(Corrected Layout after Wiki Conversion)
Line 10: Line 10:
== Download Data from URL ==
== Download Data from URL ==


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 [http://doc.qt.io/qt-5/qimage.html#loadFromData 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}}.


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


== Important Classes ==
== Important Classes ==
Line 24: Line 24:


<code>
<code>
QT ''= network
QT += network
</code>
</code>


Line 30: Line 30:


<code>
<code>
symbian:TARGET.CAPABILITY''= NetworkServices
symbian:TARGET.CAPABILITY += NetworkServices
</code>
</code>


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


  virtual ~FileDownloader();
  signals:
  void downloaded();


  QByteArray downloadedData() const;
  private slots:
 
  void fileDownloaded(QNetworkReply* pReply);
signals:
void downloaded();
 
private slots:
 
void fileDownloaded(QNetworkReply''' pReply);
 
private:
 
QNetworkAccessManager m_WebCtrl;
 
QByteArray m_DownloadedData;


private:
  QNetworkAccessManager m_WebCtrl;
  QByteArray m_DownloadedData;
};
};


Line 78: Line 72:
#include "filedownloader.h"
#include "filedownloader.h"


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


  QNetworkRequest request(imageUrl);
  QNetworkRequest request(imageUrl);
Line 88: Line 84:
}
}


FileDownloader::~FileDownloader()
FileDownloader::~FileDownloader() { }
{
 
}


void FileDownloader::fileDownloaded(QNetworkReply''' pReply)
void FileDownloader::fileDownloaded(QNetworkReply* pReply) {
{
  m_DownloadedData = pReply->readAll();
  m_DownloadedData = pReply->readAll();
  //emit a signal
  //emit a signal
Line 101: Line 93:
}
}


QByteArray FileDownloader::downloadedData() const
QByteArray FileDownloader::downloadedData() const {
{
  return m_DownloadedData;
  return m_DownloadedData;
}
}
</code>
</code>


= Usage =
== Usage ==


== Load Pixmap from URL ==
=== Load Pixmap from URL ===


* Declare slot
Declare slot


<code>
<code>
private slots:
private slots:
 
void loadImage();
void loadImage();
</code>
</code>


* Connect signal '''downloaded()''' to the slot
Connect signal '''downloaded()''' to the slot


<code>
<code>
Line 125: Line 115:
m_pImgCtrl = new FileDownloader(imageUrl, this);
m_pImgCtrl = new FileDownloader(imageUrl, this);


connect(m_pImgCtrl, SIGNAL (downloaded()), SLOT (loadImage()));
connect(m_pImgCtrl, SIGNAL (downloaded()), this, SLOT (loadImage()));
</code>
</code>


* Load QPixmap from the downloaded data
Load QPixmap from the downloaded data


<code>
<code>

Revision as of 01:13, 2 March 2015


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

Download Data from URL

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