Hi,
for my software, i need to download a file to my local HDD. I found this example:
Downloading file from URL [qt-project.org]
and tried to use it. The result looks like this:
main.cpp
//main.cpp
#include <QtGui/QGuiApplication>
#include "qtquick2applicationviewer.h"
#include "filedownloader.h"
#include <QQmlEngine>
#include <QtQml>
#include <QtDebug>
#include <QObject>
FileDownloader* n;
QNetworkAccessManager* nam;
QObject* parent;
int main(int argc, char *argv[])
{
QGuiApplication app(argc, argv);
QtQuick2ApplicationViewer viewer;
QUrl imageURL("http://i.imgur.com/Uw7Fk.jpg");
viewer.setMainQmlFile(QStringLiteral("qml/DownloadTest4/main.qml"));
viewer.showExpanded();
n = new FileDownloader(imageURL, parent);
//app.connect(n, SIGNAL(downloaded()),SLOT);
return app.exec();
}
filedownloader.cpp
//filedownloader.cpp
#include "filedownloader.h"
#include <QDebug>
#include <QFile>
FileDownloader::FileDownloader(QUrl imageUrl, QObject *parent) :
QObject(parent)
{
connect(&m_WebCtrl, SIGNAL(finished(QNetworkReply*)),
SLOT(fileDownloaded(QNetworkReply*)));
QNetworkRequest request(imageUrl);
m_WebCtrl.get(request);
qDebug()<<"Request sent!";
}
FileDownloader::~FileDownloader()
{
}
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();
}
QByteArray FileDownloader::downloadedData() const
{
return m_DownloadedData;
}
filedownloader.h
//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
Now i have the problem that i get my two console logs “Request sent!” and “Download successful!” but i can’t find the file that should have been created.
Am i doing anything wrong with QFile?
My second problem is the including of “filedownloader.h” into a QML-file.
I know that I have to register the C++ as a type, like this:
qmlRegisterType<filedownloader>("my.library", 1, 0, "FileDownloader");
but it’s not working.
I hope somebody can help me :)
↧