PDA

View Full Version : QNetworkAccessManager doesn't work



Floppy
13th November 2009, 19:53
Hello all together,
at first: Sorry for my bad English :o

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:

void Downloader::StartDownload(QString &url)
{
QNetworkAccessManager *manager = new QNetworkAccessManager();
QEventLoop loop;
QNetworkReply *reply = manager->get(QNetworkRequest(QUrl("http://qt.nokia.com"))); //Just an example
connect(reply, SIGNAL(finished()), &loop, SLOT(quit()));

loop.exec();

//Save the received data to file
QFile file("C:\\data.dat");
file.open(QIODevice::WriteOnly);
file.write(reply->readAll());
}

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!

squidge
13th November 2009, 20:26
What is the code for quit slot?

Secondly, Qt uses / for dir seperator, not \

Floppy
14th November 2009, 09:28
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!

squidge
14th November 2009, 12:09
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.

Floppy
14th November 2009, 13:07
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

wysota
14th November 2009, 13:52
Please provide a minimal compilable example reproducing the problem.

Floppy
14th November 2009, 15:34
Hm okay, I tried it in a new project with the same code as above.
Now it works :eek:

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

squidge
14th November 2009, 16:32
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.