2016-12-31 26 views
0

在我的Qt項目中,我想查找QWebView中的文本內容。我嘗試過QWebElement方法,但失敗了。在QWebView中查找文本內容失敗

我的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> 

現在,我想編輯這個網站,比如我想要的文字顯示 「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,但文字內容不被修改爲:「你好,QT」。

調試代碼後,我找到的FindFirst函數返回NULL值。那麼,findFirst函數有什麼問題。

而且,如果幾個文本在HTML中存在哪些?我如何使用文本的名稱來確定我實際需要的內容?

任何建議對我來說都意味着很多。提前致謝。

ZHQ

回答

0

首先,我想說的是,該代碼即可。當然,結果真的讓我感到困惑。

,使代碼不工作的原因是,「QWebView使用asynchrone成分,所以網頁加載是使拋事件循環的執行。」這意味着qwebview實際上並沒有在qwebview :: load函數後加載html。

首先,在HTML中QWidget的正常顯示。我在qwidget中添加一個按鈕。當我點擊按鈕時,我改變了插槽功能中的文本內容,並且它工作。

ZHQ