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]] | [[Category:snippets]] | ||
[[Category:HowTo]] | |||
[toc align_right= | [toc align_right="yes" depth="2"] | ||
'''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|فارسی]] | ||
Line 7: | Line 8: | ||
= Download Data from URL = | = Download Data from URL = | ||
The following code snippet demonstrates how to download data as | 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! | Please note that although the name of the class is FileDownloader the downloaded data is not saved on the disk as file! | ||
Line 13: | Line 14: | ||
== Important Classes == | == 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 == | == .pro File == | ||
<code> | <code> | ||
QT ''= network | |||
</code> | |||
If you are targeting Symbian devices remember to add the capability for network services. | |||
<code> | |||
symbian:TARGET.CAPABILITY''= NetworkServices | |||
</code> | |||
== filedownloader.h == | == filedownloader.h == | ||
<code>< | <code> | ||
#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: | private: | ||
Line 45: | Line 68: | ||
}; | }; | ||
#endif // FILEDOWNLOADER_H | #endif // FILEDOWNLOADER_H | ||
</code> | |||
== filedownloader.cpp == | == filedownloader.cpp == | ||
<code> | <code> | ||
#include "filedownloader.h" | |||
FileDownloader::FileDownloader(QUrl imageUrl, QObject '''parent) : | |||
QObject(parent) | |||
{ | |||
connect(&m_WebCtrl, SIGNAL (finished(QNetworkReply*)), | |||
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 | QByteArray FileDownloader::downloadedData() const | ||
{ | |||
return m_DownloadedData; | |||
} | |||
</code> | |||
= Usage = | = Usage = | ||
Line 65: | Line 111: | ||
* Declare slot | * Declare slot | ||
<code> | <code> | ||
private slots: | |||
void loadImage(); | void loadImage(); | ||
</code> | |||
* Connect signal '''downloaded()''' to the slot | * Connect signal '''downloaded()''' to the slot | ||
<code> | <code> | ||
QUrl imageUrl("http://qt.digia.com/Documents/1/QtLogo.png"); | |||
m_pImgCtrl = new FileDownloader(imageUrl, this); | |||
connect(m_pImgCtrl, SIGNAL (downloaded()), SLOT (loadImage())); | connect(m_pImgCtrl, SIGNAL (downloaded()), SLOT (loadImage())); | ||
</code> | |||
* Load QPixmap from the downloaded data | * Load QPixmap from the downloaded data | ||
<code> | <code> | ||
void MainWindow::loadImage() | |||
{ | |||
QPixmap buttonImage; | |||
buttonImage.loadFromData(m_pImgCtrl->downloadedData()); | |||
} | |||
</code> |
Revision as of 08:41, 25 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
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*)),
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()), SLOT (loadImage()));
- Load QPixmap from the downloaded data
void MainWindow::loadImage()
{
QPixmap buttonImage;
buttonImage.loadFromData(m_pImgCtrl->downloadedData());
}