Simplest file retrieval



  • Hello,

    I am trying to retrieve a file online, using http get. I based my code of the example that comes with Qt, but it is not retrieving anything. Signal "finished" or "readyRead" are never fired. I feel an "exec" or "processEvents" is missing somewhere. Following is the code (the slots "finished" and "readyRead" have been omitted, but they are never fired):

    HttpDownload::HttpDownload()
    {
    }
    
    void HttpDownload::startRequest(QUrl url)
    {
        printf("startRequest!!\n");
        reply = qnam.get(QNetworkRequest(url));
        connect(reply, SIGNAL(finished()),this, SLOT(httpFinished()));
        connect(reply, SIGNAL(readyRead()),this, SLOT(httpReadyRead()));
    }
    
    void HttpDownload::downloadFile(const std::string& fileUrl)
    {
        printf("downloadFile!!\n");
        url = QString(fileUrl.c_str());
        QFileInfo fileInfo(url.path());
        QString fileName = fileInfo.fileName();
        if (QFile::exists(fileName))
            QFile::remove(fileName);
        file=new QFile(fileName);
        if (!file->open(QIODevice::WriteOnly))
        {
            delete file;
            file=0;
            return;
        }
        startRequest(url);
    }
    

    Following is the header:

    class HttpDownload : public QObject
    {
        Q_OBJECT
    public:
        HttpDownload();
        void startRequest(QUrl url);
        void downloadFile(const std::string& fileUrl);
    private slots:
        void httpFinished();
        void httpReadyRead();
    private:
        QUrl url;
        QNetworkAccessManager qnam;
        QNetworkReply *reply;
        QFile *file;
    };
    

    And this is how I test it for now:

    HttpDownload* download=new HttpDownload();
    download->downloadFile("http://www.awebsite.com/aFile.htm");
    Sleep(10000);    
    

    It prints out:

    downloadFile!!
    startRequest!!

    And that's it. What am I doing wrong?


  • Moderators

    Hi,

    From the Docs:

    QNetworkAccessManager *manager = new QNetworkAccessManager(this);
    connect(manager, SIGNAL(finished(QNetworkReply*)),
            this, SLOT(replyFinished(QNetworkReply*)));
    
    manager->get(QNetworkRequest(QUrl("http://qt-project.org")));
    

    QNetworkAccessManager has an asynchronous API. When the replyFinished slot above is called, the parameter it takes is the QNetworkReply object containing the downloaded data as well as meta-data (headers, etc.).

    Use replyFinished to get the data. Check QNetworkReply for more details.


  • Lifetime Qt Champion

    Hi,

    To add to @p3c0, you don't do any check for potential errors


  • Moderators

    @floatingWoods said:

    HttpDownload* download=new HttpDownload();
    download->downloadFile("http://www.awebsite.com/aFile.htm");
    Sleep(10000);

    You're blocking the event loop for 10 seconds. Why? If you want to explicitly make the event loop to process events (here maybe after HttpDownload instantiation or before sleep) you can do this:

    qApp->processEvents() // #include <QCoreApplication>
    


  • THanks to the 3 of you, that works fine!!


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.