如何在QT中使用带有URL的HTTP下载文件? [英] How to download a file using HTTP with URL in QT?

查看:69
本文介绍了如何在QT中使用带有URL的HTTP下载文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在使用setHost语句时,重新调整错误未定义的引用".
如何使用带有URL的HTTP下载文件?


While using setHost statement, It retuns error "undefined reference".
How to download a file using HTTP with URL?


QUrl url = QUrl::fromUserInput(strUrl);
QFileInfo fileInfo(url.path());
QString fileName=fileInfo.fileName();
if (QFile::exists(fileName)) {
   QMessageBox::information(this, tr("HTTP"),
   tr("There already exists a file called %1 in "
   "the current directory.")
   .arg(fileName));
}

file = new QFile(fileName);
if (!file->open(QIODevice::WriteOnly)) {
QMessageBox::information(this, tr("HTTP"),
tr("Unable to save the file %1: %2.")
.arg(fileName).arg(file->errorString()));
delete file;
file = 0;
}

file.setFileName(fileName);
file.open(QIODevice::WriteOnly);
QHttp *http;
http=new QHttp(this);
setHost(url.host(),QHttp::ConnectionModeHttp);

推荐答案

您的代码中使用的QHttp类为 ^ ].新代码应使用 QNetworkAccessManager [示例代码 [
The QHttp class used in your code is obsolete[^]. New code should use the QNetworkAccessManager[^] class instead. See the example code[^] in the Qt documentation.


从HTTP在下面::
Simplest way to download from the HTTP is below::
connect(&http,SIGNAL(done(bool)),this,SLOT(httpdon e())); //added in the constructor..



在头文件中……进行以下更改...



In the header file......Make the following changes...

QHttp http;
QFile file;



专用插槽:



private slots:

bool on_pushButton_clicked();
void httpdone();



信号:



signals:

void done();



在Qthttp.cpp文件中...



in the Qthttp.cpp file...

bool QtHttp:n_pushButton_clicked()
{

QString strUrl="http://www.blabla.com//file";
QUrl url = QUrl::fromUserInput(strUrl);

QFileInfo fileInfo(url.path());
QString strhost=url.encodedHost();
QString filename=fileInfo.fileName();
file.setFileName("C:\\Qt\\QHttp\\"+filename);
http.setHost(url.host(),url.port(80));
http.get(url.path(),&file);

if(!file.open(QIODevice::WriteOnly))
{
QMessageBox::warning(NULL,"warning","file is not opened",QMessageBox::Ok);
}
file.write(http.readAll());
http.close();
return true;

}

void QtHttp::httpdone()
{

file.close();
Q_EMIT done();
}


这篇关于如何在QT中使用带有URL的HTTP下载文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆