Qt实现和js互相通信

1、js往Qt中发送消息

1.1、Qt中注册对象

auto pWebView = new QWebEngineView(this);
auto pWebPage = new QWebEnginePage(this);
auto jsProtocol = new MyObject(this);
auto pWebChannel = new QWebChannel(this);
pWebChannel->registerObject("myProjectInterface", jsProtocol);
pWebPage->setWebChannel(pWebChannel);
pWebView->setPage(pWebPage);

1.2、js中获取注册的对象


    new QWebChannel(qt.webChannelTransport, function(channel){
      this.qtContext = channel.objects.myProjectInterface;
    });

2、Qt往js中发送消息

QWebEnginePage::runJavaScript("recvMessageFromQt('hello js, i am qt')")

//********************************************************
/// @brief 
/// @author y974183789@gmail.com
/// @date 2022/12/2
/// @note
/// @version 1.0.0
//********************************************************

#include "MyProjectWebView.h"
#include <QWebEngineProfile>
#include <QWebChannel>
#include <QTimer>
#include <QDebug>

class MyObject : public QObject {
Q_OBJECT
public:
MyObject(QObject* parent) 
: QObject(parent){}
~MyObject(){}

public Q_SLOTS:
void doMessage(const QString& msg){
    //TODO 处理js发送过来的消息
    qDebug() << msg;
}
};


MyProjectWebView::MyProjectWebView(QWidget* parent)
: QWebEngineView(parent) {
    auto jsProtocol = new MyObject(this);
    QWebEnginePage *pWebPage = new QWebEnginePage(this);
    setPage(pWebPage);
    QWebChannel *pWebChannel = new QWebChannel(this);
    pWebChannel->registerObject("myProjectInterface", jsProtocol);
    pWebPage->setWebChannel(pWebChannel);
    QTimer::singleShot(5000, this, [pWebPage](){
        //发送消息到js
        pWebPage->runJavaScript(QString("recvMessageFromQt('%1');").arg("hello js, i am a Qter"));
    });

    setContextMenuPolicy(Qt::NoContextMenu);
}

MyProjectWebView::~MyProjectWebView() {
}
<!doctype html>
<html>
<head>
<title>Web Notifications Example</title>
<script type="text/javascript" src="qwebchannel.js"></script>
<script>
    this.qtContext = undefined
    //init QtWebChannel
    new QWebChannel(qt.webChannelTransport, function(channel){
      this.qtContext = channel.objects.myProjectInterface;
    });

    function sendMessageToQt(msg) {
        //send message to qt Application
        if(typeof this.qtContext != 'undefined') {
            this.qtContext.doMessage(msg);
        }
    }

    function recvMessageFromQt(msg) {
        alert("recvMessageFromQt" + msg);
    }

    function onMakeNotification() {
        sendMessageToQt("hello Qter, i'am js");
    }

</script>
</head>
<body style='text-align:center;'>
    <h3>Click the button to send a notification</h3>

    <button onclick='onMakeNotification()'>Notify!</button>

    <p>
        <output id='act'></output>
        <button id='close' style='display: none;' onclick='closeNotification()'>Close</button>
    </p><br>

    <p>
        <label for='state'>Permission:</label>
        <output id='state'></output>
    </p><br>

    <h4>More info can be found on:</h4>
    <ul style='list-style-type: none;'>
        <li>W3 <a href='https://www.w3.org/TR/notifications'>Web Notifications</a> standard</li>
        <li>Documentation for <a href='https://doc.qt.io'>Qt WebEngine</a> module</li>
    </ul>
</body>
</html>

完整demo

github地址:https://github.com/yanhj/QtWebViewDemo.git

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值