Important: Please read the Qt Code of Conduct -

QWebView Change Html Text

  • I load a local file:

    ui->webView->load( QUrl( url ) );

    The HTML has this div tag:


    I want to change CustomerName to John Doe. What's the best way to do this?

  • Moderators

    Hi @IChooseYou and Welcome,
    AFAIK you can use QWebElement and QWebFrame to do so. QWebElement has find* methods (findAll and findFirst) which can be used find the collection of elemets or the exact particular element. Then later once you found out that particular QWebElement you can invoke its methods like setPlainText or tagName.
    So for eg: in your case you can provide a id to the div element using which you can find that element

    <div id=custname>CustomerName</div>
    //finding that element
    QWebElement document = webview->page()->mainFrame()->documentElement();
    QWebElement element = document.findFirst("div#custname");
    element.setPlainText("John Doe");

Log in to reply