在我的 Qt 项目中,我想在 QWebView 中查找文本内容。我已经尝试过 QWebElement 方法,但它失败了。
我的html是:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>nothing</title>
<style>
input[type=text] { font-size:15px; width:200; background:#ccc}
</style>
</head>
<p> hello </p>
<body>
<input type='text' name='hello' value='good luck'/>
</body>
</html>
现在,我想编辑这个 html,例如,我希望文本显示“hello, qt”。我的代码是:
QWebView * webView = new QWebView();
webView->load(QUrl::fromLocalFile("C:\\Users\\zhq\\Desktop\\QT_VTK_MIP\\qt.html"));
QWebElement document = webView->page()->currentFrame()->documentElement();
QWebElement firstTextInput = document.findFirst("input[type=text]");
QString storedText = firstTextInput.attribute("value");
firstTextInput.setAttribute("value",QString("hello,qt"));
webView->show();
但是通过QWebView可以正常显示html,但是文本内容并没有被修改为“hello, qt”。
调试代码后,我发现 findFirst 函数返回 NULL 值。那么,findFirst 函数有什么问题。
而且,如果 html 中存在多个文本怎么办?如何使用文本名称来识别我真正需要的内容?
任何建议对我来说都意义重大。提前致谢。
浙青