Question

I want to create a application, by this i can download the file from my ftp server and show the progress in progressbar. Ive wrote some code, but if im clicking on the button to download the file from ftp server, my application is crashing. Ive become some qDebug answers like:

"no errors request", "updateDataTransferProgress started"

the file which must be downloaded was created in a folder, but the file is empty.:( What can u do to fix my problem?

Many thanks!

    #include "f1.h"
    #include "ui_f1.h"

    #include "ui_form2.h"
    #include "form2.h"


    #include <QNetworkAccessManager>
    #include <QFile>
    #include <QFtp>
    #include <QtNetwork>
    #include <QMessageBox>


    f1::f1(QWidget *parent) :
        QFrame(parent),
        ui(new Ui::f1)
    {
        ui->setupUi(this);
       // ui->progressBar->setValue(0);

        connect(ui->pushButton,SIGNAL(clicked()),this,SLOT(onDownServ()));


    }

    void f1::closeEvent(QCloseEvent *event)
    {

        F2->helloWorld();

    }



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


    void f1::onDownServ()
    {



        QNetworkAccessManager *nam = new QNetworkAccessManager();
            QUrl url2("ftp://test.cz/plugins.txt");
            url2.setPassword("test");
            url2.setUserName("test");



          reply = nam->get(QNetworkRequest(url2));


        connect(reply, SIGNAL(readyRead()), this, SLOT(readyRead()));
        connect(reply, SIGNAL(downloadProgress(qint64, qint64)),this, SLOT(updateDataTransferProgress(qint64,qint64)));
        connect(reply, SIGNAL(error(QNetworkReply::NetworkError)), SLOT(requestError(QNetworkReply::NetworkError)));
        connect(reply, SIGNAL(finished()), this, SLOT(requestFinished()));

    }

    void f1::requestFinished()
    {

    qDebug() << "finished !";

    save->flush();
    save->close();

    }

    void f1::requestError(QNetworkReply::NetworkError)
    {
        qDebug() << "no errors, request";
    }

    void f1::readyRead()
    {
    qDebug() << "ready read!";

    save=new QFile("plugins.txt");

        if (!save->open(QIODevice::WriteOnly))
            return;

        save->write(reply->readAll());




    }

    void f1::updateDataTransferProgress(qint64 done, qint64 total)
    {
        qDebug() << "updateDataTransferProgress started";
        ui->progressBar->setMaximum(100);
        ui->progressBar->setValue(done*100/total);


    }
Was it helpful?

Solution

QNetworkReply is a sequential-access QIODevice in which whenever more data is received from the network, the readyRead() signal is emitted. So your readyRead() slot will probably get called multiple times as new data comes gradually. So you should not initialize your file in that slot. The file initialization should be done in onDownServ() slot once:

QNetworkAccessManager *nam = new QNetworkAccessManager();
QUrl url2("ftp://test.cz/plugins.txt");
url2.setPassword("test");
url2.setUserName("test");


save=new QFile("plugins.txt");
if (!save->open(QIODevice::WriteOnly))
   return;

reply = nam->get(QNetworkRequest(url2));

When you do file initialization in readyRead() slot, it opens file in the first call and subsequent calls are returned as it can not open the new file for write operation. So the readyRead() slot gets called repeatedly and the application crashes.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top