Qt network programming a network file download 2

 Function: Enter the address resource, click download, download the file and displays the download progress.

code show as below:

myHttp2.pro

QT       += network

 mainwindow.h

#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include <QMainWindow>
#include <QUrl>

namespace Ui {
class MainWindow;
}

class QFile;
class QNetworkReply;
class QNetworkAccessManager;
class MainWindow : public QMainWindow
{
    Q_OBJECT

public:
    explicit MainWindow(QWidget *parent = nullptr);
    ~MainWindow();

    void startRequest(QUrl url);
private:
    Ui::MainWindow *ui;

    QNetworkAccessManager* manager;
    QNetworkReply *reply;
    QUrl url;
    QFile *file;
private slots:
    void httpFinished();
    void httpReadyRead();
    void updateDataReadProgress(qint64,qint64);
    void on_pushButton_clicked();
};

#endif // MAINWINDOW_H

mainwindow.cpp

#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QtNetwork>
#include <QFile>
#include <QDebug>
MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);

    manager = new QNetworkAccessManager(this);
    ui->progressBar->hide();
}

MainWindow::~MainWindow()
{
    delete ui;
}

void MainWindow::startRequest(QUrl url)
{
    reply = manager->get(QNetworkRequest(url));
    connect(reply,&QNetworkReply::readyRead,this,&MainWindow::httpReadyRead);
    connect(reply,&QNetworkReply::downloadProgress,this,&MainWindow::updateDataReadProgress);
    connect(reply,&QNetworkReply::finished,this,&MainWindow::httpFinished);
}

void MainWindow::httpReadyRead()
{
    if (file) {
        file->write(reply->readAll());
    }
}

void MainWindow::updateDataReadProgress(qint64 bytesRead,qint64 totalBytes)
{
    ui->progressBar->setMaximum(totalBytes);
    ui->progressBar->setValue(bytesRead);
}

void MainWindow::httpFinished()
{
    ui->progressBar->hide();
    if (file) {
        file->close();
        delete file;
        file = nullptr;
    }
    reply->deleteLater();
    reply = 0;
}

void MainWindow::on_pushButton_clicked()
{
    url = ui->lineEdit->text();
    QFileInfo info(url.path());
    QString fileName(info.fileName());
    qDebug()<<fileName;
    if (fileName.isEmpty()) {
        fileName = "index.html";
    }
    file = new QFile(fileName);
    if (!file->open(QIODevice::WriteOnly)) {
        delete file;
        file = 0;
        return;
    }
    startRequest(url);
    ui->progressBar->setValue(0);
    ui->progressBar->show();
}

 

As used herein, the get () function to send the network a request, then the signal QNetworkReply several objects and associated custom grooves. Wherein the readyRead () signal QIODevice inherited from class whenever new data can be read, the transmit signal will be; whenever requested network download progress update will transmit download-progress () signal is used to update the progress bar; the end of each transponder will transmit processing finished () finished the same signal that the previous program QNetworAccessManager class () signal is applied, but different senders, parameters are also different.

Published 257 original articles · won praise 22 · views 90000 +

Guess you like

Origin blog.csdn.net/qq_24127015/article/details/104654809