Download Data from URL: Difference between revisions

From Qt Wiki
Jump to navigation Jump to search
(Introduced Template DocLink)
m (→‎filedownloader.h: pretty print)
 
(9 intermediate revisions by 6 users not shown)
Line 1: Line 1:
{{LangSwitch}}
[[Category:HowTo]]
[[Category:HowTo]]
[[Category:Snippets]]
== Introduction ==
[[Category:Tutorial]]
[[Category:Learning]]


<div style="float:left;padding:14px;">__TOC__</div>
The following code snippet demonstrates how to download data as {{DocLink|QByteArray}} 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 {{DocLink|QImage|loadFromData}}.


'''English''' [[Download_Data_from_URL_Bulgarian|Български]] [[Download_Data_from_URL_Korean|한국어]][[Download_Data_from_URL_Persian|فارسی]]
Please note that although the name of the class is File''Downloader'' the downloaded data is not saved on the disk as file!
 
== 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()]
 
Please note that although the name of the class is FileDownloader the downloaded data is not saved on the disk as file!


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


<code>
<code>
QT ''= network
QT += network
</code>
 
If you are targeting Symbian devices remember to add the capability for network services.
 
<code>
symbian:TARGET.CAPABILITY''= NetworkServices
</code>
</code>


== filedownloader.h ==
== filedownloader.h ==
<syntaxhighlight lang="cpp">


<code>
#ifndef FILEDOWNLOADER_H
#ifndef FILEDOWNLOADER_H
#define FILEDOWNLOADER_H
#define FILEDOWNLOADER_H
 
#include <QObject>
#include <QObject>
#include <QByteArray>
#include <QByteArray>
#include <QNetworkAccessManager>
#include <QNetworkAccessManager>
#include <QNetworkRequest>
#include <QNetworkRequest>
#include <QNetworkReply>
#include <QNetworkReply>
 
class FileDownloader : public QObject
class FileDownloader : public QObject
{
{
  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:
QByteArray downloadedData() const;
  void downloaded();
 
signals:
  private slots:
void downloaded();
  void fileDownloaded(QNetworkReply* pReply);
 
  private:
private slots:
  QNetworkAccessManager m_WebCtrl;
 
  QByteArray m_DownloadedData;
void fileDownloaded(QNetworkReply''' pReply);
};
 
private:
 
QNetworkAccessManager m_WebCtrl;
 
QByteArray m_DownloadedData;
 
};


#endif // FILEDOWNLOADER_H
#endif // FILEDOWNLOADER_H
</code>


</syntaxhighlight>
== filedownloader.cpp ==
== filedownloader.cpp ==
<syntaxhighlight lang="cpp">


<code>
#include "filedownloader.h"
#include "filedownloader.h"
 
FileDownloader::FileDownloader(QUrl imageUrl, QObject *parent) :
FileDownloader::FileDownloader(QUrl imageUrl, QObject '''parent) :
  QObject(parent)
QObject(parent)
{
{
  connect(
connect(&amp;m_WebCtrl, SIGNAL (finished(QNetworkReply*)),
  &m_WebCtrl, SIGNAL (finished(QNetworkReply*)),
SLOT (fileDownloaded(QNetworkReply*)));
  this, SLOT (fileDownloaded(QNetworkReply*))
 
  );
QNetworkRequest request(imageUrl);
m_WebCtrl.get(request);
  QNetworkRequest request(imageUrl);
}
  m_WebCtrl.get(request);
 
}
FileDownloader::~FileDownloader()
{
FileDownloader::~FileDownloader() { }
 
}
void FileDownloader::fileDownloaded(QNetworkReply* pReply) {
 
  m_DownloadedData = pReply->readAll();
void FileDownloader::fileDownloaded(QNetworkReply''' pReply)
  //emit a signal
{
  pReply->deleteLater();
m_DownloadedData = pReply->readAll();
  emit downloaded();
//emit a signal
}
pReply->deleteLater();
emit downloaded();
QByteArray FileDownloader::downloadedData() const {
}
  return m_DownloadedData;
 
}
QByteArray FileDownloader::downloadedData() const
</syntaxhighlight>
{
return m_DownloadedData;
}
</code>


= Usage =
== Usage ==


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


* Declare slot
Declare slot
<syntaxhighlight lang="cpp">


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


void loadImage();
Connect signal '''downloaded()''' to the slot
</code>
<syntaxhighlight lang="cpp">


* 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()));
</syntaxhighlight>


<code>
Load QPixmap from the downloaded data
QUrl imageUrl("http://qt.digia.com/Documents/1/QtLogo.png");
<syntaxhighlight lang="cpp">
m_pImgCtrl = new FileDownloader(imageUrl, this);


connect(m_pImgCtrl, SIGNAL (downloaded()), SLOT (loadImage()));
void MainWindow::loadImage()
</code>
{
 
  QPixmap buttonImage;
* Load QPixmap from the downloaded data
  buttonImage.loadFromData(m_pImgCtrl->downloadedData());
 
}
<code>
</syntaxhighlight>
void MainWindow::loadImage()
{
QPixmap buttonImage;
buttonImage.loadFromData(m_pImgCtrl->downloadedData());
}
</code>

Latest revision as of 17:39, 27 April 2024

En Ar Bg De El Es Fa Fi Fr Hi Hu It Ja Kn Ko Ms Nl Pl Pt Ru Sq Th Tr Uk Zh

Introduction

The following code snippet demonstrates how to download data as QByteArray 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

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