Nitesh Narayan Lal
Nitesh Narayan Lal

Reputation: 85

How to access variables values used in Java Script from qt code

If I am having a qt code and I am running a java script using webkit , then how can I access the variable value which is used in that script from my qt-code?

Upvotes: 0

Views: 1185

Answers (1)

Silas Parker
Silas Parker

Reputation: 8157

Set the variable somewhere accessible (such as on the window object), and then use QWebFrame::evaluateJavaScript to get the variable.

In JavaScript

window.myint = 5;

In Qt:

const QVariant myvar = webview.page()->mainFrame()->evaluateJavaScript("window.myint");
bool ok;
const int myint = myvar.toInt(&ok);
if (!ok)
  qWarning() << "Error getting int from JS";

This works with any type that can be passed between JavaScript and Qt.

Upvotes: 2

Related Questions