Download Data from URL: Difference between revisions

From Qt Wiki
Jump to navigation Jump to search
No edit summary
 
No edit summary
Line 1: Line 1:
'''English''' [[Download Data from URL Bulgarian|Български]] [[Download Data from URL Korean|한국어]][[Download Data from URL Persian|فارسی]]
[[Category:snippets]]<br />[[Category:HowTo]]


=Download Data from <span class="caps">URL</span>=
[toc align_right=&quot;yes&amp;quot; depth=&quot;2&amp;quot;]


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]''
'''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 &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!
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
* &quot;QNetworkRequest&amp;quot;:http://doc.qt.io/qt-5.0/qtnetwork/qnetworkrequest.html
* &quot;QNetworkReply&amp;quot;:http://doc.qt.io/qt-5.0/qtnetwork/qnetworkreply.html
* &quot;QUrl&amp;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 &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;
 
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:


* [http://doc.qt.io/qt-5.0/qtnetwork/qnetworkaccessmanager.html QNetworkAccessManager] ''[qt.io]''
QNetworkAccessManager m_WebCtrl;
* [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==
QByteArray m_DownloadedData;


If you are targeting Symbian devices remember to add the capability for network services.
};


==filedownloader.h==
#endif // FILEDOWNLOADER_H<br /></code>


==filedownloader.cpp==
== filedownloader.cpp ==


=Usage=
<code><br />#include &quot;filedownloader.h&amp;quot;


==Load Pixmap from <span class="caps">URL</span>==
FileDownloader::FileDownloader(QUrl imageUrl, QObject '''parent) :<br /> QObject(parent)<br />{<br /> connect(&amp;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-&gt;readAll();<br /> //emit a signal<br /> pReply-&gt;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(&quot;http://qt.digia.com/Documents/1/QtLogo.png&amp;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


===Categories:===
<code><br />void MainWindow::loadImage()<br />{<br /> QPixmap buttonImage;<br /> buttonImage.loadFromData(m_pImgCtrl-&gt;downloadedData());<br />}<br /></code>
 
* [[:Category:HowTo|HowTo]]
* [[:Category:snippets|snippets]]

Revision as of 14:17, 23 February 2015


[toc align_right="yes&quot; depth="2&quot;]

English Български 한국어فارسی

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!

Important Classes

.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 &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;

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 &quot;filedownloader.h&amp;quot;

FileDownloader::FileDownloader(QUrl imageUrl, QObject '''parent) :<br /> QObject(parent)<br />{<br /> connect(&amp;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-&gt;readAll();<br /> //emit a signal<br /> pReply-&gt;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(&quot;http://qt.digia.com/Documents/1/QtLogo.png&amp;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-&gt;downloadedData());<br />}<br />