私の 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();
ただし、htmlはQWebViewで正常に表示できますが、テキストの内容は「hello, qt」と変更されていません。
コードをデバッグした後、findFirst 関数が NULL 値を返すことがわかりました。では、findFirst 関数の何が問題になっているのでしょうか。
また、html に複数のテキストが存在する場合はどうなるでしょうか。テキストの名前を使用して、実際に必要なものを特定するにはどうすればよいですか?
どんな提案も私にとって大きな意味があります。前もって感謝します。
ZhQ