How to call evaluateJavaScript() function from Qt?

2019-05-05 03:15发布

I am not able to call a javascript function from QT .

I am using the below code

QT code :
QWebFrame *frame = m_d->m_webView->page()->mainFrame(); frame->evaluateJavaScript("displayhello()");

 Here `displayhello()` is the `Javascript` function defined in the HTML file.

HTML code :

<html>
<head>
<script type="text/javascript" src="" />

<script type="text/javascript">
   function displaymessage(str)
   {
       alert(str);
   }
   function displayhello()
   {
       alert("Hello");
   }
</script>
</head>
<body>
<form>
<input type="button" value="Click me!" onClick="displayhello()">
</form>
</body>
</html>

Can anyone give me any pointers why the Javascript function is not getting called.

3条回答
Viruses.
2楼-- · 2019-05-05 03:22

You should have lower case 'c' in 'onclick':

<input type="button" value="Click me!" onclick="displayhello()">
查看更多
该账号已被封号
3楼-- · 2019-05-05 03:24

QVariant holdInformation = map->page()->mainFrame()->evaluateJavaScript (QString ("displayhello ()"));

Replace map by your desired class name.

Also, try using onload.
<body onload = "displayhello()" topmargin = "0" leftmargin = "0">

查看更多
够拽才男人
4楼-- · 2019-05-05 03:45

I know this post is old but in case someone has the same:

Always check Dev Tools to see what the JavaScript console tells you. You can enable devtools by adding this line tou your QT main function

QWebSettings::globalSettings()->setAttribute(QWebSettings::DeveloperExtrasEnabled, true);

Now by right clicking->inspect, console, you would have seen the JS interpret error.

查看更多
登录 后发表回答