Find text content in QWebView Failed

229 views Asked by At

In my Qt project, I want to find the text content in QWebView. I have tried the QWebElement method, but it failed.

My html is:

<!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>

Now, I want to edit this html, for example, I want the text display "hello, qt". And my code is:

    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();

However, the html can be display normally by QWebView, but the text content has not been modified as "hello, qt".

After debugging the code, I find the findFirst function return a NULL value. So, what's wrong with the findFirst function.

And, what if several text exist in the html? How can I using the name of the text to identify what I actually need?

Any suggestion means a lot for me. Thanks in advance.

ZhQ

1

There are 1 answers

0
Qiang Zhang On

Firstly, I want to say that the code is ok. Of course, the result really make me confused.

The reason that make the code didn't work is that "QWebView use asynchrone component, so the web page loading is make throw eventloop execution." Which means the qwebview didn't actually load the html after the qwebview::load function.

First of all, the html is shown normally in the qwidget. And I add a button in the qwidget. When I click the button, I change the text content in the slot function, and it worked.

zhq