Download Data from URL: Difference between revisions

From Qt Wiki
Jump to navigation Jump to search
No edit summary
m (→‎filedownloader.h: pretty print)
 
(11 intermediate revisions by 7 users not shown)
Line 1: Line 1:
[[Category:snippets]]<br />[[Category:HowTo]]
{{LangSwitch}}
[[Category:HowTo]]
== Introduction ==


[toc align_right=&quot;yes&amp;quot; depth=&quot;2&amp;quot;]
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 &quot;QByteArray&amp;quot;:http://doc.qt.io/qt-5.0/qtcore/qbytearray.html 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 &quot;QPixmap&amp;quot;:http://doc.qt.io/qt-5.0/qtgui/qpixmap.html or &quot;QImage&amp;quot;:http://doc.qt.io/qt-5.0/qtgui/qimage.html using method &quot;loadFromData()&quot;:http://doc.qt.io/qt-5.0/qtgui/qimage.html#loadFromData-2
 
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 ==


* &quot;QNetworkAccessManager&amp;quot;:http://doc.qt.io/qt-5.0/qtnetwork/qnetworkaccessmanager.html
* {{DocLink|QNetworkAccessManager}}
* &quot;QNetworkRequest&amp;quot;:http://doc.qt.io/qt-5.0/qtnetwork/qnetworkrequest.html
* {{DocLink|QNetworkRequest}}
* &quot;QNetworkReply&amp;quot;:http://doc.qt.io/qt-5.0/qtnetwork/qnetworkreply.html
* {{DocLink|QNetworkReply}}
* &quot;QUrl&amp;quot;:http://doc.qt.io/qt-5.0/qtcore/qurl.html
* {{DocLink|QUrl}}


== .pro File ==
== .pro File ==


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


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


<code><br />#ifndef FILEDOWNLOADER_H<br />#define 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;
};


#include &lt;QObject&amp;gt;<br />#include &lt;QByteArray&amp;gt;<br />#include &lt;QNetworkAccessManager&amp;gt;<br />#include &lt;QNetworkRequest&amp;gt;<br />#include &lt;QNetworkReply&amp;gt;
#endif // FILEDOWNLOADER_H
 
class FileDownloader : public QObject<br />{<br /> Q_OBJECT<br />public:<br /> explicit FileDownloader(QUrl imageUrl, QObject '''parent = 0);
<br /> virtual ~FileDownloader();
<br /> QByteArray downloadedData() const;
<br />signals:<br /> void downloaded();
<br />private slots:
<br /> void fileDownloaded(QNetworkReply''' pReply);
 
private:
 
QNetworkAccessManager m_WebCtrl;
 
QByteArray m_DownloadedData;
 
};
 
#endif // FILEDOWNLOADER_H<br /></code>


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


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


* Declare slot
== Usage ==


<code><br />private slots:
=== Load Pixmap from URL ===


void loadImage();<br /></code>
Declare slot
<syntaxhighlight lang="cpp">


* Connect signal '''downloaded()''' to the slot
private slots:
  void loadImage();
</syntaxhighlight>


<code><br />QUrl imageUrl(&quot;http://qt.digia.com/Documents/1/QtLogo.png&amp;quot;);<br />m_pImgCtrl = new FileDownloader(imageUrl, this);
Connect signal '''downloaded()''' to the slot
<syntaxhighlight lang="cpp">


connect(m_pImgCtrl, SIGNAL (downloaded()), SLOT (loadImage()));<br /></code>
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>


* Load QPixmap from the downloaded data
Load QPixmap from the downloaded data
<syntaxhighlight lang="cpp">


<code><br />void MainWindow::loadImage()<br />{<br /> QPixmap buttonImage;<br /> buttonImage.loadFromData(m_pImgCtrl-&gt;downloadedData());<br />}<br /></code>
void MainWindow::loadImage()
{
  QPixmap buttonImage;
  buttonImage.loadFromData(m_pImgCtrl->downloadedData());
}
</syntaxhighlight>

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