Qt下载文件 - QNetworkAccessManager,没有获取数据 [英] Qt Download File - QNetworkAccessManager, not getting data

查看:2181
本文介绍了Qt下载文件 - QNetworkAccessManager,没有获取数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图让我的应用程式从网址(通常是EXE或JAR)下载档案,但这并不会改变很多。我有这个全部运行在一个线程,但我不认为这将有所作为(如果它让我知道)。

I'm trying to have my application download a file from a URL, typically an EXE or a Jar, not that this should change much though. I have this all running in a thread, but I don't think that will make a difference (if it does let me know).

所以Do_Download是我的功能,创建管理器,设置URL和请求,并执行get。然后尝试将完成的信号连接到将写入文件的插槽。

So Do_Download is my function that creates the manager, sets the URL and request, and performs get. I then try to connect the finished signal to the slot the will write the file.

void DownloadManager::Do_Download() {
    QNetworkAccessManager *netManager = new QNetworkAccessManager(this);
    QUrl url(install_mirror); //istall_mirror is the URL provided by user
    QNetworkRequest req(url);

    QNetworkReply *reply = netManager->get(req);

    connect(reply, SIGNAL(finished()), this, SLOT(writeData()));
}

我的writeData函数检查错误,如果没有错误,数据到文件。

My writeData function checks for errors, and if there are no errors it writes the data to file.

void DownloadManager::writeData() {
    QNetworkReply *reply = qobject_cast<QNetworkReply*>(sender());

    if (reply) {
        if (reply->error() == QNetworkReply::NoError) {
            QFile file(location);
            if(file.open(QIODevice::WriteOnly)) {
                    file.write(reply->readAll());
            } else {
                errorMessage = "Error writing downloaded file for mirror installation";
            }
        } else {
            //get http status code
            int httpStatus = reply->attribute(QNetworkRequest::HttpStatusCodeAttribute).toInt();
            errorMessage = "HTTP Error code while downloading from mirror: " + httpStatus;
        }

        reply->deleteLater();
    } else {
        errorMessage = "Error downloading file from installation mirror";
    }
}

问题是没有写入数据。它只是创建一个0Kb文件。
我试图添加一个下载进度槽,所以我可以看到接收数据的情况。所以我将它添加到我的Do_Download方法。

The problem being there is no data being written. It just creates a 0Kb file. I tried adding a download progress slot so I could see what was going on recieving the data. So I added this to my Do_Download method.

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

void DownloadManager::DL_Progress(qint64 recieved, qint64 total) {
    std::cout << recieved << " / " << total << endl;
}

输出一次显示为0/01

The output displays one time as 0 / 01

我做错了什么?

推荐答案

我在代码中看到的唯一问题是不等待下载完成。

The only problem I see in your code is you are not waiting for the download to be finished. The NetworkRequest object would be destructed at the end of function call.

因此,我将重写Do_Download这样(QEventLoop同步网络请求):

So, I would rewrite Do_Download like this (QEventLoop syncronizes the network request):

void DownloadManager::Do_Download() {
    QEventLoop eventLoop;
    QNetworkAccessManager *netManager = new QNetworkAccessManager(this);
    QUrl url(install_mirror); //istall_mirror is the URL provided by user
    QNetworkRequest req(url);

    QNetworkReply *reply = netManager->get(req);

    connect(reply, SIGNAL(finished()), &eventLoop, SLOT(quit()));
    eventLoop.exec();

    writeData(reply);
}

这篇关于Qt下载文件 - QNetworkAccessManager,没有获取数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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