QWebEngine:在执行runJavaScript时处理get和post请求

时间:2017-05-05 22:49:16

标签: c++ qt

我正在寻找在加载网站后通过运行java脚本代码来处理get和post请求的可能性。这是描述:一个url可以通过QWebEnginePage :: load加载,页面包含一些按钮,javescript事件绑定到它们。按钮做一些获取和发布来自互联网的请求。无论如何,当javascript事件执行get和post请求时,我可以发信号通知我的类。如果使用QWebEngine是不可能的,Qt中的其他选项有什么工作要做。我正在寻找一些未来绝对的选择,因为它是长期项目的一部分。 感谢

1 个答案:

答案 0 :(得分:0)

您可以使用适合您情况的QWebChannel

CPP文件

QWebChannel* webChannel = new QWebChannel();
webChannel->registerObject("foo", this);
webview->page()->setWebChannel(webChannel);
HTML文件中的

<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>