QString variable to Javascript: Difference between revisions
Jump to navigation
Jump to search
No edit summary |
No edit summary |
||
Line 1: | Line 1: | ||
[[Category:snippets]]<br />[[Category:Developing_with_Qt::QtWebKit]] | |||
Small snippet showing how to call two javascript functions, one without any param, and one with a QString param… | 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 /> | This is a simple html to test with<br /><code><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 /></code> | |||
Now using the Designer, load this page to a QWebView in your mainwindow. And in the mainwindow.cpp | Now using the Designer, load this page to a QWebView in your mainwindow. And in the mainwindow.cpp | ||
= | <code><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 /></code> | |||
Revision as of 10:45, 24 February 2015
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 />