Monday, May 15, 2017

QWebEngine: handling get and post requests when a runJavaScript is performed

Leave a Comment

I am looking for possibility of handling get and post requests made by running a java-script code after a website is loaded. Here is the description: a url could be loaded via QWebEnginePage::load and the page contains some buttons with javescript events bind to them. buttons do some get and post requests from internet. Is there anyway that I could signal my classes when the get and post requests are performed by that javascript events. If it is impossible with QWebEngine What are the other options in Qt to do job. I am looking for some options that would not be absolute in the future since it is part of long-term project. Thanks

1 Answers

Answers 1

You can use QWebChannel that should work in your case.

CPP file

QWebChannel* webChannel = new QWebChannel(); webChannel->registerObject("foo", this); webview->page()->setWebChannel(webChannel); 

in HTML file

<script type="text/javascript" src="qrc:/Map/qwebchannel.js"></script> <script type="text/javascript">     new QWebChannel(qt.webChannelTransport, function(channel) {     // all published objects are available in channel.objects under     // the identifier set in their attached WebChannel.id property     var foo = channel.objects.foo;      // access a property     alert(foo.hello);      // connect to a signal     foo.someSignal.connect(function(message) {        alert("Got signal: " + message);     });     // invoke a method, and receive the return value asynchronously    foo.someMethod("bar", function(ret) {        alert("Got return value: " + ret);    }); }); </script> 
If You Enjoyed This, Take 5 Seconds To Share It

0 comments:

Post a Comment