Results 1 to 4 of 4

Thread: Issue in downloading with QNetworkAccessManager

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Issue in downloading with QNetworkAccessManager

    You call Do_download() and immediately try to parse it.
    Since downloading something is not instantanious and you write the file after the download finished, there is no file to parse when you attempt to.

    I can recommend adding some qDebug() logging into the application when things like that happen, it would have told you that this is the sequence of how things currently happen.

    Cheers,
    _

  2. #2
    Join Date
    Feb 2015
    Posts
    21
    Thanks
    2

    Default Re: Issue in downloading with QNetworkAccessManager

    I can't seem to get this to work.

    .h

    Qt Code:
    1. #ifndef DOWNLOADER_H
    2. #define DOWNLOADER_H
    3.  
    4. #include <QObject>
    5. #include <QNetworkAccessManager>
    6. #include <QNetworkRequest>
    7. #include <QNetworkReply>
    8. #include <QUrl>
    9. #include <QFile>
    10. #include <QDebug>
    11. #include <QMessageBox>
    12. #include <string>
    13. #include <Windows.h>
    14. #include <qstring.h>
    15.  
    16. class downloader : public QObject
    17. {
    18. Q_OBJECT
    19. public:
    20. explicit downloader(QObject *parent = 0);
    21.  
    22. void Do_download();
    23.  
    24. std::string getExePath();
    25.  
    26. void Do_parse(bool download, QNetworkReply *reply);
    27.  
    28. public slots:
    29. void replyFinished (QNetworkReply *reply);
    30.  
    31. private:
    32. QNetworkAccessManager *manager;
    33. bool downloadSuccess;
    34.  
    35. };
    36.  
    37. #endif // DOWNLOADER_H
    To copy to clipboard, switch view to plain text mode 

    i added a bool variable, to check if download passed, and a Do_parse function to call afterwards.

    .cpp

    Qt Code:
    1. #include "downloader.h"
    2. #include "strings.h"
    3. #include <string>
    4. #include <Windows.h>
    5.  
    6. downloader::downloader(QObject *parent) :
    7. QObject(parent)
    8. {
    9. downloadSuccess = false;
    10. }
    11.  
    12.  
    13. void downloader::Do_download()
    14. {
    15. manager = new QNetworkAccessManager(this);
    16.  
    17. connect(manager, SIGNAL(finished(QNetworkReply*)),
    18. this,SLOT(replyFinished(QNetworkReply*)));
    19.  
    20. manager->get(QNetworkRequest(QUrl("http://www.hnb.hr/tecajn/hvazeca.htm")));
    21.  
    22. Do_parse(downloadSuccess, reply); // this doesn't work
    23. }
    24.  
    25. void downloader::replyFinished(QNetworkReply *reply)
    26. {
    27.  
    28. if(reply->error())
    29. {
    30. QMessageBox msgBox;
    31. msgBox.setText(strings::msgBoxDownReplyError);
    32. msgBox.exec();
    33. downloadSuccess = false;
    34. }
    35. else
    36. {
    37.  
    38. downloadSuccess = true;
    39.  
    40. }
    41. }
    42.  
    43. void downloader::Do_parse(bool download, QNetworkReply *reply)
    44. {
    45. if(download==true)
    46. {
    47. QFile *file = new QFile(strings::filePathQt);
    48. QByteArray data = reply->readAll();
    49.  
    50. if(file->open(QIODevice::ReadWrite | QIODevice::Truncate | QIODevice::Text))
    51. {
    52. file->write(data);
    53. file->flush();
    54. file->close();
    55. }
    56. delete file;
    57.  
    58. reply->deleteLater();
    59. }
    60.  
    61. }
    To copy to clipboard, switch view to plain text mode 

    I thought of something like this. but i guess i'm missing something about the reply thing.
    Am i on the right track here?
    Is it possible to send the reply into another function like i tried in my code?

  3. #3
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Issue in downloading with QNetworkAccessManager

    You schedule the download (QNAM::get()) and then call Do_parse, before the QNAM even had the chance to do anything.

    replyFinished() is obviously a place where code gets executed after the download finished, so there is where you need to call Do_parse.
    Not before the download happens.

    Cheers,
    _

Similar Threads

  1. QNetworkAccessManager proxy issue
    By dholliday in forum Qt Programming
    Replies: 12
    Last Post: 13th November 2017, 05:29
  2. Replies: 3
    Last Post: 27th June 2015, 04:00
  3. QWebPluginFactory::create issue with QNetworkAccessManager
    By brcontainer in forum Qt Programming
    Replies: 0
    Last Post: 15th December 2013, 19:32
  4. Downloading a page with QNetworkAccessManager
    By TempleClause in forum Qt Programming
    Replies: 2
    Last Post: 1st October 2012, 20:58
  5. Downloading multiple files using QNetworkAccessManager
    By aurorius in forum Qt Programming
    Replies: 3
    Last Post: 7th August 2009, 10:51

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.