Download Data from URL/de: Difference between revisions
Jump to navigation
Jump to search
AutoSpider (talk | contribs) (Decode HTML entity names) |
MichiMolle (talk | contribs) (Cleaned up Syntax) |
||
(5 intermediate revisions by 2 users not shown) | |||
Line 1: | Line 1: | ||
[[Category:snippets]] | [[Category:snippets]] | ||
[[Category:HowTo]] | [[Category:HowTo]] | ||
[[Category:German]] | [[Category:German]] | ||
'''German''' [[Download_Data_from_URL|English]] [[Download_Data_from_URL_Bulgarian|Български]] [[Download_Data_from_URL_Korean|한국어]][[Download_Data_from_URL_Persian|فارسی]] | '''German''' [[Download_Data_from_URL|English]] [[Download_Data_from_URL_Bulgarian|Български]] [[Download_Data_from_URL_Korean|한국어]][[Download_Data_from_URL_Persian|فارسی]] | ||
Line 26: | Line 23: | ||
<code> | <code> | ||
QT | QT += network | ||
</code> | </code> | ||
Für Symbian Geräte muss der Netzwerkdienst hinzugefügt werden. | Für Symbian Geräte muss der Netzwerkdienst hinzugefügt werden. | ||
<code> | <code> | ||
symbian:TARGET.CAPABILITY | symbian:TARGET.CAPABILITY+= NetworServices | ||
</code> | </code> | ||
Line 49: | Line 46: | ||
Q_OBJECT | Q_OBJECT | ||
public: | public: | ||
explicit FileDownloader(QUrl imageUrl, QObject | explicit FileDownloader(QUrl imageUrl, QObject *parent = 0); | ||
virtual ~FileDownloader(); | virtual ~FileDownloader(); | ||
Line 60: | Line 57: | ||
private slots: | private slots: | ||
void fileDownloaded(QNetworkReply | void fileDownloaded(QNetworkReply* pReply); | ||
private: | private: | ||
Line 78: | Line 75: | ||
#include "filedownloader.h" | #include "filedownloader.h" | ||
FileDownloader::FileDownloader(QUrl imageUrl, QObject | FileDownloader::FileDownloader(QUrl imageUrl, QObject *parent) : | ||
QObject(parent) | QObject(parent) | ||
{ | { | ||
Line 93: | Line 90: | ||
} | } | ||
void FileDownloader::fileDownloaded(QNetworkReply | void FileDownloader::fileDownloaded(QNetworkReply* pReply) | ||
{ | { | ||
m_DownloadedData = pReply->readAll(); | m_DownloadedData = pReply->readAll(); |
Latest revision as of 17:45, 23 January 2016
German English Български 한국어فارسی
Daten von URL herunterladen
Der folgende Code-Schnippsel demonstriert, wie Daten von einer URL als QByteArray heruntergeladen werden können. Die heruntergeladenen Daten können dann entweder als Datei gespeichert oder in ein passendes Objekt konvertiert werden. Wird beispielsweise ein Bild heruntergeladen, kann es mit Hilfe der loadFromData() Funktion in ein QPixmap oder QImage konvertiert werden.
Beachte, dass obwohl der Name der Klasse FileDownloader ist, the heruntergeladenen Daten nicht als Datei auf der Festplatte gespeichert werden.
Wichtige Klassen
.pro Datei
QT += network
Für Symbian Geräte muss der Netzwerkdienst hinzugefügt werden.
symbian:TARGET.CAPABILITY+= NetworServices
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;
}
Verwendung
Pixmap von URL laden
- Slot deklarieren
private slots:
void loadImage();
- downloaded() Signal mit Slot verbinden
QUrl imageUrl("http://qt.nokia.com/logo.png");
m_pImgCtrl = new FileDownloader(imageUrl, this);
connect(m_pImgCtrl, SIGNAL (downloaded()), SLOT (loadImage()));
- QPixmap von den heruntergeladenen Daten laden
void MainWindow::loadImage()
{
QPixmap buttonImage;
buttonImage.loadFromData(m_pImgCtrl->downloadedData());
}