创建一个mainwind类Application

mainwind.h

#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include <QMainWindow>
#include <QtNetwork>

namespace Ui {
class MainWindow;
}

class MainWindow : public QMainWindow
{
    Q_OBJECT

public:
    explicit MainWindow(QWidget *parent = 0);
    ~MainWindow();
    void startRequest(QUrl url);//请求链接

private slots:
    void on_pushButton_clicked();//下载按钮的单击事件槽函数
    void httpFinished();//完成下载后的处理
    void httpReadyRead();//接收到数据时的处理
    void updateDataReadProgress(qint64, qint64); //更新进度条

private:
    Ui::MainWindow *ui;
    QNetworkAccessManager *manager;
    QNetworkReply *reply;
    QUrl url;   //存储网络地址
    QFile *file;  //文件指针

};

#endif // MAINWINDOW_H

在mainwind.cpp

#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QTextCodec>
#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, SIGNAL(readyRead()), this, SLOT(httpReadyRead()));

        connect(reply, SIGNAL(downloadProgress(qint64, qint64)),
         this, SLOT(updateDataReadProgress(qint64, qint64)));

       connect(reply, SIGNAL(finished()), this, SLOT(httpFinished()));
}
//判断是否创建了文件,如果是则读取返回的所有数据,然后写入文件。
//该文件是在下载按钮单击信号槽中创建并打开的。
void MainWindow::updateDataReadProgress(qint64 bytesRead, qint64 totalBytes)
{
    ui->progressBar->setMaximum(totalBytes);
    ui->progressBar->setValue(bytesRead);
}
//当下载后,重新隐藏进度条,然后删除reply和服了对象。
void MainWindow::on_pushButton_clicked()
{
        url = ui->lineEdit->text();//从lineEdit中获取信息
        QFileInfo info(url.path());//从url中获取path信息
        QString fileName(info.fileName());//从url中获取filename
        if (fileName.isEmpty()) fileName = "index.html";//判断文件名是否存在,如果为空将filename设置为默认页
        file = new QFile(fileName);//根据url中的文件名创建url对像
        if(!file->open(QIODevice::WriteOnly))
        {
            qDebug() << "file open error";
            delete file;
            file = 0;
            return;
         }
        qDebug()<<"url"<<url;
        startRequest(url);//开始请求url
        ui->progressBar->setValue(0);//设置进度条只为0
        ui->progressBar->show();//显示进度条
}

void MainWindow::httpFinished()
{
        ui->progressBar->hide();//数据传输完成隐藏进度条
        file->flush();//刷新数据缓冲区
        file->close();//关闭文件描述符
        reply->deleteLater();//释放relay对象
        reply = 0;
        delete file;
        file = 0;
}

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

执行编译运行后可下载url链接http://p4.so.qhmsg.com/t010d8cda7193fa4099.jpg如下为图片下载链接QT网络编程http之url下载运行效果

下载结果QT网络编程http之url下载

快获取捏的妹子吧(@——@)

QT网络编程http之url下载


 
                    
            
                

相关文章:

  • 2021-11-28
  • 2022-01-01
  • 2021-10-20
  • 2021-12-12
  • 2021-05-23
  • 2021-09-09
猜你喜欢
  • 2022-01-02
  • 2022-12-23
  • 2022-12-23
  • 2021-12-13
  • 2021-04-10
  • 2022-02-12
相关资源
相似解决方案