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. [solved] Getting source from QWebView
Qt 6.11 is out! See what's new in the release blog

[solved] Getting source from QWebView

Scheduled Pinned Locked Moved General and Desktop
5 Posts 2 Posters 16.1k Views 1 Watching
  • 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.
  • ? Offline
    ? Offline
    A Former User
    wrote on last edited by
    #1

    Hello
    When loading a page into a QWebView

    @ui->webView->load(QUrl("http://www.google.com"));@

    How can I get the source code (html) of the page? I can't retrieve the site directly,
    because I might have browsed forward from the loaded site before I want to display the source.
    E.g. having a textedit below the qwebview that shows the source of the current site while I browse
    in the qwebview, kinda like

    @ui->textEdit->setText(ui->webView->getSource()); (which naturally doesn't work...)@

    Thank you. Best regards
    Richard Salin

    1 Reply Last reply
    0
    • ? Offline
      ? Offline
      A Former User
      wrote on last edited by
      #2

      @ui->webView()->page()->mainFrame()->toHtml();@
      or
      @ui->webView()->page()->currentFrame()->toHtml();@

      take a look at these classes : "QWebView":https://qt-project.org/doc/qt-4.8/qwebview.html, "QWebPage":https://qt-project.org/doc/qt-4.8/qwebpage.html, "QWebFrame":https://qt-project.org/doc/qt-4.8/qwebframe.html and "QWebElement":https://qt-project.org/doc/qt-4.8/qwebelement.html.

      There are also a number of examples: "QtWebKit category":https://qt-project.org/wiki/Category:Developing_with_Qt::QtWebKit

      1 Reply Last reply
      0
      • ? Offline
        ? Offline
        A Former User
        wrote on last edited by
        #3

        Thanks. One more thing, how can I display it in the TextEdit? Iif I try the following:

        @ui->textEdit->setText(ui->webView->page()->currentFrame()->toHtml());@

        It tries to show the source as an actual page, and if I replace the "toHtml()" with "toPlainText()" all the html tags are removed... I'd like to show the source with all its tags in the textedit.

        Also, if I try to qDebug the source:

        qDebug() << ui->webView->page()->currentFrame()->toHtml();

        nothing is shown...When I replace the tohtml with toplaintext it works (without the tags). Could this be because the source is to long for the qDebug to display? Thanks
        Richard

        1 Reply Last reply
        0
        • ? Offline
          ? Offline
          A Former User
          wrote on last edited by
          #4

          try this: @ui->webView->page()->currentFrame()->documentElement().toOuterXml()@

          also to avoid QTextEdit from trying to guess the format of the text you're handing over you better use setPlainText() instead of setText() as suggested in the "documentation":https://qt-project.org/doc/qt-4.8/qtextedit.html#setText.

          @ui->textEdit->setPlainText(ui->webView->page()->currentFrame()->toHtml());@

          1 Reply Last reply
          0
          • ? Offline
            ? Offline
            A Former User
            wrote on last edited by
            #5

            The setPlainText did the trick. Thank you!
            Best regards
            Richard

            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