Qt 5.10 Book Examples
Downloader.cpp
Go to the documentation of this file.
1 // ======================================================================
2 // Downloader.cpp
3 // ======================================================================
4 // This file is a part of the book
5 // "Qt 5.10 Professional programming with C++"
6 // http://qt-book.com
7 // ======================================================================
8 // Copyright (c) 2017 by Max Schlee
9 // ======================================================================
10 
11 #include <QtNetwork>
12 #include "Downloader.h"
13 
14 // ----------------------------------------------------------------------
15 Downloader::Downloader(QObject* pobj/*=0*/) : QObject(pobj)
16 {
17  m_pnam = new QNetworkAccessManager(this);
18  connect(m_pnam, SIGNAL(finished(QNetworkReply*)),
19  this, SLOT(slotFinished(QNetworkReply*))
20  );
21 }
22 
23 // ----------------------------------------------------------------------
24 void Downloader::download(const QUrl& url)
25 {
26  QNetworkRequest request(url);
27  QNetworkReply* pnr = m_pnam->get(request);
28  connect(pnr, SIGNAL(downloadProgress(qint64, qint64)),
29  this, SIGNAL(downloadProgress(qint64, qint64))
30  );
31 }
32 
33 // ----------------------------------------------------------------------
34 void Downloader::slotFinished(QNetworkReply* pnr)
35 {
36  if (pnr->error() != QNetworkReply::NoError) {
37  emit error();
38  }
39  else {
40  emit done(pnr->url(), pnr->readAll());
41  }
42  pnr->deleteLater();
43 }
void downloadProgress(qint64, qint64)
void download(const QUrl &)
Definition: Downloader.cpp:24
Downloader(QObject *pobj=0)
Definition: Downloader.cpp:15
void done(const QUrl &, const QByteArray &)
void error()