QWebView* webview = new QWebView(this);
webview->load(QUrl("file:////home/zanilia/1.html"));
webview->setMinimumSize(1920,1080);
webview->adjustSize();
webview->show();
QWebElement body = document.firstChild();
QWebElement firstParagraph = body.firstChild();
firstParagraph.setInnerXml("<span>test<strong> succeed</strong></span>");
webview->reload();
webview->show();
1.html
<html>
<body>
<p>First Paragraph</p>
<p>Second Paragraph</p>
</body>
</html>
qt 操作dom时webview窗口显示的内容并没有发生改变
在使用 QtWebKit 操作 DOM 时,你需要确保你的页面已经加载完成。尝试在页面加载完成之后操作 DOM,而不是在页面加载命令执行之后立即进行。你可以使用 QWebView
的 loadFinished(bool)
信号来确认页面加载是否完成。以下是一段示例代码:
QWebView* webview = new QWebView(this);
webview->load(QUrl("file:////home/zanilia/1.html"));
webview->setMinimumSize(1920,1080);
webview->adjustSize();
webview->show();
connect(webview, &QWebView::loadFinished, [=](bool ok) {
if(ok) {
QWebFrame *frame = webview->page()->mainFrame();
QWebElement document = frame->documentElement();
QWebElement body = document.firstChild();
QWebElement firstParagraph = body.firstChild();
firstParagraph.setInnerXml("<span>test<strong> succeed</strong></span>");
}
});
在这段代码中,我们连接了 QWebView
的 loadFinished
信号到一个槽函数。这个槽函数会在页面加载完成后被调用,并对 DOM 进行操作。请注意,这个槽函数使用了 C++ 的 lambda 函数语法。此外,我们也需要获取当前的主 QWebFrame
才能操作其 DOM。
这样,当页面加载完成时,你对 DOM 的操作应该能够正确地在页面上显示出来。