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. QWebview not available in QT creator

QWebview not available in QT creator

Scheduled Pinned Locked Moved Unsolved General and Desktop
12 Posts 3 Posters 3.1k 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.
  • G Offline
    G Offline
    GCDX
    wrote on last edited by
    #1

    Hi everyone,

    I'm trying to do DOM traversal so that i can get information i want from HTML code. I've found a solution that says you need QWebView from QT creator. However, my Qt creator does not have QWebView in the design portion. I've googled but yet to find any solution can someone help me?

    JonBJ 1 Reply Last reply
    0
    • G GCDX

      Hi everyone,

      I'm trying to do DOM traversal so that i can get information i want from HTML code. I've found a solution that says you need QWebView from QT creator. However, my Qt creator does not have QWebView in the design portion. I've googled but yet to find any solution can someone help me?

      JonBJ Offline
      JonBJ Offline
      JonB
      wrote on last edited by JonB
      #2

      @GCDX
      Independent of anything you might or might not have available in Qt Creator (I don't use it, so I don't know), what version of Qt are you using? Because QWebView was available in Qt 4 plus Qt 5 up to 5.5. But at Qt 5.6 it was deprecated, as Qt moved from WebKit to WebEngine. WebKit/QWebView is no longer part of the "standard" Qt build, you have to take special action to create it.

      Whichever one you choose, if you only want "to do DOM traversal so that i can get information i want from HTML code", you can always create an instance of whichever dynamically at run-time instead of statically at design-time and do your work. Especially no need to create it in Qt Creator if you don't intend to present it visually to the end-user.

      1 Reply Last reply
      0
      • G Offline
        G Offline
        GCDX
        wrote on last edited by
        #3

        Hi @JonB , and thank you your reply!

        Could you elaborate on " you can always create an instance of whichever dynamically at run-time instead of statically at design-time and do your work. Especially no need to create it in Qt Creator if you don't intend to present it visually to the end-user."?
        Sorry , i'm rather new to qt, so i don't really know how to solve this. I tried using webview in qt 5.0.2 but i got stuck. Here is what i tried!0_1528447014050_cbf95a52-4533-418c-820a-9d6814de8c38-image.png

        The result is that i can't seem to find out what's wrong with this code.

        JonBJ jsulmJ 2 Replies Last reply
        0
        • G GCDX

          Hi @JonB , and thank you your reply!

          Could you elaborate on " you can always create an instance of whichever dynamically at run-time instead of statically at design-time and do your work. Especially no need to create it in Qt Creator if you don't intend to present it visually to the end-user."?
          Sorry , i'm rather new to qt, so i don't really know how to solve this. I tried using webview in qt 5.0.2 but i got stuck. Here is what i tried!0_1528447014050_cbf95a52-4533-418c-820a-9d6814de8c38-image.png

          The result is that i can't seem to find out what's wrong with this code.

          JonBJ Offline
          JonBJ Offline
          JonB
          wrote on last edited by
          #4

          @GCDX
          I don't really know what you're asking now.

          • You are already creating it dynamically with your QWebView *innerPage = new QWebView(). So you are managing to create one without having had to use anything in Qt Creator to achieve/view it, aren't you?

          • Are you asking about the underline on body in your code, or the LEAKs on exit?

          • Are you asking about why you get empty strings from your qDebug() statements?

          • Or.... ?

          G 1 Reply Last reply
          0
          • G GCDX

            Hi @JonB , and thank you your reply!

            Could you elaborate on " you can always create an instance of whichever dynamically at run-time instead of statically at design-time and do your work. Especially no need to create it in Qt Creator if you don't intend to present it visually to the end-user."?
            Sorry , i'm rather new to qt, so i don't really know how to solve this. I tried using webview in qt 5.0.2 but i got stuck. Here is what i tried!0_1528447014050_cbf95a52-4533-418c-820a-9d6814de8c38-image.png

            The result is that i can't seem to find out what's wrong with this code.

            jsulmJ Online
            jsulmJ Online
            jsulm
            Lifetime Qt Champion
            wrote on last edited by
            #5

            @GCDX If this code compiles then you have QWebView. Are you sure you are able to fetch HTML from that URL?

            https://forum.qt.io/topic/113070/qt-code-of-conduct

            G 1 Reply Last reply
            0
            • JonBJ JonB

              @GCDX
              I don't really know what you're asking now.

              • You are already creating it dynamically with your QWebView *innerPage = new QWebView(). So you are managing to create one without having had to use anything in Qt Creator to achieve/view it, aren't you?

              • Are you asking about the underline on body in your code, or the LEAKs on exit?

              • Are you asking about why you get empty strings from your qDebug() statements?

              • Or.... ?

              G Offline
              G Offline
              GCDX
              wrote on last edited by
              #6

              @JonB

              Yes i created a WebView but in the design UI it doesn't have the function for me to put inside the QML code for design.

              I am trying to parse out the strings HelloWorld and TESTCODE. However, when i try to store the strings using the toPlainText() and print it out as using QDebug, it prints an empty string, so i dont know what is wrong with the code.

              YES! Why do i get empty strings??

              JonBJ 1 Reply Last reply
              0
              • jsulmJ jsulm

                @GCDX If this code compiles then you have QWebView. Are you sure you are able to fetch HTML from that URL?

                G Offline
                G Offline
                GCDX
                wrote on last edited by
                #7

                @jsulm

                How to check if i can fetch HTML from the URL? I can't just fetch from any website online?

                1 Reply Last reply
                0
                • G GCDX

                  @JonB

                  Yes i created a WebView but in the design UI it doesn't have the function for me to put inside the QML code for design.

                  I am trying to parse out the strings HelloWorld and TESTCODE. However, when i try to store the strings using the toPlainText() and print it out as using QDebug, it prints an empty string, so i dont know what is wrong with the code.

                  YES! Why do i get empty strings??

                  JonBJ Offline
                  JonBJ Offline
                  JonB
                  wrote on last edited by JonB
                  #8

                  @GCDX

                  Yes i created a WebView but in the design UI it doesn't have the function for me to put inside the QML code for design.

                  I know nothing about QML or the Qt Creator designer. I don't know what "it doesn't have the function for me to put inside the QML code for design" might mean.

                  YES! Why do i get empty strings??

                  The only thing you have done in your code is:

                  innerPage->setUrl(url);

                  You then assume you can immediately access the elements & their content. You can't. Setting the Url only starts to load the document. It does not finish loading, and therefore have full data available, until a signal has been received. See the solution & explanation at https://stackoverflow.com/a/10767844/489865 (QWebView::loadFinished(bool ok) signal).

                  G 1 Reply Last reply
                  1
                  • JonBJ JonB

                    @GCDX

                    Yes i created a WebView but in the design UI it doesn't have the function for me to put inside the QML code for design.

                    I know nothing about QML or the Qt Creator designer. I don't know what "it doesn't have the function for me to put inside the QML code for design" might mean.

                    YES! Why do i get empty strings??

                    The only thing you have done in your code is:

                    innerPage->setUrl(url);

                    You then assume you can immediately access the elements & their content. You can't. Setting the Url only starts to load the document. It does not finish loading, and therefore have full data available, until a signal has been received. See the solution & explanation at https://stackoverflow.com/a/10767844/489865 (QWebView::loadFinished(bool ok) signal).

                    G Offline
                    G Offline
                    GCDX
                    wrote on last edited by
                    #9

                    @JonB Thanks so much for your help, could you indulge me a little and explain a little more? I dont fully understand the link.

                    But i did this! It doesn't compile, but i really don't know how to do it.

                    void MainWindow::on_pushButton_clicked()
                    {

                    QWebView *innerPage = new QWebView();
                    QUrl url("http://www.comp.nus.edu.sg/~geraldc/web/index.html");
                    /* Code on the webpage is
                     * <html>
                     * <head></head>
                     * <body>
                     *      <p>HelloWorld</p>
                     *      <p>TESTCODE</p>
                     * </body>
                     * </html>
                    */
                    innerPage->setUrl(url);
                    connect(innerPage, SIGNAL(loadFinished(bool)), SLOT(parse(innerPage)));
                    

                    }

                    void MainWindow::parse(QWebView *innerpage)
                    {
                    QWebFrame *frameInner = innerPage->page()->mainFrame();
                    QWebElement doc = frameInner->documentElement();
                    QWebElement body = doc.firstChild();
                    QWebElement firstParagraph =doc.firstChild();
                    QWebElement secondParagraph = firstParagraph.nextSibling();
                    QString storedText = firstParagraph.toPlainText();
                    QString text = secondParagraph.toPlainText();
                    qDebug()<<QString(storedText);
                    qDebug()<<secondParagraph.toPlainText();
                    }

                    jsulmJ 1 Reply Last reply
                    0
                    • G GCDX

                      @JonB Thanks so much for your help, could you indulge me a little and explain a little more? I dont fully understand the link.

                      But i did this! It doesn't compile, but i really don't know how to do it.

                      void MainWindow::on_pushButton_clicked()
                      {

                      QWebView *innerPage = new QWebView();
                      QUrl url("http://www.comp.nus.edu.sg/~geraldc/web/index.html");
                      /* Code on the webpage is
                       * <html>
                       * <head></head>
                       * <body>
                       *      <p>HelloWorld</p>
                       *      <p>TESTCODE</p>
                       * </body>
                       * </html>
                      */
                      innerPage->setUrl(url);
                      connect(innerPage, SIGNAL(loadFinished(bool)), SLOT(parse(innerPage)));
                      

                      }

                      void MainWindow::parse(QWebView *innerpage)
                      {
                      QWebFrame *frameInner = innerPage->page()->mainFrame();
                      QWebElement doc = frameInner->documentElement();
                      QWebElement body = doc.firstChild();
                      QWebElement firstParagraph =doc.firstChild();
                      QWebElement secondParagraph = firstParagraph.nextSibling();
                      QString storedText = firstParagraph.toPlainText();
                      QString text = secondParagraph.toPlainText();
                      qDebug()<<QString(storedText);
                      qDebug()<<secondParagraph.toPlainText();
                      }

                      jsulmJ Online
                      jsulmJ Online
                      jsulm
                      Lifetime Qt Champion
                      wrote on last edited by
                      #10

                      @GCDX said in QWebview not available in QT creator:

                      connect(innerPage, SIGNAL(loadFinished(bool)), SLOT(parse(innerPage)));

                      this is not how signals/slots work. You cannot pass parameters to a slot in a connect.
                      Change to

                      connect(innerPage, SIGNAL(loadFinished(bool)), SLOT(parse(bool)));
                      

                      You can get the sender in the slot calling sender() function.
                      Take a look at http://doc.qt.io/qt-5.9/signalsandslots.html

                      https://forum.qt.io/topic/113070/qt-code-of-conduct

                      G 1 Reply Last reply
                      2
                      • jsulmJ jsulm

                        @GCDX said in QWebview not available in QT creator:

                        connect(innerPage, SIGNAL(loadFinished(bool)), SLOT(parse(innerPage)));

                        this is not how signals/slots work. You cannot pass parameters to a slot in a connect.
                        Change to

                        connect(innerPage, SIGNAL(loadFinished(bool)), SLOT(parse(bool)));
                        

                        You can get the sender in the slot calling sender() function.
                        Take a look at http://doc.qt.io/qt-5.9/signalsandslots.html

                        G Offline
                        G Offline
                        GCDX
                        wrote on last edited by
                        #11

                        @jsulm @JonB Thanks so much for both your help!!! i managed to parse out the HelloWorld and TESTCODE. However, its a little odd.
                        0_1528477779771_78c24251-7a3a-4e2d-b33b-41eb9d326f68-image.png
                        The first paragraph that is supposed to parse out HelloWorld returns a "" but the second paragraph parsed out both " Hello World TESTCODE" Its a little strange. But thank you for your help!!!!

                        JonBJ 1 Reply Last reply
                        1
                        • G GCDX

                          @jsulm @JonB Thanks so much for both your help!!! i managed to parse out the HelloWorld and TESTCODE. However, its a little odd.
                          0_1528477779771_78c24251-7a3a-4e2d-b33b-41eb9d326f68-image.png
                          The first paragraph that is supposed to parse out HelloWorld returns a "" but the second paragraph parsed out both " Hello World TESTCODE" Its a little strange. But thank you for your help!!!!

                          JonBJ Offline
                          JonBJ Offline
                          JonB
                          wrote on last edited by
                          #12

                          @GCDX

                          The first paragraph that is supposed to parse out HelloWorld returns a "" but the second paragraph parsed out both " Hello World TESTCODE" Its a little strange. But thank you for your help!!!!

                          It's to do with doc.firstChild() and doc.firstChild().nextSibling() not being quite where you think they are in the parsed DOM. For example, try recursing down into them and you'll see what they are actually positioned on, if you know how to use debugger it's much easier that debug messages. Since you want to do DOM traversal you will end up needing to understand the structure to get the code right.

                          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