首先在Qt中引入模块 Web Channel WebEngine WebEngine Widgets
QWebChannel* channel = new QWebChannel(ui.webEngineView->page());
channel->registerObject("bridge",this);
ui.webEngineView->page()->setWebChannel(channel);
ui.webEngineView->setContextMenuPolicy(Qt::NoContextMenu);
接收槽函数
void showMsgBox(QString str)
{
}
JS中引用 qwebchannel.js 然后编写代码,调用上述槽函数showMsgBox
<script src="./js/qwebchannel.js'"></script>
var bridge;
new QWebChannel(qt.webChannelTransport,function(channel)
{
bridge = channel.objects.bridge;
});
bridge.showMsgBox(QString);