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. How to view source of HTML in QWebView ?
Forum Update on Monday, May 27th 2025

How to view source of HTML in QWebView ?

Scheduled Pinned Locked Moved Solved General and Desktop
3 Posts 2 Posters 1.0k 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.
  • S Offline
    S Offline
    sonichy
    wrote on 12 Aug 2017, 08:31 last edited by sonichy 8 Dec 2017, 08:51
    #1

    void MainWindow::viewSource()
    {
    QString s = ui->webView->page()->currentFrame()->toHtml();
    QUrl url = ui->webView->url();
    ui->webView->setHtml(s,url);
    }
    It has no effort.

    https://github.com/sonichy

    J 1 Reply Last reply 14 Aug 2017, 09:18
    0
    • S sonichy
      12 Aug 2017, 08:31

      void MainWindow::viewSource()
      {
      QString s = ui->webView->page()->currentFrame()->toHtml();
      QUrl url = ui->webView->url();
      ui->webView->setHtml(s,url);
      }
      It has no effort.

      J Offline
      J Offline
      jsulm
      Lifetime Qt Champion
      wrote on 14 Aug 2017, 09:18 last edited by
      #2

      @sonichy Take a look at https://stackoverflow.com/questions/16783708/how-to-display-raw-html-code-in-pre-or-something-like-it-but-without-escaping-it
      You need to prevent the HTML source code from being interpreted as HTML, so you need to create a new HTML string where you put your HTML source into <pre></pre>.

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

      S 1 Reply Last reply 18 Aug 2017, 04:55
      0
      • J jsulm
        14 Aug 2017, 09:18

        @sonichy Take a look at https://stackoverflow.com/questions/16783708/how-to-display-raw-html-code-in-pre-or-something-like-it-but-without-escaping-it
        You need to prevent the HTML source code from being interpreted as HTML, so you need to create a new HTML string where you put your HTML source into <pre></pre>.

        S Offline
        S Offline
        sonichy
        wrote on 18 Aug 2017, 04:55 last edited by
        #3

        @jsulm

        I find the solution: change "<" to "&lt;" and ">" to "&gt;" before use <pre>.
        QString s = ((QWebView*)(ui->tabWidget->currentWidget()))->page()->currentFrame()->toHtml();
        s=s.replace("<", "&lt;");
        s=s.replace(">", "&gt;");
        s="<title>source</title><pre>"+s+"</pre>";
        QUrl url = ((QWebView*)(ui->tabWidget->currentWidget()))->url();
        newTab();
        ((QWebView*)(ui->tabWidget->currentWidget()))->setHtml(s,url);
        

        https://github.com/sonichy

        1 Reply Last reply
        1

        3/3

        18 Aug 2017, 04:55

        • Login

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