Download Data from URL: Difference between revisions
Jump to navigation
Jump to search
m (Improved code formatting) |
m (→filedownloader.h: pretty print) |
||
Line 21: | Line 21: | ||
== filedownloader.h == | == filedownloader.h == | ||
<syntaxhighlight lang="cpp"> | |||
#ifndef FILEDOWNLOADER_H | #ifndef FILEDOWNLOADER_H | ||
Line 48: | Line 49: | ||
QByteArray m_DownloadedData; | QByteArray m_DownloadedData; | ||
}; | }; | ||
#endif // FILEDOWNLOADER_H | #endif // FILEDOWNLOADER_H | ||
</syntaxhighlight> | |||
== filedownloader.cpp == | == filedownloader.cpp == | ||
<syntaxhighlight lang="cpp"> | |||
#include "filedownloader.h" | #include "filedownloader.h" | ||
Line 79: | Line 82: | ||
return m_DownloadedData; | return m_DownloadedData; | ||
} | } | ||
</syntaxhighlight> | |||
== Usage == | == Usage == | ||
Line 85: | Line 89: | ||
Declare slot | Declare slot | ||
<syntaxhighlight lang="cpp"> | |||
private slots: | private slots: | ||
void loadImage(); | void loadImage(); | ||
</syntaxhighlight> | |||
Connect signal '''downloaded()''' to the slot | Connect signal '''downloaded()''' to the slot | ||
<syntaxhighlight lang="cpp"> | |||
QUrl imageUrl("http: //qt.digia.com/Documents/1/QtLogo.png"); | QUrl imageUrl("http: //qt.digia.com/Documents/1/QtLogo.png"); | ||
Line 95: | Line 102: | ||
connect(m_pImgCtrl, SIGNAL (downloaded()), this, SLOT (loadImage())); | connect(m_pImgCtrl, SIGNAL (downloaded()), this, SLOT (loadImage())); | ||
</syntaxhighlight> | |||
Load QPixmap from the downloaded data | Load QPixmap from the downloaded data | ||
<syntaxhighlight lang="cpp"> | |||
void MainWindow::loadImage() | void MainWindow::loadImage() | ||
Line 103: | Line 112: | ||
buttonImage.loadFromData(m_pImgCtrl->downloadedData()); | buttonImage.loadFromData(m_pImgCtrl->downloadedData()); | ||
} | } | ||
</syntaxhighlight> |
Latest revision as of 17:39, 27 April 2024
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());
}