Downloading File in Qt From URL

后端 未结 3 879
半阙折子戏
半阙折子戏 2020-12-31 17:43

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 d

3条回答
  •  长情又很酷
    2020-12-31 18:11

    You need QCoreApplication to start the event loop for Qt4. Something like this should work (not tested) :

    int main(int argc, char **argv) {
        QCoreApplication app(argc, argv);
        QtDownload dl;
        dl.setTarget("http://www.java2s.com/Code/Cpp/Qt/DownloadfromURL.htm");
    
        dl.download();
        QObject::connect(app, SIGNAL(aboutToQuit()), app, SLOT(quit()));
        return app.exec();
    }
    

    edit :: new version

    I found some problems :

    1. You don't need the custom reply, also you never set it to 0 in your constructor, so if it was never used it will delete a random piece of memory in your ~QtDownload();
    2. you were deleting data inside QtDownload::downloadFinished, which shouldn't be done, it is handled by Qt, so it was getting deleted twice.
    3. because of #2, you were deleting reply 3 times.

    Here's the modified version :

    qtdownload.h :

    #include 
    #include 
    #include 
    #include 
    
    
    class QtDownload : public QObject {
        Q_OBJECT
    public:
        explicit QtDownload();
        ~QtDownload();
    
        void setTarget(const QString& t);
    
    private:
        QNetworkAccessManager manager;
        QString target;
    
    signals:
        void done();
    
    public slots:
        void download();
        void downloadFinished(QNetworkReply* data);
        void downloadProgress(qint64 recieved, qint64 total);
    };
    

    qtdownload.cpp :

    #include "qtdownload.h"
    #include 
    #include 
    #include 
    #include 
    #include 
    
    QtDownload::QtDownload() : QObject(0) {
        QObject::connect(&manager, SIGNAL(finished(QNetworkReply*)),this, SLOT(downloadFinished(QNetworkReply*)));
    }
    
    QtDownload::~QtDownload() {
    
    }
    
    
    void QtDownload::setTarget(const QString &t) {
        this->target = t;
    }
    
    void QtDownload::downloadFinished(QNetworkReply *data) {
        QFile localFile("downloadedfile");
        if (!localFile.open(QIODevice::WriteOnly))
            return;
        const QByteArray sdata = data->readAll();
        localFile.write(sdata);
        qDebug() << sdata;
        localFile.close();
    
        emit done();
    }
    
    void QtDownload::download() {
        QUrl url = QUrl::fromEncoded(this->target.toLocal8Bit());
        QNetworkRequest request(url);
        QObject::connect(manager.get(request), SIGNAL(downloadProgress(qint64,qint64)), this, SLOT(downloadProgress(qint64,qint64)));
    
    }
    
    void QtDownload::downloadProgress(qint64 recieved, qint64 total) {
        qDebug() << recieved << total;
    }
    

    main.cpp :

    #include 
    #include "qtdownload.h"
    int main(int argc, char **argv) {
        QCoreApplication app(argc, argv);
        QtDownload dl;
        dl.setTarget("http://localhost");
    
        dl.download();
        //quit when the download is done.
        QObject::connect(&dl, SIGNAL(done()), &app, SLOT(quit()));
        return app.exec();
    }
    

提交回复
热议问题