Quantcast
Channel: Qt DevNet forums: Qt Quick 1283365070**
Viewing all articles
Browse latest Browse all 4972

Implementing filedownloader to QML

$
0
0
Hi, in your example code you do: void FileDownloader::fileDownloaded(QNetworkReply* pReply) {     qDebug()<<"Download successful!";     m_DownloadedData = pReply->readAll();     QFile file("D:\Test.jpg");     file.open(QIODevice::WriteOnly);     file.write(pReply->readAll());     //emit a signal     pReply->deleteLater();     emit downloaded(); } the line “m_DownloadedData = pReply->readAll();” creates this issue, after this line, the buffer of your network reply is empty, and the data is inside “m_DownloadedData” a few lines lower, you try to save the image by doing: “file.write(pReply->readAll());” but the buffer is already empty… so you shoud do: “file.write( m_DownloadedData );” i should do, void FileDownloader::fileDownloaded(QNetworkReply* pReply) {     qDebug()<<"Download successful!";     QFile file("D:\Test.jpg");         if( !file.open(QIODevice::WriteOnly) )         return;  // file could not be created       file.write(pReply->readAll());     //emit a signal     pReply->deleteLater();     emit downloaded(); } and remove the variable “m_DownloadedData” for your second issue, you also have to include the macro QML_DECLARE_TYPE(CLASSNAME) in your header file, before you can use the qmlRegisterType method. so: //filedownloader.h #ifndef FILEDOWNLOADER_H #define FILEDOWNLOADER_H   #include <QtQml>  // add for QML_DECLARE_TYPE macro !!!!!   #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;   };   QML_DECLARE_TYPE( FileDownloader )    // marco !!   #endif // FILEDOWNLOADER_H

Viewing all articles
Browse latest Browse all 4972

Trending Articles