1.常用到的类
QNetworkAccessManager类用于协调网络操作,负责发送网络请求,创建网络响应
QNetworkReply类表示网络请求的响应。在QNetworkAccessManager发送一个网络请求后创建一个网络响应。它提供了以下信号:
finished():完成后发出信号
readyRead():有数据读的数据发出信号
downloadProgress():表示网络操作进度的信号,有bytesRead和totalBytes两个参数,表示已读取字节数和总字节数
QNetworkReply 是QIODevice的子类,所以QNetworkReply支持流读写功能,也支持异步或同步工作。
2.代码
.h
#ifndef HTTP_H
#define HTTP_H
#include <QMainWindow>
#include <QUrl>
#include <QFile>
#include <QNetworkAccessManager>
#include <QNetworkReply>
QT_BEGIN_NAMESPACE
namespace Ui { class http; }
QT_END_NAMESPACE
class http : public QMainWindow
{
Q_OBJECT
public:
http(QWidget *parent = nullptr);
~http();
private:
Ui::http *ui;
QNetworkAccessManager networkManager;
QNetworkReply *reply;
QFile *downFile;
public slots:
void on_finished();
void on_readyRead();
void downLoadProgress(qint64 bytesRead,qint64 totalBytes);
private slots:
void on_pushButton_2_clicked();
void on_pushButton_clicked();
};
#endif // HTTP_H
#ifndef HTTP_H
#define HTTP_H
#include <QMainWindow>
#include <QUrl>
#include <QFile>
#include <QNetworkAccessManager>
#include <QNetworkReply>
QT_BEGIN_NAMESPACE
namespace Ui { class http; }
QT_END_NAMESPACE
class http : public QMainWindow
{
Q_OBJECT
public:
http(QWidget *parent = nullptr);
~http();
private:
Ui::http *ui;
QNetworkAccessManager networkManager;
QNetworkReply *reply;
QFile *downFile;
public slots:
void on_finished();
void on_readyRead();
void downLoadProgress(qint64 bytesRead,qint64 totalBytes);
private slots:
void on_pushButton_2_clicked();
void on_pushButton_clicked();
};
#endif // HTTP_H
.cpp
#include "http.h"
#include "ui_http.h"
#include<QDir>
http::http(QWidget *parent)
: QMainWindow(parent)
, ui(new Ui::http)
{
ui->setupUi(this);
}
http::~http()
{
delete ui;
}
void http::on_finished()
{
QFileInfo fileInfo;
fileInfo.setFile(downFile->fileName());
downFile->close();
delete downFile;
reply->deleteLater();
reply = nullptr;
ui->pushButton->setEnabled(true);
}
void http::on_readyRead()
{
downFile->write(reply->readAll());
}
void http::downLoadProgress(qint64 bytesRead, qint64 totalBytes)
{
ui->progressBar->setMaximum(totalBytes);
ui->progressBar->setValue(bytesRead);
}
void http::on_pushButton_2_clicked()
{
//缺省路径
QString curPath = QDir::currentPath();
QDir dir(curPath);
QString sub = "temp";
dir.mkdir(sub);
ui->lineEdit_2->setText(curPath + "/" + sub + "/");
}
void http::on_pushButton_clicked()
{
//下载
QString url = ui->lineEdit->text().trimmed();
if(url.isEmpty())
return;
QUrl newUrl = QUrl::fromUserInput(url);
if(!newUrl.isValid())
return;
QString tempDir = ui->lineEdit_2->text().trimmed();
if(tempDir.isEmpty())
return;
QString fullFileName = tempDir+newUrl.fileName();
if(QFile::exists(fullFileName))
QFile::remove(fullFileName);
downFile = new QFile(fullFileName);
if(!downFile->open(QIODevice::WriteOnly))
return;
ui->pushButton->setEnabled(false);
reply = networkManager.get(QNetworkRequest(newUrl));
connect(reply,&QNetworkReply::finished,this,&http::on_finished);
connect(reply,&QNetworkReply::readyRead,this,&http::on_readyRead);
connect(reply,&QNetworkReply::downloadProgress,this,&http::downLoadProgress);
}
结果:
点击缺省路径,保存文件
在地址栏输入下载的文件地址,例:下载网易云音乐logo图片,地址如下
http://p3.music.126.net/tBTNafgjNnTL1KlZMt7lVA==/18885211718935735.jpg
原文链接:https://blog.csdn.net/wzz953200463/article/details/123858775