Results 1 to 5 of 5

Thread: Proper file downloading?

  1. #1
    Join Date
    May 2014
    Posts
    6
    Thanks
    2
    Qt products
    Qt5
    Platforms
    Unix/X11 Windows

    Question Proper file downloading?

    Currently in a project I'm working on I'm using a slightly modified version of this (emits fileSaved() and takes a save path) to download files like so:

    Qt Code:
    1. QEventLoop imageLoop;
    2. QDownloader *downloader = new QDownloader();
    3. connect(downloader, SIGNAL(fileSaved()), &imageLoop, SLOT(quit()));
    4.  
    5. for(int i = 0; i < imageURLs.length(); i++) {
    6. progressDialog->setValue((int)(100 * (double)i / imageURLs.length()));
    7. progressDialog->setLabelText("Downloading image: " + QString::number(i + 1) + "/" + QString::number(imageURLs.length()));
    8.  
    9. if(progressDialog->wasCanceled())
    10. break;
    11.  
    12. downloader->setFile(imageURLs.at(i), QDir::homePath() + "/images/" + imageNames.at(i) + ".jpg");
    13. imageLoop.exec();
    14. }
    To copy to clipboard, switch view to plain text mode 

    The program crashes sometimes doing this and I was wondering what I am doing wrong or what is a more common or appropriate way to accomplish this.

    Thanks

  2. #2
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: Proper file downloading?

    Run your program in your debugger and when it crashes inspect the stack back trace to determine where, and usually why, it has crashed.

  3. #3
    Join Date
    May 2014
    Posts
    6
    Thanks
    2
    Qt products
    Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Proper file downloading?

    Quote Originally Posted by ChrisW67 View Post
    Run your program in your debugger and when it crashes inspect the stack back trace to determine where, and usually why, it has crashed.
    It looks like it's crashing on

    Qt Code:
    1. void QDownloader::onReadyRead()
    2. {
    3. file->write(reply->readAll());
    4. }
    To copy to clipboard, switch view to plain text mode 

    So I added a quick check to isWritable() and it still end up crashing but file->isOpen() returns false.

    Eventually I determined that the issue was that the finished() signal kept firing before the final readyRead() so the file would close.

    I also ended up moving the

    Qt Code:
    1. connect(manager,SIGNAL(finished(QNetworkReply*)),this,SLOT(onFinished(QNetworkReply*)));
    To copy to clipboard, switch view to plain text mode 

    statement to the constructor because it was calling the slot an extra time for each file downloaded.

  4. #4
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: Proper file downloading?

    Looking at that code you should not need that connect() from the manager at all. You either connect to the QNetworkAccessManager::finished() signal or the QNetworkReply::finished() signal but not both. It's unlikely the reply's finished signal would come before the reply's last readyRead so I would use those.

  5. The following user says thank you to ChrisW67 for this useful post:

    matt4682 (2nd May 2014)

  6. #5
    Join Date
    May 2014
    Posts
    6
    Thanks
    2
    Qt products
    Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Proper file downloading?

    Thanks a bunch, it's running flawlessly!

Similar Threads

  1. Replies: 1
    Last Post: 27th July 2013, 23:57
  2. Downloading a file and saving in a path
    By StarRocks in forum Qt Programming
    Replies: 19
    Last Post: 3rd January 2013, 07:43
  3. Concurrent file downloading
    By Alir3z4 in forum Qt Programming
    Replies: 14
    Last Post: 21st February 2012, 16:56
  4. downloading the file problems
    By migel in forum Newbie
    Replies: 0
    Last Post: 7th June 2011, 18:30
  5. File size of a remote file without downloading it
    By dirkdepauw in forum Qt Programming
    Replies: 5
    Last Post: 4th November 2010, 10:48

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.