QWebEngineView. How to pass variable to Js function?
-
wrote on 13 Dec 2019, 17:54 last edited by Ucn_
Hello. I would to know how I can pass a QString variable to js function. Example:
QString value; webview = new QWebEngineView; webview->page()->runJavaScript("getInfo(value);");
I receive this error:
js: Uncaught ReferenceError: value is not defined [17240:14380:1213/204829.811:INFO:CONSOLE(1)] "Uncaught ReferenceError: value is not defined", source: qrc:/new/web/Posts.html (1)
It only accepts if value is quoted, "value".
Thanks -
Hello. I would to know how I can pass a QString variable to js function. Example:
QString value; webview = new QWebEngineView; webview->page()->runJavaScript("getInfo(value);");
I receive this error:
js: Uncaught ReferenceError: value is not defined [17240:14380:1213/204829.811:INFO:CONSOLE(1)] "Uncaught ReferenceError: value is not defined", source: qrc:/new/web/Posts.html (1)
It only accepts if value is quoted, "value".
Thankswrote on 13 Dec 2019, 18:17 last edited by@Ucn_
To pass the value ofvalue
into your JavaScript, as shown, you'll want something likewebview->page()->runJavaScript("getInfo('" + value + "');");
Note how it is being quoted inside
'
and'
.There will be some alternative
QString
way of making the string I do with+
above, if you prefer. -
@Ucn_
To pass the value ofvalue
into your JavaScript, as shown, you'll want something likewebview->page()->runJavaScript("getInfo('" + value + "');");
Note how it is being quoted inside
'
and'
.There will be some alternative
QString
way of making the string I do with+
above, if you prefer. -
wrote on 13 Dec 2019, 19:10 last edited by JonB
@Ucn_
OK, I believe this is how you do it in nice, readable fashion:webview->page()->runJavaScript(QString("getInfo('%1', '%2');").arg(value1).arg(value2));
Looks neater, doesn't it? :)
To do it properly/robustly, if your values could ever contain a
'
character you would have to process your value via a function to protect embedded'
s. Also if it has any\
s.
1/6