PDA

View Full Version : Download the source code of a webpage using the QNetworkAccessManager



ouekah
17th February 2010, 11:35
Hi,

I would like to download the source code of a webpage using the QNetworkAccessManager. So I read the documentation (http://doc.trolltech.com/4.6/qnetworkaccessmanager.html) and there's an example:

"...A simple download off the network could be accomplished with:

QNetworkAccessManager *manager = new QNetworkAccessManager(this);
connect(manager, SIGNAL(finished(QNetworkReply*)), this, SLOT(replyFinished(QNetworkReply*)));

manager->get(QNetworkRequest(QUrl("http://qt.nokia.com")));

When the replyFinished slot above is called, the parameter it takes is the QNetworkReply object containing the downloaded data as well as meta-data (headers, etc.)..."

I wrote the same example, but it looks like that replyFinished is never called :confused:. Why ?

Here is my code:

yahooRetriever.cpp
-----------------------------------------------------
#include "yahooRetriever.h"

YahooRetriever::YahooRetriever() {
std::cout << "yahooRetriever" << std::endl;
manager = new QNetworkAccessManager(this);
connect(manager, SIGNAL(finished(QNetworkReply*)), this, SLOT(result(QNetworkReply*)));
}

void YahooRetriever::test() {
std::cout << "start test" << std::endl;
manager->get(QNetworkRequest(QUrl("http://www.yahoo.com")));
std::cout << "end test" << std::endl;
}

void YahooRetriever::result(QNetworkReply *reply) {
std::cout << "loading complete" << std::endl;
}
-----------------------------------------------------


main.cpp
-----------------------------------------------------
#include "yahooRetriever.h"

int main(int argc, char *argv[]) {
YahooRetriever yahooRetriever;
yahooRetriever.test();
return 0;
}
-----------------------------------------------------


yahooRetriever.h
-----------------------------------------------------
#include <QtNetwork>
#include <iostream>

class YahooRetriever : QObject {
Q_OBJECT
public:
YahooRetriever();
void test();
private slots:
void result(QNetworkReply *reply);
private:
QNetworkAccessManager *manager;
-----------------------------------------------------

When I run my program, it displays the following:

yahooRetriever
start test
end test

but it should also diplay

loading complete

once the download is complete and it doesn't. It's like the finished signal is never emitted by the manager...

I'm using Qt 4.6 on OS X 10.6 and eclipse to code my project.

Could someone tell me what I do wrong ? I would really appreciate some help :)

aamer4yu
17th February 2010, 11:40
You didnt create the QApplication object, did you ?
The event processing is started in app.exec(). All Qt program's main function look something like this -

main()
{
QApplication app;
QMainWindow window; // or any other window
window.show();

return app.exec();
}

vishwajeet.dusane
17th February 2010, 12:23
Hi

Try this application from ur <QTDIR>\examples\network\http

This is the exact stuff which u r looking for i guess :)

ouekah
17th February 2010, 12:26
You are right aamer4yu ! These lines were commented...

It works perfectly now. Thank you so much !