Call Javascript Functions from C/C++

If you have ever wondered how to control QWebView’s contents using C/C++, it’s actually pretty easy to accomplish using Qt. All you need to do is to write a Javascript function in your HTML page that does whatever that you need, and evaluate the Javascript function from C/C++.

For example, you wrote a Javascript function in your HTML page, like so:

<html>
<head>
<script type="text/javascript">
function MyFunction()
{
     DoSomething();
}
</script>
</head>
</html>

Then, in C/C++ you can simply call:

myWebView->page()->mainFrame()->evaluateJavaScript("MyFunction();");

That’s all! Once you called evaluateJavaScript() function, Qt will handle the rest for you.

If you want to learn how to call C/C++ functions from Javascript, read this.

Leave a Reply

Your email address will not be published. Required fields are marked *