Qt Forum

    • Login
    • Search
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search
    • Unsolved

    Update: Forum Guidelines & Code of Conduct

    Very simple C++ question - synchronous file download

    General and Desktop
    3
    3
    3155
    Loading More Posts
    • 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.
    • S
      SumRandomGuy last edited by

      I've got this very simple FileDownloader class. What would be the best way to make my program pause while the download is happening? It's downloading a configuration file and I'd rather it wait for the download to complete.

      Thanks!

      @#include "filedownloader.h"

      FileDownloader::FileDownloader(QObject *parent) :
      QObject(parent)
      {
      http = new QHttp(this);
      connect(http, SIGNAL(requestFinished(int,bool)), this, SLOT(requestFinished(int,bool)));

      downloadComplete = false;
      downloadStatus = NOTHING;
      

      }

      void FileDownloader::startDownload()
      {
      if (Host.isNull() || LocalFile.isNull() || RemoteFile.isNull())
      return;

      http->setHost(Host);
      http->get(RemoteFile);
      downloadComplete = false;
      downloadStatus = INPROGRESS;
      

      }

      bool FileDownloader::isDownloadComplete()
      {
      return downloadComplete;
      }

      void FileDownloader::requestFinished(int id, bool error)
      {
      if (error) {
      qDebug() << "Error: ";
      downloadStatus = ERROR;

      } else {
          qDebug() << "OK";
          QFile *DestFile = new QFile&#40;LocalFile&#41;;
      
          qDebug() << "Writing File";
          if (DestFile->open(QFile::Append)) {
              DestFile->write(http->readAll());
              DestFile->flush();
              DestFile->close();
          }
      
          delete DestFile;
          downloadComplete = true;
          downloadStatus   = SUCCESS;
      }
      

      }@

      1 Reply Last reply Reply Quote 0
      • L
        leon.anavi last edited by

        Depending the user interface of your application you can disable all controls and just set a loading dialog until the download is completed. Btw this is a simple example "how to download data from URL":http://qt-project.org/wiki/Download_Data_from_URL that you might find useful.

        http://anavi.org/

        1 Reply Last reply Reply Quote 0
        • C
          Code_ReaQtor last edited by

          Having the signal-slot implementation early on the contructor class is wrong as it will act as asynchronous. You may use QEventLoop to make things synchrous while making your GUI (for example) responsive.

          @http->setHost(Host);
          http->get(RemoteFile);
          QEventLoop loop;
          QObject::connect(reply,SIGNAL(readyRead()),&loop,SLOT(quit()));
          loop.exec();
          //download here
          http->readAll();@

          Why dont you use QNetworkAccessManager by the way? QHttp is already obsolete.

          From QHttp documentation:

          bq. This class is obsolete. It is provided to keep old source code working. We strongly advise against using it in new code.

          Please visit my open-source projects at https://github.com/Code-ReaQtor.

          1 Reply Last reply Reply Quote 0
          • First post
            Last post