Results 1 to 5 of 5

Thread: QNetworkAccessManager.get(QNetworkRequest) not working.. :(

  1. #1
    Join Date
    Jul 2011
    Posts
    6
    Thanks
    4
    Qt products
    Qt4
    Platforms
    Windows Symbian S60

    Default QNetworkAccessManager.get(QNetworkRequest) not working.. :(

    Hey there, I'm completely new to this forum! I'm coming here because I'm having an issue in my Qt code, and my beginner skills aren't helping me a lot...

    I'm trying to send a request to get the content of a tumblr blog, at "kauritree.tumblr.com/api/read". The file I should get is an XML file, so I created a class that is in charge of downloading the file, and transforming it in whatever I want. I just cant get the QNetworkAccessManager.get(request) to be working... and I dont know why. The GET request is not even sent (I checked using Wireshark)... Thank you for your help!

    here is my code:

    main.cpp:
    Qt Code:
    1. QUrl qUrl;
    2. qUrl.setUrl("http://kauritree.tumblr.com/api/read/");
    3. ListeXml listeXml;
    4. listeXml.downloadd(qUrl);
    To copy to clipboard, switch view to plain text mode 

    my ListeXml.cpp
    Qt Code:
    1. ListeXml::ListeXml(QObject *parent) :
    2. QObject(parent)
    3. {
    4. }
    5.  
    6. QString ListeXml::downloadd(QUrl url)
    7. {
    8. DownloadManager manager;
    9. manager.doDownload(url.toString());
    10. }
    To copy to clipboard, switch view to plain text mode 

    DownloadManager.cpp
    Qt Code:
    1. DownloadManager::DownloadManager() //Constructor
    2. {
    3. QObject::connect(&manager, SIGNAL(finished(QNetworkReply*)),SLOT(downloadFinished(QNetworkReply*)));
    4. }
    5.  
    6. void DownloadManager::doDownload(QString Url)
    7. {
    8. QUrl url;
    9. url.setUrl(Url);
    10. const QNetworkRequest request(url);
    11. QNetworkReply *reply = manager.get(request);
    12. }
    To copy to clipboard, switch view to plain text mode 

  2. #2
    Join Date
    Jan 2006
    Location
    Germany
    Posts
    4,380
    Thanks
    19
    Thanked 1,005 Times in 913 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Symbian S60
    Wiki edits
    5

    Default Re: QNetworkAccessManager.get(QNetworkRequest) not working.. :(

    Hi,

    it is because the get function of QNetworkAccessManager works asynchron. That means that DownloadManager::doDownload returns right after calling get, then ListeXml::downloadd returns and finally it proceed at listeXml.downloadd(qUrl); without Qt had had the chance to start the network request and all objects you create on the stack will be deleted, thus the request will never be sent.

  3. The following user says thank you to Lykurg for this useful post:

    matthieunc (28th July 2011)

  4. #3
    Join Date
    Jul 2011
    Posts
    6
    Thanks
    4
    Qt products
    Qt4
    Platforms
    Windows Symbian S60

    Default Re: QNetworkAccessManager.get(QNetworkRequest) not working.. :(

    Right after posting, I kept testing and I found a code that succeeded in downloading the file. The download instruction is in main.cpp, and it's working, so am I missing something about your explaination? here is the main.cpp that got it working (without changing anything else):

    main.cpp
    Qt Code:
    1. #include "mainwindow.h"
    2. #include "listexml.h"
    3.  
    4. #include <QDebug>
    5. #include <QtGui/QApplication>
    6.  
    7.  
    8. int main(int argc, char *argv[])
    9. {
    10. QApplication app(argc, argv);
    11.  
    12. DownloadManager manager;
    13. manager.doDownload(QString("http://symblr.tumblr.com/api/read/"));
    14.  
    15. return app.exec();
    16. }
    To copy to clipboard, switch view to plain text mode 

    However, I just copied the code from "ListeXml.cpp" to "main.cpp". So... why??
    Meanwhile, I'm going to try to code a signal/slot system that ends the doDownload function only when the file is downloaded.

  5. #4
    Join Date
    Jan 2006
    Location
    Germany
    Posts
    4,380
    Thanks
    19
    Thanked 1,005 Times in 913 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Symbian S60
    Wiki edits
    5

    Default Re: QNetworkAccessManager.get(QNetworkRequest) not working.. :(

    In main() it succeed because the manager keeps alive. In your further code it gets destroyed right after it was created. Make yourself familiar with the lifetime of objects and with stack/heap allocation.

  6. The following user says thank you to Lykurg for this useful post:

    matthieunc (28th July 2011)

  7. #5
    Join Date
    Jul 2011
    Posts
    6
    Thanks
    4
    Qt products
    Qt4
    Platforms
    Windows Symbian S60

    Default Re: QNetworkAccessManager.get(QNetworkRequest) not working.. :(

    I'll follow your advice! Thank you soooo much for your help!

Similar Threads

  1. QNetworkRequest Proxy
    By Ricardo_arg in forum Qt Programming
    Replies: 2
    Last Post: 12th April 2011, 19:50
  2. QNetworkRequest atribute
    By mero in forum Qt Programming
    Replies: 23
    Last Post: 13th March 2011, 00:02
  3. Replies: 0
    Last Post: 21st December 2009, 06:04
  4. QNetworkAccessManager::head not working
    By jonks in forum Qt Programming
    Replies: 4
    Last Post: 21st October 2009, 23:23
  5. Replies: 5
    Last Post: 20th January 2009, 15:11

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.