qtdownload.cpp 1.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. #include "qtdownload.h"
  2. #include <QCoreApplication>
  3. #include <QUrl>
  4. #include <QtNetwork/QNetworkRequest>
  5. #include <QFile>
  6. #include <QDebug>
  7. QtDownload::QtDownload() : QObject(0) {
  8. QObject::connect(&manager, SIGNAL(finished(QNetworkReply*)),this, SLOT(downloadFinished(QNetworkReply*)));
  9. }
  10. QtDownload::~QtDownload() {
  11. }
  12. void QtDownload::setTarget(const QString &t) {
  13. this->target = t;
  14. }
  15. void QtDownload::downloadFinished(QNetworkReply *data) {
  16. QFile localFile("downloadedfile");
  17. if (!localFile.open(QIODevice::WriteOnly))
  18. return;
  19. const QByteArray sdata = data->readAll();
  20. localFile.write(sdata);
  21. qDebug() << sdata;
  22. localFile.close();
  23. emit done();
  24. }
  25. void QtDownload::download() {
  26. QUrl url = QUrl::fromEncoded(this->target.toLocal8Bit());
  27. QNetworkRequest request(url);
  28. QObject::connect(manager.get(request), SIGNAL(downloadProgress(qint64,qint64)), this, SLOT(downloadProgress(qint64,qint64)));
  29. }
  30. void QtDownload::downloadProgress(qint64 recieved, qint64 total) {
  31. qDebug() << recieved << total;
  32. }