Qt wiki will be updated on October 12th 2023 starting at 11:30 AM (EEST) and the maintenance will last around 2-3 hours. During the maintenance the site will be unavailable.
QString variable to Javascript
Jump to navigation
Jump to search
Small snippet showing how to call two javascript functions, one without any param, and one with a QString param…
This is a simple html to test with
<br />&lt;html&amp;gt;<br /> &lt;head&amp;gt;<br /> &lt;script type="text/javascript&quot;&gt;<br /> function displaymessage(str)<br /> {<br /> alert&amp;#40;str&amp;#41;;<br /> }
function displayhello()<br /> {<br /> alert&amp;#40;"Hello&quot;&#41;;<br /> }<br /> &lt;/script&amp;gt;<br /> &lt;/head&amp;gt;
&lt;body&amp;gt;<br /> &lt;form&amp;gt;<br /> &lt;input type="button&quot; value="Click me!"&gt;<br /> &lt;/form&amp;gt;<br /> &lt;/body&amp;gt;<br />&lt;/html&amp;gt;<br />
Now using the Designer, load this page to a QWebView in your mainwindow. And in the mainwindow.cpp
<br /> QWebFrame *frame = ui->webView->page()<s>>mainFrame();
<br /> // the below line will call the javascript function that does not have any param<br /> frame</s>>evaluateJavaScript("displayhello()");
// now this is how to call the javascript function which takes a QString param<br /> QString data("Qt is the Best!"); // can be some large data, say from a file
QString param = QString("displaymessage('%1')").arg(data); // FIXME: Does not work if "data&quot; contains a quote character!<br /> frame->evaluateJavaScript(param);<br />