March 27, 2012

ThaRez ThaRez
Lab Rat
123 posts

[solved] Getting source from QWebView

 

Hello
When loading a page into a QWebView

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

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

Thank you. Best regards
Richard Salin

4 replies

March 27, 2012

KA51O KA51O
Ant Farmer
478 posts

  1. ui->webView()->page()->mainFrame()->toHtml();

or
  1. ui->webView()->page()->currentFrame()->toHtml();

take a look at these classes : QWebView [qt-project.org], QWebPage [qt-project.org], QWebFrame [qt-project.org] and QWebElement [qt-project.org].

There are also a number of examples: QtWebKit category [qt-project.org]

March 27, 2012

ThaRez ThaRez
Lab Rat
123 posts

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

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

March 27, 2012

KA51O KA51O
Ant Farmer
478 posts

try this:

  1. 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 [qt-project.org].

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

March 27, 2012

ThaRez ThaRez
Lab Rat
123 posts

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

 
  ‹‹ Cross-platform      How do i get the touchpad co-ordinates in Qt 4.7? ››

You must log in to post a reply. Not a member yet? Register here!