[solved] Getting source from QWebView
-
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 -
@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
-
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 -
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());@
-
The setPlainText did the trick. Thank you!
Best regards
Richard