qt http Download file

Source: Internet
Author: User
Tags file transfer protocol

This article describes how to use HTTP to download files from a Web site. In QT network programming, protocol, or HTTP, is required. It is a Hypertext Transfer Protocol, which is a file transfer protocol. There is not much explanation for HTTP.

In QT Network programming , protocol, or HTTP, is required. It is a Hypertext Transfer Protocol, which is a file transfer protocol. In this section we will explain how to use HTTP to download files from a Web site. The programming environment used is Qt4.6.3-based QT Creator 1.3.1 under Windows

First, the simplest implementation.

1. We create a new Qt 4 Gui qapplication.

The project is named "http", then the Qtnetwork module is selected, and the base class selects Qwidget. Note: If the QtNetwork module is not added when you create a new project, you should manually add the code to the project file. Pro

    1. QT + = Network

Indicates that we have used a network module.

2. We add a Text Browser in the Widget.ui file, such as.

3: In Widget.h we add code.

Add header file: #include

Private variables in private: Qnetworkaccessmanager *manager;

Private slot function in private slots: void Replyfinished (qnetworkreply *);

4. Add code to the Widget.cpp file.

Add the following code to the constructor:

    1. Manager = new Qnetworkaccessmanager (this); New Qnetworkaccessmanager Object
    2. Connect (manager,signal (finished (qnetworkreply*)),//correlated signals and slots
    3. This,slot (replyfinished (qnetworkreply*)));
    4. Manager->get (Qnetworkrequest (Qurl ("http://www.yafeilinux.com")); Send Request

Then define the function:

    1. void widget::replyfinished (qnetworkreply *reply)//When the reply is over
    2. {
    3. Qtextcodec *codec = Qtextcodec::codecforname ("Utf8″");
    4. Use UTF8 encoding to display Chinese
    5. QString all = Codec->tounicode (Reply->readall ());
    6. Ui->textbrowser->settext (All);
    7. Reply->deletelater (); Finally, you want to release the reply object
    8. }

5. The operating effect is as follows.

6. Code Analysis.

The above implementation of the simplest application of the HTTP protocol download Web page program. The Qnetworkaccessmanager class is used to send network requests and accept replies, specifically, by using the Qnetworkrequest class to manage requests, qnetworkreply classes to receive replies, and to process the data.

In the code above, we used the following code to send the request:

    1. Manager->get (Qnetworkrequest (Qurl ("http://www.yafeilinux.com"));

It returns a Qnetworkreply object, which is said below. We just need to know that it will download the data as soon as the request is successfully sent. When the data download is complete, the manager emits a finished () signal, which we associate with:

    1. Connect (manager,signal (finished (qnetworkreply*)),
    2. This,slot (replyfinished (qnetworkreply*)));

That is, when the download data is finished, the replyfinished () function is executed. In this function we process the received data:

    1. Qtextcodec *codec = Qtextcodec::codecforname ("Utf8″");
    2. QString all = Codec->tounicode (Reply->readall ());
    3. Ui->textbrowser->settext (All);

Here, in order to display the Chinese in the downloaded Web page, we used the Qtextcodec class object and applied the UTF8 encoding.

You can read all the downloaded data using the Reply->readall () function. We then display the data in Textbrowser. When the reply object has finished its function, we need to release it, which is the last code:

    1. Reply->deletelater ();
second, the function extension through the example above, it is very simple to write a program based on the HTTP protocol in QT, only more than 10 lines of code. However, generally we download the file to see the download progress. Let's change the program above so that it can download any file and show the download progress. 1. We change the Widget.ui file as. Here we have added a line Edit, a label, a progress Bar and a push Button, and their familiarity remains the default. We click the right mouse button on the push button, select Go to slot, then select clicked (), enter its Click event slot function, and now we do not write code. Before we write the code, let's start with the process of executing the entire program: Let's first hide the progress bar. When we enter in line edit, click the Download button, we apply the input, get the file name, create a new file on the disk, to save the downloaded data, then link, and display the progress bar. During the download, we write each data we get to the file and update the progress bar, and after we receive the file, we re-hide the progress bar and do some cleanup work. According to this idea, we begin to write the code. 2. We add the code in the Widget.h file, and it's part of the following when done.
The relevant content in the 3.widget.cpp file is as follows. (1) in the constructor:
We first hide the progress bar in the constructor. Wait until you start the download and show it again. (2) The Click event Slot function of the download button.


Here we first get the input address from the interface and then break out the file name. Because there may not be a file name in the address, we use a default file name. Then we use this file name to create a new file that will be saved to the Debug folder in the project folder. Below we open the file, then link it and show the progress bar.


(3) Link request function.

    1. void widget::startrequest (qurl URL)//link Request
    2. {
    3. Reply = Manager->get (qnetworkrequest (URL));
    4. The following associated signals and slots
    5. Connect (reply,signal (finished ()), This,slot (httpfinished ()));
    6. After the download is complete
    7. Connect (reply,signal (Readyread ()), This,slot (Httpreadyread ()));
    8. When there is data available
    9. Connect (reply,signal (downloadprogress (Qint64,qint64)),
    10. This,slot (Updatedatareadprogress (Qint64,qint64)));
    11. Update progress bar
    12. }

In the previous example we mentioned Manager->get (qnetworkrequest (URL)) and returned a Qnetworkreply object, where we obtained this object and used it to perform the function of displaying the download progress of the data. This is mainly associated with several signals and slots. When data is available, the reply emits a readyread () signal, and we can then save the available data. It is here that the data is saved in a segmented download, which saves a lot of memory than when you download all the data. Using Reply's downloadprogress () signal, it is easy to achieve the display of the progress bar.

(4) Save data function.

    1. void Widget::httpreadyread ()//Available data
    2. {
    3. if (file) File->write (Reply->readall ()); If the file exists, write to the file
    4. }

Here, when file is available, the downloaded data is written to the file.

(5) Update the progress bar function.

    1. void Widget::updatedatareadprogress (Qint64 bytesread, Qint64 totalbytes)
    2. {
    3. Ui->progressbar->setmaximum (totalbytes); Maximum Value
    4. Ui->progressbar->setvalue (Bytesread); Current value
    5. }

Update the progress bar whenever there is data coming.

(6) Complete the download.

    1. void widget::httpfinished ()//Complete download
    2. {
    3. Ui->progressbar->hide ();
    4. File->flush ();
    5. File->close ();
    6. Reply->deletelater ();
    7. Reply = 0;
    8. Delete file;
    9. File = 0;
    10. }

This is only done when the download is complete, to do some processing.

4. We run the program, the effect is as follows.

Download Web file:

Download the Laura Block game on the Hua June Software Park:

After the download is complete, you can see the downloaded files in the Debug folder in the project folder.

The content of our HTTP app is here, and it's easy to see it, and it doesn't require you to know too much about the fundamentals of HTTP. For other uses of the related classes, you can view their help.

Http://blog.csdn.net/zhangbinsijifeng/article/details/47605123

qt http Download file

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.