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 ?
QtWS25 Last Chance

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.
  • sonichyS Offline
    sonichyS Offline
    sonichy
    wrote on last edited by sonichy
    #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

    jsulmJ 1 Reply Last reply
    0
    • sonichyS sonichy

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

      jsulmJ Offline
      jsulmJ Offline
      jsulm
      Lifetime Qt Champion
      wrote on 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

      sonichyS 1 Reply Last reply
      0
      • jsulmJ jsulm

        @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>.

        sonichyS Offline
        sonichyS Offline
        sonichy
        wrote on 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

        • Login

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