PDA

View Full Version : How to get result from javascript code run by runJavaScript in QtWebengine?



qtcentre2019
21st July 2019, 11:24
How can I get the result from javascript run by runJavaScript in QtWebengine?

I want to get result from the js run by runJavaScript. The result is formed after some time in js, but since js returns immediately, I can not get the mature result. How can I modify the js code or the C++ code to get the mature result? I know I can use QWebChannel to notify C++ of the mature result at appropriate time but that is a little complex. Can I delay the return time of js code so it can return with the matured result?

C++:


page()->runJavaScript(js,[this](const QVariant &v) {
qDebug()<<v.toString();
}

js:


var result="immature result";
function fun()
{
result="mature result";
}
setTimeout(fun,2000);
result;

wysota
1st August 2019, 23:29
Your function will only be executed asynchronously after two seconds so the only option I see is to run another script afterwards and fetch the result. Or use QWebChannel as you have already stated yourself.