如何在Qt中从互联网下载文件?

3

我不知道问题在哪里...编译器(Qt)在没有错误的情况下运行程序,但文件没有被下载...

请问您能告诉我出了什么问题吗?

我按照位于qt文件夹中的“下载”示例制作了自己的应用程序。唯一的区别是他们有控制台,而我有Windows应用程序。

mainwindow.h

#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QtNetwork>
#include <QMainWindow>

namespace Ui {

    class MainWindow;
}

class MainWindow : public QMainWindow
{
    Q_OBJECT

public:
    explicit MainWindow(QWidget *parent = 0);
    ~MainWindow();

private slots:
    void on_pushButton_clicked();

private:
    Ui::MainWindow *ui;
};

class DownloadManager: public QObject
{
    Q_OBJECT
    QNetworkAccessManager manager;
    QList<QNetworkReply *> currentDownloads;

public:
    DownloadManager();
    void doDownload(const QUrl &url);
    QString saveFileName(const QUrl &url);
    bool saveToDisk(const QString &filename, QIODevice *data);

public slots:
    void execute();
    void downloadFinished(QNetworkReply *reply);
};


#endif // MAINWINDOW_H

main.cpp

#include <QtGui/QApplication>
#include "mainwindow.h"
#include <QtNetwork>
#include <QNetworkAccessManager>
#include <QStringList>
#include <QCoreApplication>
#include <QFile>
#include <QFileInfo>
#include <QList>
#include <QNetworkAccessManager>
#include <QNetworkRequest>
#include <QNetworkReply>
#include <QStringList>
#include <QTimer>
#include <QUrl>

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    MainWindow w;
#if defined(Q_WS_S60)
    w.showMaximized();
#else
    w.show();
#endif

    return a.exec();
}

DownloadManager::DownloadManager()
{
    connect(&manager, SIGNAL(finished(QNetworkReply*)),
            SLOT(downloadFinished(QNetworkReply*)));
}

void DownloadManager::doDownload(const QUrl &url)
{
    QNetworkRequest request(url);
    QNetworkReply *reply = manager.get(request);

    currentDownloads.append(reply);
}

QString DownloadManager::saveFileName(const QUrl &url)
{
    QString path = url.path();
    QString basename = QFileInfo(path).fileName();

    if (basename.isEmpty())
        basename = "download";

    if (QFile::exists(basename)) {
        // already exists, don't overwrite
        int i = 0;
        basename += '.';
        while (QFile::exists(basename + QString::number(i)))
            ++i;

        basename += QString::number(i);
    }

    return basename;
}

bool DownloadManager::saveToDisk(const QString &filename, QIODevice *data)
{
    QFile file(filename);
    if (!file.open(QIODevice::WriteOnly)) {
        fprintf(stderr, "Could not open %s for writing: %s\n",
                qPrintable(filename),
                qPrintable(file.errorString()));
        return false;
    }

    file.write(data->readAll());
    file.close();

    return true;
}

void DownloadManager::execute()
{
    QStringList args = QCoreApplication::instance()->arguments();
    args[0]="http://www.google.ru/images/srpr/logo3w.png";

QString arg=args[0];

        QUrl url = QUrl::fromEncoded(arg.toLocal8Bit());
        doDownload(url);

}

void DownloadManager::downloadFinished(QNetworkReply *reply)
{
    QUrl url = reply->url();
    if (reply->error()) {
        fprintf(stderr, "Download of %s failed: %s\n",
                url.toEncoded().constData(),
                qPrintable(reply->errorString()));
    } else {
        QString filename = saveFileName(url);
        if (saveToDisk(filename, reply))
            printf("Download of %s succeded (saved to %s)\n",
                   url.toEncoded().constData(), qPrintable(filename));
    }

    currentDownloads.removeAll(reply);
    reply->deleteLater();

    if (currentDownloads.isEmpty())
      //   all downloads finished
        QCoreApplication::instance()->quit();
}

mainwindow.cpp

#include "mainwindow.h"
#include "ui_mainwindow.h"


MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);
}

MainWindow::~MainWindow()
{
    delete ui;
}


void MainWindow::on_pushButton_clicked()
{
    DownloadManager manager;

    manager.execute();
    QTimer::singleShot(0, &manager, SLOT(execute()));
}

尝试将那些fprintf转换为消息框或其他GUI元素;它可能正在显示错误,但您无法看到它们,因为它是一个GUI应用程序而不是控制台应用程序。 - icktoofay
-1 懒惰地复制粘贴整个代码,而不是隔离相关部分。 - laurent
好的,谢谢。我添加了一个QMessageBox,但仍然没有任何反应。 没有任何MessageBox显示... - Kamil
尝试在调试器中逐步执行每个函数。在每个函数的开头设置断点。 - MSalters
感谢大家!如果在此之后加入以下代码,它就能正常工作: QStringList args = QCoreApplication::instance()->arguments(); args[0]="http://www.google.ru/images/srpr/logo3w.png"; QString arg=args[0]; QUrl url = QUrl::fromEncoded(arg.toLocal8Bit()); doDownload(url);在这些代码之后,我添加了以下代码: QMessageBox msgBox; msgBox.setText("请稍等..."); msgBox.exec(); - Kamil
显示剩余2条评论
1个回答

4
问题出现在你代码的这个位置:
void MainWindow::on_pushButton_clicked()
{
    DownloadManager manager;    
    manager.execute();
}

QNetworkAccessManager是异步的,因此需要事件循环来进行任何下载操作。但是,当函数on_pushButton_clicked()返回并将控制权交还给事件循环时,QNetworkAccessManager已经被销毁,并且没有时间做任何事情。

当您在DownloadManager :: execute中添加QMessageBox时,实际上是在on_pushButton_clicked()槽内运行另一个事件循环,并为QNetworkAccessManager提供执行其工作的机会。

正确的解决方案是动态分配DownloadManager,并在完成所有下载后使其自行销毁。

void MainWindow::on_pushButton_clicked()
{
    DownloadManager *manager = new DownloadManager(this);    
    manager->execute();
}

void DownloadManager::downloadFinished(QNetworkReply *reply)
{
    ...
    if (currentDownloads.isEmpty())
        this->deleteLater();
}

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接