Download Data from URL: Difference between revisions

From Qt Wiki
Jump to navigation Jump to search
No edit summary
 
(convert {{doclinkanchor}} to the improved {{doclink}})
(10 intermediate revisions by 5 users not shown)
Line 1: Line 1:
'''English''' [[Download Data from URL Bulgarian|Български]] [[Download Data from URL Korean|한국어]][[Download Data from URL Persian|فارسی]]
{{LangSwitch}}
[[Category:HowTo]]
== Introduction ==


=Download Data from <span class="caps">URL</span>=
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}}.


The following code snippet demonstrates how to download data as [http://doc.qt.io/qt-5.0/qtcore/qbytearray.html QByteArray] ''[qt.io]'' from <span class="caps">URL</span>. 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 [http://doc.qt.io/qt-5.0/qtgui/qpixmap.html QPixmap] ''[qt.io]'' or [http://doc.qt.io/qt-5.0/qtgui/qimage.html QImage] ''[qt.io]'' using method [http://doc.qt.io/qt-5.0/qtgui/qimage.html#loadFromData-2 loadFromData()] ''[qt.io]''
Please note that although the name of the class is File''Downloader'' 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==
* {{DocLink|QNetworkAccessManager}}
* {{DocLink|QNetworkRequest}}
* {{DocLink|QNetworkReply}}
* {{DocLink|QUrl}}


* [http://doc.qt.io/qt-5.0/qtnetwork/qnetworkaccessmanager.html QNetworkAccessManager] ''[qt.io]''
== .pro File ==
* [http://doc.qt.io/qt-5.0/qtnetwork/qnetworkrequest.html QNetworkRequest] ''[qt.io]''
* [http://doc.qt.io/qt-5.0/qtnetwork/qnetworkreply.html QNetworkReply] ''[qt.io]''
* [http://doc.qt.io/qt-5.0/qtcore/qurl.html QUrl] ''[qt.io]''


==.pro File==
<code>
QT += network
</code>


If you are targeting Symbian devices remember to add the capability for network services.
== filedownloader.h ==


==filedownloader.h==
<code>
#ifndef FILEDOWNLOADER_H
#define FILEDOWNLOADER_H


==filedownloader.cpp==
#include <QObject>
#include <QByteArray>
#include <QNetworkAccessManager>
#include <QNetworkRequest>
#include <QNetworkReply>


=Usage=
class FileDownloader : public QObject
{
Q_OBJECT
public:
  explicit FileDownloader(QUrl imageUrl, QObject *parent = 0);
  virtual ~FileDownloader();
  QByteArray downloadedData() const;


==Load Pixmap from <span class="caps">URL</span>==
signals:
  void downloaded();


* Declare slot
private slots:
  void fileDownloaded(QNetworkReply* pReply);


* Connect signal '''downloaded()''' to the slot
private:
  QNetworkAccessManager m_WebCtrl;
  QByteArray m_DownloadedData;
};


* Load QPixmap from the downloaded data
#endif // FILEDOWNLOADER_H
</code>


===Categories:===
== filedownloader.cpp ==


* [[:Category:HowTo|HowTo]]
<code>
* [[:Category:snippets|snippets]]
#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;
}
</code>
 
== Usage ==
 
=== Load Pixmap from URL ===
 
Declare slot
 
<code>
private slots:
void loadImage();
</code>
 
Connect signal '''downloaded()''' to the slot
 
<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()));
</code>
 
Load QPixmap from the downloaded data
 
<code>
void MainWindow::loadImage()
{
QPixmap buttonImage;
buttonImage.loadFromData(m_pImgCtrl->downloadedData());
}
</code>

Revision as of 09:29, 23 October 2015

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