Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. [SLOVED] qt 5.0.2 how to get the source of webpage ?
QtWS25 Last Chance

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

Scheduled Pinned Locked Moved General and Desktop
12 Posts 2 Posters 6.3k Views
  • 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 Offline
    T Offline
    ThElitEyeS
    wrote on last edited by
    #1

    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
    0
    • raven-worxR Offline
      raven-worxR Offline
      raven-worx
      Moderators
      wrote on last edited by
      #2

      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
      0
      • T Offline
        T Offline
        ThElitEyeS
        wrote on last edited by
        #3

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

        1 Reply Last reply
        0
        • raven-worxR Offline
          raven-worxR Offline
          raven-worx
          Moderators
          wrote on last edited by
          #4

          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
          0
          • T Offline
            T Offline
            ThElitEyeS
            wrote on last edited by
            #5

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

            1 Reply Last reply
            0
            • raven-worxR Offline
              raven-worxR Offline
              raven-worx
              Moderators
              wrote on last edited by
              #6

              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
              0
              • T Offline
                T Offline
                ThElitEyeS
                wrote on last edited by
                #7

                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
                0
                • raven-worxR Offline
                  raven-worxR Offline
                  raven-worx
                  Moderators
                  wrote on last edited by
                  #8

                  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
                  0
                  • T Offline
                    T Offline
                    ThElitEyeS
                    wrote on last edited by
                    #9

                    I am completely lost

                    1 Reply Last reply
                    0
                    • raven-worxR Offline
                      raven-worxR Offline
                      raven-worx
                      Moderators
                      wrote on last edited by
                      #10

                      [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
                      0
                      • T Offline
                        T Offline
                        ThElitEyeS
                        wrote on last edited by
                        #11

                        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
                        0
                        • T Offline
                          T Offline
                          ThElitEyeS
                          wrote on last edited by
                          #12

                          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
                          0

                          • Login

                          • Login or register to search.
                          • First post
                            Last post
                          0
                          • Categories
                          • Recent
                          • Tags
                          • Popular
                          • Users
                          • Groups
                          • Search
                          • Get Qt Extensions
                          • Unsolved