Download Data from URL/bg: Difference between revisions

From Qt Wiki
Jump to navigation Jump to search
No edit summary
No edit summary
Line 1: Line 1:
[[Category:snippets]]<br />[[Category:HowTo]]
[[Category:snippets]]
[[Category:HowTo]]


[toc align_right="yes" depth="2"]
[toc align_right="yes" depth="2"]
Line 20: Line 21:
== .pro файл ==
== .pro файл ==


<code><br />QT ''= network<br /></code>
<code>
<br />Ако целите съвместимост със Symbian устройства не забравяйте да добавите необходимите разрешения за ползване на мрежови услуги.
QT ''= network
<br /><code><br />symbian:TARGET.CAPABILITY''= NetworkServices<br /></code>
</code>
 
Ако целите съвместимост със Symbian устройства не забравяйте да добавите необходимите разрешения за ползване на мрежови услуги.
 
<code>
symbian:TARGET.CAPABILITY''= NetworkServices
</code>


== filedownloader.h ==
== filedownloader.h ==


<code><br />#ifndef FILEDOWNLOADER_H<br />#define FILEDOWNLOADER_H
<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);


#include <QObject><br />#include <QByteArray><br />#include <QNetworkAccessManager><br />#include <QNetworkRequest><br />#include <QNetworkReply>
virtual ~FileDownloader();


class FileDownloader : public QObject<br />{<br /> Q_OBJECT<br />public:<br /> explicit FileDownloader(QUrl imageUrl, QObject '''parent = 0);
QByteArray downloadedData() const;
<br /> virtual ~FileDownloader();
 
<br /> QByteArray downloadedData() const;
signals:
<br />signals:<br /> void downloaded();
void downloaded();
<br />private slots:
 
<br /> void fileDownloaded(QNetworkReply''' pReply);
private slots:
 
void fileDownloaded(QNetworkReply''' pReply);


private:
private:
Line 45: Line 68:
};
};


#endif // FILEDOWNLOADER_H<br /></code>
#endif // FILEDOWNLOADER_H
</code>


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


<code><br />#include "filedownloader.h"
<code>
#include "filedownloader.h"
 
FileDownloader::FileDownloader(QUrl imageUrl, QObject '''parent) :
QObject(parent)
{
connect(&amp;m_WebCtrl, SIGNAL (finished(QNetworkReply*)),
SLOT (fileDownloaded(QNetworkReply*)));
 
QNetworkRequest request(imageUrl);
m_WebCtrl.get(request);
}
 
FileDownloader::~FileDownloader()
{
 
}


FileDownloader::FileDownloader(QUrl imageUrl, QObject '''parent) :<br /> QObject(parent)<br />{<br /> connect(&amp;m_WebCtrl, SIGNAL (finished(QNetworkReply*)),<br /> SLOT (fileDownloaded(QNetworkReply*)));
void FileDownloader::fileDownloaded(QNetworkReply''' pReply)
<br /> QNetworkRequest request(imageUrl);<br /> m_WebCtrl.get(request);<br />}
{
<br />FileDownloader::~FileDownloader()<br />{
m_DownloadedData = pReply->readAll();
<br />}
//emit a signal
<br />void FileDownloader::fileDownloaded(QNetworkReply''' pReply)<br />{<br /> m_DownloadedData = pReply->readAll();<br /> //emit a signal<br /> pReply->deleteLater();<br /> emit downloaded();<br />}
pReply->deleteLater();
emit downloaded();
}


QByteArray FileDownloader::downloadedData() const<br />{<br /> return m_DownloadedData;<br />}<br /></code>
QByteArray FileDownloader::downloadedData() const
{
return m_DownloadedData;
}
</code>


= Употреба =
= Употреба =
Line 65: Line 111:
* Декларирайте слот
* Декларирайте слот


<code><br />private slots:
<code>
private slots:


void loadImage();<br /></code>
void loadImage();
</code>


* Свържете сигнал '''downloaded()''' към слота
* Свържете сигнал '''downloaded()''' към слота


<code><br />QUrl imageUrl("http://qt.nokia.com/logo.png");<br />m_pImgCtrl = new FileDownloader(imageUrl, this);
<code>
QUrl imageUrl("http://qt.nokia.com/logo.png");
m_pImgCtrl = new FileDownloader(imageUrl, this);


connect(m_pImgCtrl, SIGNAL (downloaded()), SLOT (loadImage()));<br /></code>
connect(m_pImgCtrl, SIGNAL (downloaded()), SLOT (loadImage()));
</code>


* Заредете QPixmap от свалените данни
* Заредете QPixmap от свалените данни


<code><br />void MainWindow::loadImage()<br />{<br /> QPixmap buttonImage;<br /> buttonImage.loadFromData(m_pImgCtrl->downloadedData());<br />}
<code>
void MainWindow::loadImage()
{
QPixmap buttonImage;
buttonImage.loadFromData(m_pImgCtrl->downloadedData());
}

Revision as of 11:49, 25 February 2015


[toc align_right="yes" depth="2"]

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

Сваляне на данни от URL

Приложеният отрязък от код показва как се свалят данни като "QByteArray":http://doc.qt.io/qt-5.0/qtcore/qbytearray.html от URL (адрес на даден ресурс в Интернет или друга мрежа). Свалените данни могат да бъдат записани като файл или да бъдат преобразувани в подходящ обект. Така например, ако е свалена картинка, то тя може да бъде заредена от "QPixmap":http://doc.qt.io/qt-5.0/qtgui/qpixmap.html или "QImage":http://doc.qt.io/qt-5.0/qtgui/qimage.html чрез метода "loadFromData()":http://doc.qt.io/qt-5.0/qtgui/qimage.html#loadFromData-2

Моля, обърнете внимание, че приложения клас FileDownloader независимо от името си не запазва свалените данни на диска като файл!

Важни класове

.pro файл

QT ''= network

Ако целите съвместимост със Symbian устройства не забравяйте да добавите необходимите разрешения за ползване на мрежови услуги.

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(&amp;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;
}

Употреба

Заредете QPixmap от URL

  • Декларирайте слот
private slots:

void loadImage();
  • Свържете сигнал downloaded() към слота
QUrl imageUrl("http://qt.nokia.com/logo.png");
m_pImgCtrl = new FileDownloader(imageUrl, this);

connect(m_pImgCtrl, SIGNAL (downloaded()), SLOT (loadImage()));
  • Заредете QPixmap от свалените данни

void MainWindow::loadImage() {

QPixmap buttonImage;
buttonImage.loadFromData(m_pImgCtrl->downloadedData());

}