在使用URL下载文件时,出现标题的问题,编译器时32位的,后查询得到解决。 见以下博客 :
https://www.cnblogs.com/sagerking/p/12667575.htmlhttps://www.cnblogs.com/sagerking/p/12667575.html
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include<QtNetwork>
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)
{
replay = manager->get(QNetworkRequest(url));//第二步
connect(replay,SIGNAL(downloadProgress(qint64,qint64)),this,SLOT(updateDateReadProgress(qint64,qint64)));
connect(replay,SIGNAL(finished()),this,SLOT(httpFinshed()));
}
void MainWindow::on_pushButton_clicked()
{
url = ui->lineEdit->text();
QFileInfo info(url.path());
QString fileName(info.fileName()+".txt");//注意加入需要保存的文件类型
file =new QFile(fileName);
if(!file->open(QIODevice::WriteOnly))
{
// qDebug()<<"file open error";
printf("%s/n",strerror(errno));
delete file;
file=NULL;
return;
}
startRequest(url);
ui->progressBar->setValue(0);
ui->progressBar->show();
}
void MainWindow::httpFinshed()
{
QString all = replay->readAll();
ui->textBrowser->setText(all);
httpReadReady();//保存文件
ui->progressBar->hide();
file->flush();
file->close();
replay->deleteLater();
replay=0;
delete file;
file=0;
}
void MainWindow::httpReadReady()
{
int result=0;
if(result<=0)
qDebug()<<file->error();
}
void MainWindow::updateDateReadProgress(qint64 bytesRead, qint64 totalBytes)
{
ui->progressBar->setMaximum(totalBytes);
ui->progressBar->setValue(bytesRead);
}