在Qt从URL下载文件 [英] Downloading File in Qt From URL

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

问题描述

在我的程序中,我需要下载一个文件,我遇到了这篇文章:

In my program I need to download a file, and I came across this article:

http://www.java2s.com/Code/Cpp/Qt/DownloadfromURL.htm

此代码可以工作,但它不适合我的程序,所以我重新编码它。我没有完成它,但我有基础编码。但是,当我测试它,它弹出一个发送错误报告窗口。

This code does work but it doesn't fit into my program so I re-coded it. I haven't completed it all but I've got the basics coded. However, when I test it, it pops up with a send error report window.

到目前为止,这是我的代码:

So far this is my code:

QtDownload.h

QtDownload.h

#include <QObject>
#include <QString>
#include <QNetworkAccessManager>
#include <QNetworkReply>


class QtDownload : public QObject
{
    Q_OBJECT
public:
    explicit QtDownload();
    ~QtDownload();

    void setTarget(const QString& t);

private:
    QNetworkAccessManager manager;
    QNetworkReply* reply;
    QString target;
    void connectSignalsAndSlots();

signals:

public slots:
    void download();
    void downloadFinished(QNetworkReply* data);
    void downloadProgress(qint64 recieved, qint64 total);
};

QtDownload.cpp

QtDownload.cpp

#include "qtdownload.h"

#include <QUrl>
#include <QNetworkRequest>
#include <QFile>

QtDownload::QtDownload()
    : QObject(0)
{
    this->connectSignalsAndSlots();
}

QtDownload::~QtDownload()
{
    if (reply != 0)
        delete reply;
}

void QtDownload::connectSignalsAndSlots()
{
    QObject::connect(&manager, SIGNAL(finished(QNetworkReply*)),this, SLOT(downloadFinished(QNetworkReply*)));
    QObject::connect(reply, SIGNAL(downloadProgress(qint64,qint64)), this, SLOT(downloadProgress(qint64,qint64)));
}

void QtDownload::setTarget(const QString &t)
{
    this->target = t;
}

void QtDownload::downloadFinished(QNetworkReply *data)
{
    QFile localFile("downloadedfile");
    if (!localFile.open(QIODevice::WriteOnly))
        return;
    localFile.write(data->readAll());
    localFile.close();
    delete data;
    data = 0;
}

void QtDownload::download()
{
    QUrl url = QUrl::fromEncoded(this->target.toLocal8Bit());
    QNetworkRequest request(url);
    this->reply = manager.get(request);
}

void QtDownload::downloadProgress(qint64 recieved, qint64 total)
{

}

main.cpp

#include "qtdownload.h"
#include <QTimer>

int main()
{
    QtDownload dl;
    dl.setTarget("http://www.java2s.com/Code/Cpp/Qt/DownloadfromURL.htm");

    QTimer::singleShot(0, &dl, SLOT(download()));
}

正如我所说的,它还没有完全完成,我继续前进。

As I said it's not completely finished but I want this part to be working before I move on.

我也是Qt的新人,所以任何提示都会感激。

I'm also new to Qt so any tips would be appreciated.

推荐答案


  • 您正在使用未初始化的指针,因此它指向无处。在您的构造函数中初始化回复与 NULL

  • reply = manager.get(...)),而不是在你的构造函数内部。 QNetworkManager as /doc.qt.nokia.com/4.7/qnetworkaccessmanager.html#finishedrel =nofollow> docs say

    • You're using uninitialized pointer, so it points out to nowhere. Initialize reply with NULL in your constructor.
    • You should connect reply after it was created (reply = manager.get(...)), not inside of your constructor.
    • QNetworkReply is never deleted by QNetworkManager as docs say:

    • 不要删除连接到此信号的插槽中的答复对象。使用deleteLater()。

      Do not delete the reply object in the slot connected to this signal. Use deleteLater().

      所以你不应该在 QNetworkReply 已完成。


      • c $ c>插槽设置数据 0 只会将参数值设置为 0 ,而不是您的类成员回复。这是一个不必要的代码行。您应该将回复成员改为 NULL

      • In finished slot setting data to 0 will only set parameter value to 0, not your class member reply. It's an unneeded line of code. You should set your reply member to NULL instead.

      此外,你应该考虑每次你获取数据块时写入一个文件,因为整个文件将在你的当前情况下缓存在内存中。当指向的网址文件较大时,可能会导致软件的内存使用量过大。

      Also you should consider writing to a file every time you get data chunk, as whole file will be buffered in memory in your current case. It may lead to huge memory usage of your software when file at pointed URL is big.

      这篇关于在Qt从URL下载文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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