QString variable to Javascript: Difference between revisions

From Qt Wiki
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 />&amp;lt;html&amp;amp;gt;<br /> &amp;lt;head&amp;amp;gt;<br /> &amp;lt;script type=&quot;text/javascript&amp;quot;&amp;gt;<br /> function displaymessage(str)<br /> {<br /> alert&amp;amp;#40;str&amp;amp;#41;;<br /> }
 
function displayhello()<br /> {<br /> alert&amp;amp;#40;&quot;Hello&amp;quot;&amp;#41;;<br /> }<br /> &amp;lt;/script&amp;amp;gt;<br /> &amp;lt;/head&amp;amp;gt;
 
&amp;lt;body&amp;amp;gt;<br /> &amp;lt;form&amp;amp;gt;<br /> &amp;lt;input type=&quot;button&amp;quot; value=&quot;Click me!&quot;&amp;gt;<br /> &amp;lt;/form&amp;amp;gt;<br /> &amp;lt;/body&amp;amp;gt;<br />&amp;lt;/html&amp;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


===Categories:===
<code><br /> QWebFrame *frame = ui-&gt;webView-&gt;page()<s>&gt;mainFrame();
<br /> // the below line will call the javascript function that does not have any param<br /> frame</s>&gt;evaluateJavaScript(&quot;displayhello()&quot;);
 
// now this is how to call the javascript function which takes a QString param<br /> QString data(&quot;Qt is the Best!&quot;); // can be some large data, say from a file


* [[:Category:Developing with Qt|Developing_with_Qt]]
QString param = QString(&quot;displaymessage('%1')&quot;).arg(data); // FIXME: Does not work if &quot;data&amp;quot; contains a quote character!<br /> frame-&gt;evaluateJavaScript(param);<br /></code>
** [[:Category:Developing with Qt::QtWebKit|QtWebKit]]
* [[:Category:snippets|snippets]]

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 />&amp;lt;html&amp;amp;gt;<br /> &amp;lt;head&amp;amp;gt;<br /> &amp;lt;script type=&quot;text/javascript&amp;quot;&amp;gt;<br /> function displaymessage(str)<br /> {<br /> alert&amp;amp;#40;str&amp;amp;#41;;<br /> }

function displayhello()<br /> {<br /> alert&amp;amp;#40;&quot;Hello&amp;quot;&amp;#41;;<br /> }<br /> &amp;lt;/script&amp;amp;gt;<br /> &amp;lt;/head&amp;amp;gt;

&amp;lt;body&amp;amp;gt;<br /> &amp;lt;form&amp;amp;gt;<br /> &amp;lt;input type=&quot;button&amp;quot; value=&quot;Click me!&quot;&amp;gt;<br /> &amp;lt;/form&amp;amp;gt;<br /> &amp;lt;/body&amp;amp;gt;<br />&amp;lt;/html&amp;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-&gt;webView-&gt;page()<s>&gt;mainFrame();
<br /> // the below line will call the javascript function that does not have any param<br /> frame</s>&gt;evaluateJavaScript(&quot;displayhello()&quot;);

// now this is how to call the javascript function which takes a QString param<br /> QString data(&quot;Qt is the Best!&quot;); // can be some large data, say from a file

QString param = QString(&quot;displaymessage('%1')&quot;).arg(data); // FIXME: Does not work if &quot;data&amp;quot; contains a quote character!<br /> frame-&gt;evaluateJavaScript(param);<br />