Results 1 to 5 of 5

Thread: QNetworkAccessManager and QNetworkRequest not doing anything

  1. #1
    Join Date
    Sep 2013
    Posts
    8
    Thanks
    3

    Default QNetworkAccessManager and QNetworkRequest not doing anything

    I'm having trouble using QNetworkAccessManager to download anything, I create the QNetworkRequest and pass it to QNetworkAccessManager::get, but nothing happens, the finished signal never calls the function I connected (onFinished), and I even created a while loop after calling QNetworkAccessManager::get that checks QNetworkReply::isFinished every 500ms, but it always returns false and the progress is always 0 of 0. I called QNetworkReply::error which returns NoError but nothing seems to be working . I also tried disabling my firewall and antivirus in case they were blocking traffic but that didn't change anything, and the QWebView I'm also using in the app works fine so it shouldn't be a problem with connecting to the internet.

    Can anyone help me? I'm using Qt 5.1.1 and the url I'm using is just "http://google.com" which I figure shouldn't cause any problems.

    Below is the code for the class I created to test out how this stuff works and which I'm having problems with, please ignore the poor design of the code, it's just testing code to see how things work so I have a better idea how to fit it into the application.


    Class declaration:
    Qt Code:
    1. class FileDownloader : public QObject
    2. {
    3. Q_OBJECT
    4.  
    5. public:
    6. FileDownloader(QObject* parent = 0);
    7. ~FileDownloader();
    8.  
    9. void DownloadToFile(const QString& url, const QString& file);
    10.  
    11. public slots:
    12. void onFinished(QNetworkReply* reply);
    13.  
    14. private:
    15. QNetworkRequest* m_networkRequest;
    16. QNetworkAccessManager* m_networkManager;
    17. QNetworkReply* m_reply;
    18. QString m_file;
    19. };
    To copy to clipboard, switch view to plain text mode 

    Implementation:
    Qt Code:
    1. FileDownloader::FileDownloader(QObject* parent)
    2. : QObject(parent)
    3. {
    4. m_reply = NULL;
    5. m_networkManager = new QNetworkAccessManager(this);
    6. QObject::connect(m_networkManager, SIGNAL(finished(QNetworkReply*)), this, SLOT(onFinished(QNetworkReply*)));
    7. }
    8.  
    9. FileDownloader::~FileDownloader()
    10. {
    11. delete m_networkManager;
    12. }
    13.  
    14. void FileDownloader::DownloadToFile(const QString& url, const QString& file)
    15. {
    16. m_networkRequest = new QNetworkRequest(QUrl(url));
    17. m_reply = m_networkManager->get(*m_networkRequest);
    18. m_file = file;
    19.  
    20. while (m_reply->isFinished() == false)
    21. {
    22. Sleep(500);
    23.  
    24. char buffer[256];
    25. qint64 done = 0;
    26. qint64 total = 0;
    27. m_reply->downloadProgress(done, total);
    28. QNetworkReply::NetworkError error = m_reply->error();
    29. sprintf_s(buffer, 256, "Data: %d/%d\n%s\n", done, total, m_reply->errorString().toStdString().c_str());
    30. OutputDebugStringA(buffer);
    31. }
    32. }
    33.  
    34. void FileDownloader::onFinished(QNetworkReply* reply)
    35. {
    36. QFile outputFile = m_file;
    37. outputFile.open(QFile::WriteOnly);
    38. outputFile.write(reply->readAll());
    39. outputFile.close();
    40. }
    To copy to clipboard, switch view to plain text mode 

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: QNetworkAccessManager and QNetworkRequest not doing anything

    Quote Originally Posted by AusSkiller View Post
    and I even created a while loop after calling QNetworkAccessManager::get that checks QNetworkReply::isFinished every 500ms
    That's exactly why it doesn't work. You have to return to the event loop for any Qt networking classes to start their operations.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  3. #3
    Join Date
    Sep 2013
    Posts
    8
    Thanks
    3

    Default Re: QNetworkAccessManager and QNetworkRequest not doing anything

    Quote Originally Posted by wysota View Post
    That's exactly why it doesn't work. You have to return to the event loop for any Qt networking classes to start their operations.
    Originally it didn't have the while loop but I encountered the problem so I added it to try to figure out why it wasn't working. Commenting out the while loop doesn't change anything, it still doesn't work. Do I need to explicitly create an event loop though? I don't remember ever creating one but most of the examples I read didn't seem to create one either.


    Added after 22 minutes:


    Argh! Me and my silliness. I'm porting my app from using windows forms and managed C++/CLR to using straight C++ and Qt, and one of the differences is that the HttpWebRequest and HttpWebResponse stuff I was using before uses blocking calls, which was fine since I managed all the threads myself, but of course QNetworkAccessManager works asynchronously so I'd created the FileDownloader to make it easier to handle the async calls knowing that would be a problem. However in my rush to get it working I'd forgot to ensure the instance of the FileDownloader I was using would be persistent and instead just had it as a local variable which naturally fell out of scope and crapped up everything, changing it to static (just for testing purposes) makes it work properly.

    Except now I've encountered a different problem with it, it doesn't seem to follow redirects automatically , is there a simple way to enable that or will I need to handle it all myself?
    Last edited by AusSkiller; 1st October 2013 at 07:45.

  4. #4
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: QNetworkAccessManager and QNetworkRequest not doing anything

    As far as I know you need to handle that yourself.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


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

    AusSkiller (1st October 2013)

  6. #5
    Join Date
    Sep 2013
    Posts
    8
    Thanks
    3

    Default Re: QNetworkAccessManager and QNetworkRequest not doing anything

    Quote Originally Posted by wysota View Post
    As far as I know you need to handle that yourself.
    Cool thanks.

Similar Threads

  1. Replies: 2
    Last Post: 19th April 2013, 05:42
  2. QNetworkAccessManager.get(QNetworkRequest) not working.. :(
    By matthieunc in forum Qt Programming
    Replies: 4
    Last Post: 23rd July 2011, 13:18
  3. QNetworkRequest Proxy
    By Ricardo_arg in forum Qt Programming
    Replies: 2
    Last Post: 12th April 2011, 18:50
  4. QNetworkRequest atribute
    By mero in forum Qt Programming
    Replies: 23
    Last Post: 12th March 2011, 23:02
  5. Replies: 5
    Last Post: 20th January 2009, 14:11

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.