QT HTTP exercise-Create a UI program that can download URL resources and save it to a local directory

Source: Internet
Author: User
#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}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.