How to call the valueJavaScript () function from Qt?

I cannot call javascript function from QT.

I am using the code below

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> 

Could someone give me any directions why the Javascript function is not called.

+4
source share
3 answers

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

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

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

Now, by right-clicking → check console, you would see a JS interpretation error.

+4
source

You must have lowercase "c" in "onclick":

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

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

Replace map with the desired class name.

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

0
source

Source: https://habr.com/ru/post/1436087/


All Articles