Qt += webenginewidgets webchannel
#include <QtWebEngineWidgets> QWebEngineView *m_view;
QWebChannel *channel = new QWebChannel(this); channel->registerObject("form", this); m_view->page()->setWebChannel(channel);
m_view->page()->runJavaScript(s);
-建立QWebChanneljavascript
<script type="text/javascript" src="qwebchannel.js"></script> var form; new QWebChannel(qt.webChannelTransport, function(channel) { form = channel.objects.form; form.call("異次元世界的呼喊 -->"); //到這裏,就能夠調用QObject對象 } );
<link rel="stylesheet" href="bootstrap.min.css">
<div id="#app"> <button class="btn btn-default" id="calc" @click="callQtMethod">{{btn_calc_caption}}</button> </div> <script type="text/javascript" src="vue.min.js"></script> new Vue({ el: "#app", data:{ btn_calc_caption: "計算" } methods: { callQtMethod: function(event) { var s = 'form.on_' + event.target.id + '()'; eval(s); //alert(event.target.id); } } });