Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Simplest file retrieval
Forum Updated to NodeBB v4.3 + New Features

Simplest file retrieval

Scheduled Pinned Locked Moved General and Desktop
http get
5 Posts 4 Posters 1.7k Views 4 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • F Offline
    F Offline
    floatingWoods
    wrote on last edited by
    #1

    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?

    1 Reply Last reply
    0
    • p3c0P Offline
      p3c0P Offline
      p3c0
      Moderators
      wrote on last edited by
      #2

      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.

      157

      1 Reply Last reply
      1
      • SGaistS Offline
        SGaistS Offline
        SGaist
        Lifetime Qt Champion
        wrote on last edited by
        #3

        Hi,

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

        Interested in AI ? www.idiap.ch
        Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

        1 Reply Last reply
        1
        • ? Offline
          ? Offline
          A Former User
          wrote on last edited by
          #4

          @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>
          
          1 Reply Last reply
          1
          • F Offline
            F Offline
            floatingWoods
            wrote on last edited by
            #5

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

            1 Reply Last reply
            0

            • Login

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • Users
            • Groups
            • Search
            • Get Qt Extensions
            • Unsolved