Qt Forum

    • Login
    • Search
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search
    • Unsolved

    [SLOVED] qt 5.0.2 how to get the source of webpage ?

    General and Desktop
    2
    12
    5783
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • T
      ThElitEyeS last edited by

      Hello guys i want to know how i can get source code of webpage with out viewing the page just load the source code from page.
      For example i put http://google.com
      its give me the source code of http://google.com in a variable .

      Solution:
      in .pro add network

      @#include <QCoreApplication>
      #include <QDebug>
      #include <QtNetwork>

      int main(int argc, char *argv[])
      {
      QCoreApplication a(argc, argv);
      QNetworkAccessManager manager;
      QNetworkRequest request(QUrl("http://google.com"));
      QNetworkReply *reply(manager.get(request));
      QEventLoop loop;
      QObject::connect(reply, SIGNAL(finished()), &loop, SLOT(quit()));
      loop.exec();
      qDebug(reply->readAll());
      return a.exec();
      }@

      1 Reply Last reply Reply Quote 0
      • raven-worx
        raven-worx Moderators last edited by

        see "QNetworkAccessManager::get()":http://qt-project.org/doc/qt-5.0/qtnetwork/qnetworkaccessmanager.html#get

        @
        //QPointer<QNetworkReply> m_Reply;
        m_Reply = nam->get( QNetworkRequest( QUrl("http://google.com") );
        connect( reply, SIGNAL(finished()), this, SLOT(onRequestFinished()) );

        ....

        void onRequestFinished()
        {
        if( ! m_Reply )
        return;

        if( m_Reply->error() == QNetworkReply::NoError )
        {
            QString sourceCode( m_Reply->readAll() );
            ...
        }
        m_Reply->deleteLater();
        

        }
        @

        --- SUPPORT REQUESTS VIA CHAT WILL BE IGNORED ---
        If you have a question please use the forum so others can benefit from the solution in the future

        1 Reply Last reply Reply Quote 0
        • T
          ThElitEyeS last edited by

          !http://s21.postimg.org/4w93kt3ev/Screenshot_from_2013_07_01_11_24_11.png(error)!

          1 Reply Last reply Reply Quote 0
          • raven-worx
            raven-worx Moderators last edited by

            well ... you're not using QNam::get() as i stated. Also see the docs.
            The error message is also pretty obvious - which already provides the solution...

            --- SUPPORT REQUESTS VIA CHAT WILL BE IGNORED ---
            If you have a question please use the forum so others can benefit from the solution in the future

            1 Reply Last reply Reply Quote 0
            • T
              ThElitEyeS last edited by

              Can you write full example for me please?
              because i am really new in qt network

              1 Reply Last reply Reply Quote 0
              • raven-worx
                raven-worx Moderators last edited by

                i think i already did ?!!
                Just read the complete error message and compare my get() call with yours... if you don't see a difference ... well ...

                --- SUPPORT REQUESTS VIA CHAT WILL BE IGNORED ---
                If you have a question please use the forum so others can benefit from the solution in the future

                1 Reply Last reply Reply Quote 0
                • T
                  ThElitEyeS last edited by

                  Where i should put
                  void onRequestFinished?
                  @
                  ui->setupUi(this);
                  QNetworkAccessManager *manager = new QNetworkAccessManager(this);
                  QPointer<QNetworkReply> reply;
                  reply = manager->get(QNetworkRequest(QUrl("http://qt-project.org")));
                  connect(reply, SIGNAL(finished()), this, SLOT(onRequestFinished()));
                  void onRequestFinished() {

                  }
                  

                  @
                  returns:
                  In constructor 'MainWindow::MainWindow(QWidget*)':
                  error: a function-definition is not allowed here before '{' token

                  1 Reply Last reply Reply Quote 0
                  • raven-worx
                    raven-worx Moderators last edited by

                    this is a slot... so it is a member method of your class.
                    And also the QPointer<QNetworkReply> object should be a member then to identify the reply in this slot.

                    Hope this clears it up:

                    In your header file:
                    @
                    class MyClass : public ...
                    {
                    public:
                    MyClass();

                    protected slots:
                    void onRequestFinished();

                    protected:
                    QPointer<QNetworkReply> m_Reply;
                    }
                    @

                    In your source file:
                    @
                    MyClass::MyClass()
                    {
                    m_Reply = nam->get( QNetworkRequest( QUrl("http://google.com") );
                    connect( reply, SIGNAL(finished()), this, SLOT(onRequestFinished()) );
                    }

                    void MyClass::onRequestFinished()
                    {
                    if( ! m_Reply )
                    return;

                    if( m_Reply->error() == QNetworkReply::NoError )
                    {
                        QString sourceCode( m_Reply->readAll() );
                        ...
                    }
                    m_Reply->deleteLater();
                    

                    }
                    @

                    --- SUPPORT REQUESTS VIA CHAT WILL BE IGNORED ---
                    If you have a question please use the forum so others can benefit from the solution in the future

                    1 Reply Last reply Reply Quote 0
                    • T
                      ThElitEyeS last edited by

                      I am completely lost

                      1 Reply Last reply Reply Quote 0
                      • raven-worx
                        raven-worx Moderators last edited by

                        [quote author="ThElitEyeS" date="1372673232"]I am completely lost[/quote]
                        sorry..but so am i... if thats all you say.

                        I'm not able to help you, except you tell me what exactly you don't understand.
                        The code i've posted is a complete example in usage of QNetworkAccessManager::get(), only your own code needs to be integrated into my code.

                        --- SUPPORT REQUESTS VIA CHAT WILL BE IGNORED ---
                        If you have a question please use the forum so others can benefit from the solution in the future

                        1 Reply Last reply Reply Quote 0
                        • T
                          ThElitEyeS last edited by

                          Is that right ?
                          !http://s15.postimg.org/tkdarufvf/Screenshot_from_2013_07_02_23_00_40.png(image)!
                          *Also how i can check if reply is finished in console application and how i can qDebug the code ?
                          *if i try
                          @qDebug << code; it will give me a error@

                          1 Reply Last reply Reply Quote 0
                          • T
                            ThElitEyeS last edited by

                            i think i got it working.
                            thank you raven for the big help you showed :)
                            !http://s21.postimg.org/8l395k8av/Screenshot_from_2013_07_03_00_15_57.png(a)!

                            1 Reply Last reply Reply Quote 0
                            • First post
                              Last post