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: Difference between revisions

From Qt Wiki
Jump to navigation Jump to search
(Decode HTML entity names)
(Cleanup)
Line 1: Line 1:
{{Cleanup | reason=Auto-imported from ExpressionEngine.}}
{{LangSwitch}}
 
[[Category:snippets]]
[[Category:snippets]]
[[Category:Developing_with_Qt::QtWebKit]]
[[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…


Line 11: Line 9:
  <head>
  <head>
  <script type="text/javascript">
  <script type="text/javascript">
  function displaymessage(str)
  function displaymessage(str)
  {
  {
alert(str);
  alert(str);
  }
  }


function displayhello()
function displayhello()
  {
  {
alert("Hello");
  alert("Hello");
  }
  }
  </script>
  </script>
  </head>
  </head>


<body>
<body>
<form>
  <form>
<input type="button" value="Click me!">
    <input type="button" value="Click me!">
</form>
  </form>
  </body>
  </body>
</html>
</html>
</code>
</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 {{DocLink|QWebView}} in your mainwindow. And in the mainwindow.cpp


<code>
<code>
QWebFrame *frame = ui->webView->page()->mainFrame();
QWebFrame* frame = ui->webView->page()->mainFrame();


// the below line will call the javascript function that does not have any param
// the below line will call the javascript function that does not have any param
frame->evaluateJavaScript("displayhello()");
frame->evaluateJavaScript("displayhello()");


// now this is how to call the javascript function which takes a QString param
// now this is how to call the javascript function which takes a QString param
QString data("Qt is the Best!"); // can be some large data, say from a file
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" contains a quote character!
QString param = QString("displaymessage('%1')").arg(data); // FIXME: Does not work if "data" contains a quote character!
frame->evaluateJavaScript(param);
frame->evaluateJavaScript(param);
</code>
</code>

Revision as of 22:21, 28 June 2015

En Ar Bg De El Es Fa Fi Fr Hi Hu It Ja Kn Ko Ms Nl Pl Pt Ru Sq Th Tr Uk Zh

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

<html>
 <head>
 <script type="text/javascript">
 
 function displaymessage(str)
 {
   alert(str);
 }

 function displayhello()
 {
   alert("Hello");
 }
 
 </script>
 </head>

 <body>
   <form>
     <input type="button" value="Click me!">
   </form>
 </body>
</html>

Now using the Designer, load this page to a QWebView in your mainwindow. And in the mainwindow.cpp

QWebFrame* frame = ui->webView->page()->mainFrame();

// the below line will call the javascript function that does not have any param
frame->evaluateJavaScript("displayhello()");

// now this is how to call the javascript function which takes a QString param
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" contains a quote character!
frame->evaluateJavaScript(param);