Results 1 to 8 of 8

Thread: QNetworkAccessManager doesn't work

  1. #1
    Join Date
    Nov 2009
    Posts
    4
    Thanks
    2
    Qt products
    Qt4
    Platforms
    Windows

    Red face QNetworkAccessManager doesn't work

    Hello all together,
    at first: Sorry for my bad English

    Now my problem:
    I'm trying to download a file from the web using QNetworkAccessManager. Here you can see a snippet out of my code:
    Qt Code:
    1. void Downloader::StartDownload(QString &url)
    2. {
    3. QNetworkAccessManager *manager = new QNetworkAccessManager();
    4. QNetworkReply *reply = manager->get(QNetworkRequest(QUrl("http://qt.nokia.com"))); //Just an example
    5. connect(reply, SIGNAL(finished()), &loop, SLOT(quit()));
    6.  
    7. loop.exec();
    8.  
    9. //Save the received data to file
    10. QFile file("C:\\data.dat");
    11. file.open(QIODevice::WriteOnly);
    12. file.write(reply->readAll());
    13. }
    To copy to clipboard, switch view to plain text mode 

    This all is started in a new QThread. It seems as if the QEventLoop is never left, because no file is created on C:
    Maybe you can tell me where I made a fault
    Thank you!

  2. #2
    Join Date
    Sep 2009
    Location
    UK
    Posts
    2,447
    Thanks
    6
    Thanked 348 Times in 333 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: QNetworkAccessManager doesn't work

    What is the code for quit slot?

    Secondly, Qt uses / for dir seperator, not \

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

    Floppy (14th November 2009)

  4. #3
    Join Date
    Nov 2009
    Posts
    4
    Thanks
    2
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: QNetworkAccessManager doesn't work

    I used this Quit-Slot:
    http://doc.trolltech.com/4.5/qeventloop.html#quit

    It doesn't change anything if i change the "\\" to "/"

    Thanks for your answer!

  5. #4
    Join Date
    Sep 2009
    Location
    UK
    Posts
    2,447
    Thanks
    6
    Thanked 348 Times in 333 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: QNetworkAccessManager doesn't work

    as yes, so you do. I forgot quit was a slot in QEventLoop.

    Using '/' instead of '\\' is easier for you (less typing, less change of mistake of putting only one backslash) and means your code is more portable across platforms which do use '/' seperator such as Linux. If the platform using another seperator, Qt can simply convert it for you if you use a '/'.

    Have you tried using QNetworkAccessManager::finished instead of QNetworkReply::finished? I know they should be emitted in tandem, but its worth a shot, surely.

    Also, check the output window for any diagnostic messages. Thats helped me before.

  6. #5
    Join Date
    Nov 2009
    Posts
    4
    Thanks
    2
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: QNetworkAccessManager doesn't work

    Okay, I tried the QNetworkAccessManager::finished() signal but it also didn't work....
    What do you mean with
    Also, check the output window for any diagnostic messages. Thats helped me before.
    ?

    Thank you

  7. #6
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,364
    Thanks
    3
    Thanked 5,014 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 doesn't work

    Please provide a minimal compilable example reproducing the problem.
    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.


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

    Floppy (14th November 2009)

  9. #7
    Join Date
    Nov 2009
    Posts
    4
    Thanks
    2
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: QNetworkAccessManager doesn't work

    Hm okay, I tried it in a new project with the same code as above.
    Now it works

    I will try to find the reason and post it here.
    Thank you all!

  10. #8
    Join Date
    Sep 2009
    Location
    UK
    Posts
    2,447
    Thanks
    6
    Thanked 348 Times in 333 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: QNetworkAccessManager doesn't work

    In debug mode, diagnostic messages are sent via stderr. For example, if you attempt to connect a signal to a non-existant slot, you'll get a message at runtime stating that rather than scratching your head wondering why something doesn't work.

Similar Threads

  1. Making MySQL plugin work on a windows x86 enviroment
    By Baasie in forum Installation and Deployment
    Replies: 1
    Last Post: 2nd September 2009, 15:15
  2. getting MySQL to work with Qt
    By Ashish in forum Installation and Deployment
    Replies: 7
    Last Post: 19th May 2009, 08:57
  3. Qt4 : QPainter::setRedirected doesn't work
    By Ankitha Varsha in forum Qt Programming
    Replies: 2
    Last Post: 20th June 2008, 17:52
  4. QActions don't work with menubar hidden
    By Pepe in forum Qt Programming
    Replies: 1
    Last Post: 16th August 2007, 01:04
  5. Change work area OS
    By pakulo in forum Qt Programming
    Replies: 15
    Last Post: 15th May 2007, 07:20

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.