Download Data from URL: Difference between revisions
Jump to navigation
Jump to search
No edit summary |
No edit summary |
||
Line 1: | Line 1: | ||
[[Category:snippets]]<br />[[Category:HowTo]] | |||
= | [toc align_right="yes&quot; depth="2&quot;] | ||
The following code snippet demonstrates how to download data as | '''English''' [[Download_Data_from_URL_Bulgarian|Български]] [[Download_Data_from_URL_Korean|한국어]][[Download_Data_from_URL_Persian|فارسی]] | ||
= Download Data from URL = | |||
The following code snippet demonstrates how to download data as "QByteArray&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 "QPixmap&quot;:http://doc.qt.io/qt-5.0/qtgui/qpixmap.html or "QImage&quot;:http://doc.qt.io/qt-5.0/qtgui/qimage.html using method "loadFromData()":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! | 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 == | ||
* "QNetworkAccessManager&quot;:http://doc.qt.io/qt-5.0/qtnetwork/qnetworkaccessmanager.html | |||
* "QNetworkRequest&quot;:http://doc.qt.io/qt-5.0/qtnetwork/qnetworkrequest.html | |||
* "QNetworkReply&quot;:http://doc.qt.io/qt-5.0/qtnetwork/qnetworkreply.html | |||
* "QUrl&quot;:http://doc.qt.io/qt-5.0/qtcore/qurl.html | |||
== .pro File == | |||
<code><br />QT ''= network<br /></code> | |||
<br />If you are targeting Symbian devices remember to add the capability for network services. | |||
<br /><code><br />symbian:TARGET.CAPABILITY''= NetworkServices<br /></code> | |||
== filedownloader.h == | |||
<code><br />#ifndef FILEDOWNLOADER_H<br />#define FILEDOWNLOADER_H | |||
#include <QObject&gt;<br />#include <QByteArray&gt;<br />#include <QNetworkAccessManager&gt;<br />#include <QNetworkRequest&gt;<br />#include <QNetworkReply&gt; | |||
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> | |||
==filedownloader.cpp== | == filedownloader.cpp == | ||
<code><br />#include "filedownloader.h&quot; | |||
==Load Pixmap from | FileDownloader::FileDownloader(QUrl imageUrl, QObject '''parent) :<br /> QObject(parent)<br />{<br /> connect(&m_WebCtrl, SIGNAL (finished(QNetworkReply*)),<br /> SLOT (fileDownloaded(QNetworkReply*))); | ||
<br /> QNetworkRequest request(imageUrl);<br /> m_WebCtrl.get(request);<br />} | |||
<br />FileDownloader::~FileDownloader()<br />{ | |||
<br />} | |||
<br />void FileDownloader::fileDownloaded(QNetworkReply''' pReply)<br />{<br /> m_DownloadedData = pReply->readAll();<br /> //emit a signal<br /> pReply->deleteLater();<br /> emit downloaded();<br />} | |||
QByteArray FileDownloader::downloadedData() const<br />{<br /> return m_DownloadedData;<br />}<br /></code> | |||
= Usage = | |||
== Load Pixmap from URL == | |||
* Declare slot | * Declare slot | ||
<code><br />private slots: | |||
void loadImage();<br /></code> | |||
* Connect signal '''downloaded()''' to the slot | * Connect signal '''downloaded()''' to the slot | ||
<code><br />QUrl imageUrl("http://qt.digia.com/Documents/1/QtLogo.png&quot;);<br />m_pImgCtrl = new FileDownloader(imageUrl, this); | |||
connect(m_pImgCtrl, SIGNAL (downloaded()), SLOT (loadImage()));<br /></code> | |||
* Load QPixmap from the downloaded data | * Load QPixmap from the downloaded data | ||
<code><br />void MainWindow::loadImage()<br />{<br /> QPixmap buttonImage;<br /> buttonImage.loadFromData(m_pImgCtrl->downloadedData());<br />}<br /></code> | |||
Revision as of 14:17, 23 February 2015
[toc align_right="yes" depth="2"]
Download Data from URL
The following code snippet demonstrates how to download data as "QByteArray":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 "QPixmap":http://doc.qt.io/qt-5.0/qtgui/qpixmap.html or "QImage":http://doc.qt.io/qt-5.0/qtgui/qimage.html using method "loadFromData()":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
- "QNetworkAccessManager":http://doc.qt.io/qt-5.0/qtnetwork/qnetworkaccessmanager.html
- "QNetworkRequest":http://doc.qt.io/qt-5.0/qtnetwork/qnetworkrequest.html
- "QNetworkReply":http://doc.qt.io/qt-5.0/qtnetwork/qnetworkreply.html
- "QUrl":http://doc.qt.io/qt-5.0/qtcore/qurl.html
.pro File
<br />QT ''= network<br />
If you are targeting Symbian devices remember to add the capability for network services.
<br />symbian:TARGET.CAPABILITY''= NetworkServices<br />
filedownloader.h
<br />#ifndef FILEDOWNLOADER_H<br />#define FILEDOWNLOADER_H
#include <QObject&gt;<br />#include <QByteArray&gt;<br />#include <QNetworkAccessManager&gt;<br />#include <QNetworkRequest&gt;<br />#include <QNetworkReply&gt;
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 />
filedownloader.cpp
<br />#include "filedownloader.h&quot;
FileDownloader::FileDownloader(QUrl imageUrl, QObject '''parent) :<br /> QObject(parent)<br />{<br /> connect(&m_WebCtrl, SIGNAL (finished(QNetworkReply*)),<br /> SLOT (fileDownloaded(QNetworkReply*)));
<br /> QNetworkRequest request(imageUrl);<br /> m_WebCtrl.get(request);<br />}
<br />FileDownloader::~FileDownloader()<br />{
<br />}
<br />void FileDownloader::fileDownloaded(QNetworkReply''' pReply)<br />{<br /> m_DownloadedData = pReply->readAll();<br /> //emit a signal<br /> pReply->deleteLater();<br /> emit downloaded();<br />}
QByteArray FileDownloader::downloadedData() const<br />{<br /> return m_DownloadedData;<br />}<br />
Usage
Load Pixmap from URL
- Declare slot
<br />private slots:
void loadImage();<br />
- Connect signal downloaded() to the slot
<br />QUrl imageUrl("http://qt.digia.com/Documents/1/QtLogo.png&quot;);<br />m_pImgCtrl = new FileDownloader(imageUrl, this);
connect(m_pImgCtrl, SIGNAL (downloaded()), SLOT (loadImage()));<br />
- Load QPixmap from the downloaded data
<br />void MainWindow::loadImage()<br />{<br /> QPixmap buttonImage;<br /> buttonImage.loadFromData(m_pImgCtrl->downloadedData());<br />}<br />