Qt has a class named qwebview, which is a browser based on the WebKit engine. In addition, QT has components that can display HTML code, such as qtextedit, which provides us with rich, practical, simple, and unexpected features. Qt has excellent scalability. It allows your c ++ code to interact with JavaScript on the webpage. Here, I would like to share my experience in executing JavaScript Functions on a web page using C ++.
JavaScript code is called through the evaluatejavascript function of the qwebframe class. The following is an example. I wrote a local HTML file as follows:
<HTML>
<Head>
<SCRIPT type = "text/JavaScript" src = "test. js">
</SCRIPT>
</Head>
<Body>
<Form>
<Input type = "button" value = "Click me! "Onclick =" test () ">
</Form>
</Body>
</Html>
This simple web page has a button that calls the test function after you click it. The test function is in a Javascript file of test. js. The content of the file is as follows:
Function Test (){
Alert ("test ");
}
Function add (){
Return 33;
}
Create a project, add a qwebviewobject, and upload it to test.html,
Ui. webview-> load (qurl ("test.html "));
Add a button. To call JavaScript, you can use the C ++ code when clicking the button:
Ui. webview-> page ()-> mainframe ()-> evaluatejavascript ("test ()");
After running, a prompt box is displayed on the webpage. This is the same as clicking a button on a webpage.
Note:
If QT calls JS functions and JS functions contain parameters, pay more attention to them. In QT, the function call method should be written as follows:
Qstring method = qstring ("jsfunction (\" % 1 \ ", \" % 2 \ ")"). Arg (param1). Arg (param2 );
M_web_view-> page ()-> mainframe ()-> evaluatejavascript (method );
Quotation marks on both sides of the parameter are required and must be escaped.