Qt 5.10 Book Examples
DownloaderGui.cpp
Go to the documentation of this file.
1 // ======================================================================
2 // DownloaderGui.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 <QtWidgets>
12 #include "Downloader.h"
13 #include "DownloaderGui.h"
14 
15 // ----------------------------------------------------------------------
16 DownloaderGui::DownloaderGui(QWidget* pwgt /*=0*/) : QWidget(pwgt)
17 {
18  m_pdl = new Downloader(this);
19  m_ppb = new QProgressBar;
20  m_ptxt = new QLineEdit;
21  m_pcmd = new QPushButton(tr("&Go"));
22 
23  QString strDownloadLink = "http://qt-book.com/pic.jpg";
24  m_ptxt->setText(strDownloadLink);
25 
26  connect(m_pcmd, SIGNAL(clicked()), SLOT(slotGo()));
27  connect(m_pdl, SIGNAL(downloadProgress(qint64, qint64)),
28  this, SLOT(slotDownloadProgress(qint64, qint64))
29  );
30  connect(m_pdl, SIGNAL(done(const QUrl&, const QByteArray&)),
31  this, SLOT(slotDone(const QUrl&, const QByteArray&))
32  );
33 
34  QGridLayout* pLayout = new QGridLayout;
35  pLayout->addWidget(m_ptxt, 0, 0);
36  pLayout->addWidget(m_pcmd, 0, 1);
37  pLayout->addWidget(m_ppb, 1, 0, 1, 1);
38  setLayout(pLayout);
39 }
40 
41 // ----------------------------------------------------------------------
42 void DownloaderGui::slotGo()
43 {
44  m_pdl->download(QUrl(m_ptxt->text()));
45 }
46 
47 // ----------------------------------------------------------------------
48 void DownloaderGui::slotDownloadProgress(qint64 nReceived, qint64 nTotal)
49 {
50  if (nTotal <= 0) {
51  slotError();
52  return;
53  }
54  m_ppb->setValue(100 * nReceived / nTotal);
55 }
56 
57 // ----------------------------------------------------------------------
58 void DownloaderGui::slotDone(const QUrl& url, const QByteArray& ba)
59 {
60  QString strFileName =
61  QStandardPaths::writableLocation(QStandardPaths::PicturesLocation)
62  + "/" + url.path().section('/', -1);
63  QFile file(strFileName);
64  if (file.open(QIODevice::WriteOnly)) {
65  file.write(ba);
66  file.close();
67 
68  if (strFileName.endsWith(".jpg")
69  || strFileName.endsWith(".png")
70  ) {
71  showPic(strFileName);
72  }
73  }
74 }
75 
76 // ----------------------------------------------------------------------
77 void DownloaderGui::showPic(const QString& strFileName)
78 {
79  QPixmap pix(strFileName);
80  pix = pix.scaled(pix.size() / 2,
81  Qt::IgnoreAspectRatio,
82  Qt::SmoothTransformation
83  );
84  QLabel* plbl = new QLabel;
85  plbl->setPixmap(pix);
86  plbl->setFixedSize(pix.size());
87  plbl->show();
88 }
89 
90 // ----------------------------------------------------------------------
91 void DownloaderGui::slotError()
92 {
93  QMessageBox::critical(0,
94  tr("Error"),
95  tr("An error while download is occured")
96  );
97 }
98 
void download(const QUrl &)
Definition: Downloader.cpp:24
DownloaderGui(QWidget *pwgt=0)