3

我有一个非常令人困惑的问题。

我有一个从一些 ftp 服务器下载文件的简单项目。它工作得很好。

然后,我尝试将相同的代码实现到一个更大的项目中(第一个是控制台应用程序,第二个是 GUI,但我认为这不会改变任何东西..)。

在进行了一些调试之后,在我看来,finished()来自QNetworkAccessManager某种方式的信号永远不会被发出(或接收)。同样,完全相同的代码行作为一个单独的项目工作。

下载器.h

#ifndef DOWNLOADER_H
#define DOWNLOADER_H

#include <QObject>
#include <QNetworkAccessManager>
#include <QNetworkRequest>
#include <QNetworkReply>
#include <QUrl>
#include <QFile>
#include <QDebug>

class Downloader : public QObject
{
    Q_OBJECT

public:
    explicit Downloader(QObject *parent = 0);



signals:

    void dloadend();
    void printed();

public slots:
    void replyFinished (QNetworkReply *reply);
    void doDownload(QUrl url);
    void printDLend();

private:
    QNetworkAccessManager *manager;
};

#endif // DOWNLOADER_H

下载器.cpp

#include "downloader.h"

Downloader::Downloader(QObject *parent) :
    QObject(parent)
{
}
void Downloader::doDownload(QUrl url)
{
    qDebug()<<"entry: doDownload\n";
    QNetworkRequest req(url);
    manager = new QNetworkAccessManager(this);
    connect(manager, SIGNAL(finished(QNetworkReply*)),this,SLOT(printDLend()));//SLOT(replyFinished(QNetworkReply*)));
    manager -> get(req);
    qDebug()<<"exit: doDownload\n";
}

void Downloader::replyFinished (QNetworkReply *reply)
{
    qDebug()<<"entry: reply\n";

    if(reply->error()) {
        qDebug() << "ERROR!";
        qDebug() << reply->errorString();
    }
    else

    {
        qDebug() << "Download finished!";

        QFile *file = new QFile("C:/users/jelicicm/Desktop/test1.hex");

        if(file->open(QFile::Append))
        {
            file->write(reply->readAll());
            file->flush(); file->close();
            qDebug() <<"Downloaded file size:" <<file->size() <<"Bytes";
            qDebug() <<"File name: "<< file->fileName();
;
        }
        delete file;
    }

    reply->deleteLater();
    manager->deleteLater();
    emit dloadend();

}

mainwindow.cpp(重要部分)

void MainWindow::on_actionDownloadFirmwareImage_triggered()
{
    Downloader d;

    QUrl url("ftp://ftp.xenbase.org/pub/Genomics/JGI/Xenla6.0/Xenla_6.0_JGI_Gene_Models.fasta.tgz");

    qDebug() << "url, debug";
    ui->plainTextEdit->appendPlainText(url.toDisplayString());

    d.doDownload(url);

    QObject::connect(&d,SIGNAL(dloadend()),this, SLOT(printDLend()));
}

无法理解这一点。

欢迎任何帮助,

谢谢!

编辑>更多信息:调试器发布此>

网址,调试

条目:doDownload

退出:下载

4

1 回答 1

1

您在堆栈上创建对象Downloader,并在函数退出后立即将其删除。您必须使用创建对象new,并提供MainWindow对象作为父对象,因此在您关闭 MainWindow 后,该对象将被销毁。如果下载完成,您仍然需要销毁对象,因此只需将dloadend()信号连接到deleteLater()插槽,Qt 循环将在处理完所有信号后立即删除您的对象。

void MainWindow::on_actionDownloadFirmwareImage_triggered()
{
    Downloader *d = new Downloader(this);

    QUrl url("ftp://example.com/some.file.tgz");

    qDebug() << "url, debug";
    ui->plainTextEdit->appendPlainText(url.toDisplayString());

    d->doDownload(url);

    QObject::connect(d,SIGNAL(dloadend()),this, SLOT(printDLend()));
    QObject::connect(d,SIGNAL(dloadend()), d, SLOT(deleteLater()));
}
于 2015-04-03T15:15:25.237 回答