#include #include "mainwindow.h" int main (int argc, char *argv[]) {qapplication A (argc, argv);
MainWindow W;
W.show ();
return A.exec ();
} #ifndef mainwindow_h #define MAINWINDOW_H #include #include//① forward declaration class QFile;
Class qnetworkreply;
Class Qnetworkaccessmanager;
Namespace Ui {class MainWindow;
} class Mainwindow:public Qmainwindow {q_object public:explicit MainWindow (qwidget *parent = 0);
~mainwindow (); void startrequest (qurl URL);
Declares a public response function to receive the URL private slots://③ declaration slot void httpfinished ();
void Httpreadyread ();
void Updatedataprogress (Qint64,qint64);
void on_pushbutton_clicked ();
Private://② defines Qnetworkaccessmanager *manager;
Ui::mainwindow *ui;
Qnetworkreply *reply;
Qurl URL;
QFile *file;
}; #endif//Mainwindow_h#include "mainwindow.h" #include "ui_mainwindow.h" #include #include #include #include mainwindo W::mainwindow (Qwidget *parent): QmaiNwindow (parent), UI (new Ui::mainwindow) {ui->setupui (this); Manager = new Qnetworkaccessmanager (this); Create a network Management instance Manager ui->progressbar->hide ();
Hide Progress bar} Mainwindow::~mainwindow () {Delete UI;}
void mainwindow::startrequest (qurl url) {reply = Manager->get (qnetworkrequest (URL));
Connect (reply,signal (Readyread ()), This,slot (Httpreadyread ()));
Connect (reply,signal (downloadprogress (Qint64,qint64)), This,slot (Updatedataprogress (Qint64,qint64)));
Connect (reply,signal (finished ()), This,slot (httpfinished ()));
}//Download complete, hide progress bar, close file void mainwindow::httpfinished () {ui->progressbar->hide ();
File->flush ();
File->close ();
Reply->deletelater ();
reply=0;
Delete file;
file=0;
}//Read ready to read the file into the files in void Mainwindow::httpreadyread () {if (file) File->write (Reply->readall ());} During the download process, the activation progress bar dynamically shows void Mainwindow::updatedataprogress (Qint64 bytesread, Qint64 totalbytes) {ui->progressbar-≫setmaximum (totalbytes);
Ui->progressbar->setvalue (Bytesread); } void Mainwindow::on_pushbutton_clicked () {Qdir *temp = new Qdir;
Get the address of qdir, not copy a Qdir//determine if the folder exists bool exist = Temp->exists ("./download");
if (exist) qmessagebox::warning (This,tr ("Creat folder"), TR ("The folder is exist!"); else {bool OK = Temp->mkdir ("./download"); Create if (OK) qmessagebox::warning when the folder does not exist (THIS,TR ("Creat folder"), tr ("creat the folder Successful")
);
}//Get URL address url from lineEdit = Ui->lineedit->text ();
Qfileinfo info= (Url.path ());
Qdebug () <open (qiodevice::writeonly)) {qdebug () << "File open error";
Delete file;
file=0;
Return } startrequest (URL); Get URL ui->progressbar->setvalue (0); Set the progress bar value starting at 0 ui->progressbar->show (); Show Progress Bar}