Results 1 to 4 of 4

Thread: issue in downloading using QNetworkAccessManager

  1. #1
    Join Date
    Feb 2015
    Posts
    185
    Thanks
    5
    Qt products
    Qt5
    Platforms
    MacOS X Windows

    Default issue in downloading using QNetworkAccessManager

    I am trying to download from an url using QNetworkAccessManager.

    Qt Code:
    1. void qmlinterface::onSyncBooks(QString isbn,QString chapNo)
    2. {
    3. QNetworkAccessManager *manager = new QNetworkAccessManager(this);
    4. connect(manager, SIGNAL(finished(QNetworkReply*)), this, SLOT(testDownload(QNetworkReply*)));
    5. QUrl serviceUrl = QUrl("http://172.16.14.41/mod_open1/download_quiz.php");
    6.  
    7. QByteArray postData;
    8. postData.append("isbn="+isbn);
    9. postData.append("&user_mail="+userName);
    10. postData.append("&section_id="+chapNo);
    11.  
    12. QNetworkRequest request(serviceUrl);
    13. manager->post(request, postData);
    14. }
    15. void qmlinterface::testDownload(QNetworkReply *pReply)
    16. {
    17. qDebug()<<"error : "<<pReply->errorString();
    18. downloadedData = pReply->readAll();
    19. qDebug()<<"downloadedData size : "<<downloadedData.size();
    20. pReply->deleteLater();
    21. addSyncDetailsToBookPath();
    22.  
    23. }
    To copy to clipboard, switch view to plain text mode 

    Got the response but before the response is completed fully, the control executes the next command
    Last edited by ejoshva; 26th June 2015 at 13:51.

  2. #2
    Join Date
    Nov 2007
    Posts
    55
    Thanks
    1
    Thanked 9 Times in 9 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: issue in downloading using QNetworkAccessManager

    you do not upload the data, so you'll never get the signal finished().

    You must add something like:

    Qt Code:
    1. connect( mpReply, SIGNAL( readyRead() ), SLOT( downloadReadyRead() )); // trigger data download
    2. mpReply = manager.get( request );
    3. ...
    4.  
    5. void qmlinterface::downloadReadyRead()
    6. {
    7. mBuffer += mpReply->readAll);
    8. //qDebug() << "mpBuffer size =" << mpBuffer->size();
    9. }
    To copy to clipboard, switch view to plain text mode 

    in class declaration add:
    Qt Code:
    1. private slots:
    2. void downloadReadyRead();
    3.  
    4. private:
    5. QNetworkReply* mpReply;
    6. QByteArray mBuffer
    To copy to clipboard, switch view to plain text mode 

    Notice that this approach would probably not work correctly if you are sending several requests.
    You should use then a queue to store the requests and working down the queue one after the other.

  3. #3
    Join Date
    Nov 2007
    Posts
    55
    Thanks
    1
    Thanked 9 Times in 9 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: issue in downloading using QNetworkAccessManager

    removed... coming tomorow
    Last edited by alainstgt; 27th June 2015 at 01:43.

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

    Default Re: issue in downloading using QNetworkAccessManager

    Quote Originally Posted by ejoshva View Post
    Got the response but before the response is completed fully, the control executes the next command
    You are not clear what "next command" is.

    The network post operation is asynchronous and your application will continue functioning while the network operation is carried out and response is returned. This is expected behaviour. If you need something to happen after the response is received then you should do it/trigger it from the finished() handler (after checking that it finished successfully).

Similar Threads

  1. QNetworkAccessManager proxy issue
    By dholliday in forum Qt Programming
    Replies: 12
    Last Post: 13th November 2017, 05:29
  2. QWebPluginFactory::create issue with QNetworkAccessManager
    By brcontainer in forum Qt Programming
    Replies: 0
    Last Post: 15th December 2013, 19:32
  3. Downloading a page with QNetworkAccessManager
    By TempleClause in forum Qt Programming
    Replies: 2
    Last Post: 1st October 2012, 20:58
  4. Downloading files
    By MTK358 in forum Newbie
    Replies: 3
    Last Post: 23rd June 2010, 00:08
  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.