代码拉取完成,页面将自动刷新
同步操作将从 TKG/QtBase 强制同步,此操作会覆盖自 Fork 仓库以来所做的任何修改,且无法恢复!!!
确定后同步将在后台操作,完成时将刷新页面,请耐心等待。
#include <QNetworkAccessManager>
#include <QNetworkReply>
#include <QFile>
#include <QDir>
#include <QMessageBox>
#include <QEventLoop>
#include "httpdownload.h"
HttpDownload::HttpDownload(QWidget *widget, QObject *parent) :
QObject(parent),m_manager(new QNetworkAccessManager(0)),m_loop(new QEventLoop){
m_widget = widget;
}
void HttpDownload::replyNewDate(){
if(m_file){
m_file->write(m_reply->readAll());
m_file->flush();
}
}
void HttpDownload::replyFinished(){
// 能通过reply读取请求的所有数据大小和吗?貌似很难,这种流式数据
m_reply->deleteLater();
m_file->close();
m_file->deleteLater();
emit finished();
}
void HttpDownload::replyError(){
m_down_size = 0;
emit finished();
}
void HttpDownload::replyProgress(qint64 recv,qint64 total){
m_down_size = total;//当数据大时,这会被重复设置
}
int HttpDownload::downloadFile(const QString &url, const QString &file, const QString &dir){
m_reply = m_manager->get(QNetworkRequest(QUrl(url+file)));
if(dir.isEmpty()){
m_file = new QFile(file);
return 0;
}else{
QDir directory(dir);
if(!directory.exists()){
directory.mkpath(dir);
}
if(dir.endsWith("/"))
m_file = new QFile(dir+file);
else
m_file = new QFile(dir+"/"+file);
}
if(!m_file->open(QIODevice::WriteOnly)){
QMessageBox::warning(0,"错误","文件打开失败",QMessageBox::Ok);
return 0;
}
connect(m_reply,SIGNAL(readyRead()),this,SLOT(replyNewDate()));
connect(m_reply,SIGNAL(finished()),this,SLOT(replyFinished()));
connect(m_reply,SIGNAL(error(QNetworkReply::NetworkError)),this,SLOT(replyError()));
connect(m_reply,SIGNAL(downloadProgress(qint64,qint64)),this,SLOT(replyProgress(qint64,qint64)));
connect(this,SIGNAL(finished()),m_loop,SLOT(quit()));
m_loop->exec();
return m_down_size;
}
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。