How to call evaluateJavaScript() function from Qt?

牧云@^-^@ 提交于 2019-12-10 09:32:52

问题


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.


回答1:


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.




回答2:


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

<input type="button" value="Click me!" onclick="displayhello()">



回答3:


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">



来源:https://stackoverflow.com/questions/12578377/how-to-call-evaluatejavascript-function-from-qt

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!