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. Very simple C++ question - synchronous file download
QtWS25 Last Chance

Very simple C++ question - synchronous file download

Scheduled Pinned Locked Moved General and Desktop
3 Posts 3 Posters 3.4k Views
  • 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 Offline
    S Offline
    SumRandomGuy
    wrote on last edited by
    #1

    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
    0
    • L Offline
      L Offline
      leon.anavi
      wrote on last edited by
      #2

      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
      0
      • C Offline
        C Offline
        Code_ReaQtor
        wrote on last edited by
        #3

        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
        0

        • Login

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