Results 1 to 4 of 4

Thread: Download the source code of a webpage using the QNetworkAccessManager

  1. #1
    Join Date
    Feb 2010
    Posts
    61
    Thanks
    18
    Qt products
    Qt4
    Platforms
    MacOS X

    Default Download the source code of a webpage using the QNetworkAccessManager

    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 . 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

  2. #2
    Join Date
    Oct 2006
    Location
    New Delhi, India
    Posts
    2,467
    Thanks
    8
    Thanked 334 Times in 317 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Download the source code of a webpage using the QNetworkAccessManager

    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 -
    Qt Code:
    1. main()
    2. {
    3. QMainWindow window; // or any other window
    4. window.show();
    5.  
    6. return app.exec();
    7. }
    To copy to clipboard, switch view to plain text mode 

  3. #3
    Join Date
    Sep 2009
    Posts
    72
    Thanked 10 Times in 10 Posts
    Qt products
    Qt3 Qt4 Qt/Embedded Qt Jambi
    Platforms
    Unix/X11 Windows

    Default Re: Download the source code of a webpage using the QNetworkAccessManager

    Hi

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

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

  4. #4
    Join Date
    Feb 2010
    Posts
    61
    Thanks
    18
    Qt products
    Qt4
    Platforms
    MacOS X

    Default Re: Download the source code of a webpage using the QNetworkAccessManager

    You are right aamer4yu ! These lines were commented...

    It works perfectly now. Thank you so much !

Similar Threads

  1. Where is the source code?
    By Fletcher in forum Newbie
    Replies: 1
    Last Post: 10th December 2009, 20:45
  2. Download Source Code
    By psih128 in forum Newbie
    Replies: 3
    Last Post: 6th July 2009, 10:01
  3. Source code
    By afflictedd2 in forum Qt Programming
    Replies: 3
    Last Post: 4th November 2008, 09:04
  4. source code
    By Colx007 in forum Newbie
    Replies: 5
    Last Post: 19th December 2007, 09:15
  5. Qte source code
    By Gaurav vyas in forum Qt for Embedded and Mobile
    Replies: 1
    Last Post: 1st July 2007, 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.