Qt calls Javascript

Source: Internet
Author: User

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.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.