一生一世学坛

 找回密码
 立即注册
搜索
查看: 4371|回复: 0
打印 上一主题 下一主题

QT中C++和js通信

[复制链接]

334

主题

385

帖子

6830

积分

管理员

Rank: 9Rank: 9Rank: 9

积分
6830
跳转到指定楼层
楼主
发表于 2022-1-1 23:45:31 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
需要实现的C++类:
  1. #pragma once
  2. #include <qobject.h>
  3. #include <qwebengineview.h>
  4. class JsContext : public QObject
  5. {
  6.         Q_OBJECT
  7. public:
  8.         explicit JsContext(QObject *parent = nullptr);

  9. signals:
  10.         //把收到的来自js的消息,发送给需要的QT组件
  11.         void recvdMsg(const QString& msg);

  12. public:
  13.         // 向js页面发送消息
  14.         void sendMsg(QWebEnginePage* page, const QString& msg);

  15. public slots:
  16.         // 接收到页面发送来的消息
  17.         void onMsg(const QString& msg);
  18. };
复制代码
  1. #include "JsContext.h"

  2. JsContext::JsContext(QObject *parent)
  3. {

  4. }
  5. void JsContext::onMsg(const QString &msg)
  6. {
  7.         emit recvdMsg(msg);
  8. }

  9. void JsContext::sendMsg(QWebEnginePage* page, const QString& msg)
  10. {
  11.         //recvMessage是js文件中实现的方法
  12.         page->runJavaScript(QString("recvMessage('%1');").arg(msg));
  13. }
复制代码
需要实现的js文件:index.html   msgutils.js  qwebchannel.js,其中前两个是自己实现,后面一个是Qt自带
  1. var jscontext;
  2. // 初始化
  3. function init()
  4. {
  5.     if (typeof qt != 'undefined')
  6.     {
  7.         new QWebChannel(qt.webChannelTransport, function(channel)
  8.         {
  9.         jscontext = channel.objects.jscontext;
  10.         }
  11.         );
  12.     }
  13.     else
  14.     {
  15.         alert("qt对象获取失败!");
  16.     }
  17. }
  18. // 接收qt发送的消息
  19. function recvMessage(msg)
  20. {
  21.     alert("接收到Qt发送的消息:" + msg);
  22. }
  23. // 向qt发送消息
  24. function sendMessage(msg)
  25. {
  26.     if(typeof jscontext == 'undefined')
  27.     {
  28.         alert("context对象获取失败!");
  29.     }
  30.     else
  31.     {
  32.         jscontext.onMsg(msg);
  33.     }
  34. }
  35. // 控件控制函数
  36. function onBtnSendMsg()
  37. {
  38.     var cmd = document.getElementById("待发送消息").value;
  39.     sendMessage(cmd);   
  40. }
  41. init();
复制代码
  1. /****************************************************************************
  2. **
  3. ** Copyright (C) 2016 The Qt Company Ltd.
  4. ** Copyright (C) 2016 Klarälvdalens Datakonsult AB, a KDAB Group company, info@kdab.com, author Milian Wolff <milian.wolff@kdab.com>
  5. ** Contact: https://www.qt.io/licensing/
  6. **
  7. ** This file is part of the QtWebChannel module of the Qt Toolkit.
  8. **
  9. ** $QT_BEGIN_LICENSE:LGPL$
  10. ** Commercial License Usage
  11. ** Licensees holding valid commercial Qt licenses may use this file in
  12. ** accordance with the commercial license agreement provided with the
  13. ** Software or, alternatively, in accordance with the terms contained in
  14. ** a written agreement between you and The Qt Company. For licensing terms
  15. ** and conditions see https://www.qt.io/terms-conditions. For further
  16. ** information use the contact form at https://www.qt.io/contact-us.
  17. **
  18. ** GNU Lesser General Public License Usage
  19. ** Alternatively, this file may be used under the terms of the GNU Lesser
  20. ** General Public License version 3 as published by the Free Software
  21. ** Foundation and appearing in the file LICENSE.LGPL3 included in the
  22. ** packaging of this file. Please review the following information to
  23. ** ensure the GNU Lesser General Public License version 3 requirements
  24. ** will be met: https://www.gnu.org/licenses/lgpl-3.0.html.
  25. **
  26. ** GNU General Public License Usage
  27. ** Alternatively, this file may be used under the terms of the GNU
  28. ** General Public License version 2.0 or (at your option) the GNU General
  29. ** Public license version 3 or any later version approved by the KDE Free
  30. ** Qt Foundation. The licenses are as published by the Free Software
  31. ** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3
  32. ** included in the packaging of this file. Please review the following
  33. ** information to ensure the GNU General Public License requirements will
  34. ** be met: https://www.gnu.org/licenses/gpl-2.0.html and
  35. ** https://www.gnu.org/licenses/gpl-3.0.html.
  36. **
  37. ** $QT_END_LICENSE$
  38. **
  39. ****************************************************************************/

  40. "use strict";

  41. var QWebChannelMessageTypes = {
  42.     signal: 1,
  43.     propertyUpdate: 2,
  44.     init: 3,
  45.     idle: 4,
  46.     debug: 5,
  47.     invokeMethod: 6,
  48.     connectToSignal: 7,
  49.     disconnectFromSignal: 8,
  50.     setProperty: 9,
  51.     response: 10,
  52. };

  53. var QWebChannel = function(transport, initCallback)
  54. {
  55.     if (typeof transport !== "object" || typeof transport.send !== "function") {
  56.         console.error("The QWebChannel expects a transport object with a send function and onmessage callback property." +
  57.                       " Given is: transport: " + typeof(transport) + ", transport.send: " + typeof(transport.send));
  58.         return;
  59.     }

  60.     var channel = this;
  61.     this.transport = transport;

  62.     this.send = function(data)
  63.     {
  64.         if (typeof(data) !== "string") {
  65.             data = JSON.stringify(data);
  66.         }
  67.         channel.transport.send(data);
  68.     }

  69.     this.transport.onmessage = function(message)
  70.     {
  71.         var data = message.data;
  72.         if (typeof data === "string") {
  73.             data = JSON.parse(data);
  74.         }
  75.         switch (data.type) {
  76.             case QWebChannelMessageTypes.signal:
  77.                 channel.handleSignal(data);
  78.                 break;
  79.             case QWebChannelMessageTypes.response:
  80.                 channel.handleResponse(data);
  81.                 break;
  82.             case QWebChannelMessageTypes.propertyUpdate:
  83.                 channel.handlePropertyUpdate(data);
  84.                 break;
  85.             default:
  86.                 console.error("invalid message received:", message.data);
  87.                 break;
  88.         }
  89.     }

  90.     this.execCallbacks = {};
  91.     this.execId = 0;
  92.     this.exec = function(data, callback)
  93.     {
  94.         if (!callback) {
  95.             // if no callback is given, send directly
  96.             channel.send(data);
  97.             return;
  98.         }
  99.         if (channel.execId === Number.MAX_VALUE) {
  100.             // wrap
  101.             channel.execId = Number.MIN_VALUE;
  102.         }
  103.         if (data.hasOwnProperty("id")) {
  104.             console.error("Cannot exec message with property id: " + JSON.stringify(data));
  105.             return;
  106.         }
  107.         data.id = channel.execId++;
  108.         channel.execCallbacks[data.id] = callback;
  109.         channel.send(data);
  110.     };

  111.     this.objects = {};

  112.     this.handleSignal = function(message)
  113.     {
  114.         var object = channel.objects[message.object];
  115.         if (object) {
  116.             object.signalEmitted(message.signal, message.args);
  117.         } else {
  118.             console.warn("Unhandled signal: " + message.object + "::" + message.signal);
  119.         }
  120.     }

  121.     this.handleResponse = function(message)
  122.     {
  123.         if (!message.hasOwnProperty("id")) {
  124.             console.error("Invalid response message received: ", JSON.stringify(message));
  125.             return;
  126.         }
  127.         channel.execCallbacks[message.id](message.data);
  128.         delete channel.execCallbacks[message.id];
  129.     }

  130.     this.handlePropertyUpdate = function(message)
  131.     {
  132.         for (var i in message.data) {
  133.             var data = message.data[i];
  134.             var object = channel.objects[data.object];
  135.             if (object) {
  136.                 object.propertyUpdate(data.signals, data.properties);
  137.             } else {
  138.                 console.warn("Unhandled property update: " + data.object + "::" + data.signal);
  139.             }
  140.         }
  141.         channel.exec({type: QWebChannelMessageTypes.idle});
  142.     }

  143.     this.debug = function(message)
  144.     {
  145.         channel.send({type: QWebChannelMessageTypes.debug, data: message});
  146.     };

  147.     channel.exec({type: QWebChannelMessageTypes.init}, function(data) {
  148.         for (var objectName in data) {
  149.             var object = new QObject(objectName, data[objectName], channel);
  150.         }
  151.         // now unwrap properties, which might reference other registered objects
  152.         for (var objectName in channel.objects) {
  153.             channel.objects[objectName].unwrapProperties();
  154.         }
  155.         if (initCallback) {
  156.             initCallback(channel);
  157.         }
  158.         channel.exec({type: QWebChannelMessageTypes.idle});
  159.     });
  160. };

  161. function QObject(name, data, webChannel)
  162. {
  163.     this.__id__ = name;
  164.     webChannel.objects[name] = this;

  165.     // List of callbacks that get invoked upon signal emission
  166.     this.__objectSignals__ = {};

  167.     // Cache of all properties, updated when a notify signal is emitted
  168.     this.__propertyCache__ = {};

  169.     var object = this;

  170.     // ----------------------------------------------------------------------

  171.     this.unwrapQObject = function(response)
  172.     {
  173.         if (response instanceof Array) {
  174.             // support list of objects
  175.             var ret = new Array(response.length);
  176.             for (var i = 0; i < response.length; ++i) {
  177.                 ret[i] = object.unwrapQObject(response[i]);
  178.             }
  179.             return ret;
  180.         }
  181.         if (!response
  182.             || !response["__QObject*__"]
  183.             || response.id === undefined) {
  184.             return response;
  185.         }

  186.         var objectId = response.id;
  187.         if (webChannel.objects[objectId])
  188.             return webChannel.objects[objectId];

  189.         if (!response.data) {
  190.             console.error("Cannot unwrap unknown QObject " + objectId + " without data.");
  191.             return;
  192.         }

  193.         var qObject = new QObject( objectId, response.data, webChannel );
  194.         qObject.destroyed.connect(function() {
  195.             if (webChannel.objects[objectId] === qObject) {
  196.                 delete webChannel.objects[objectId];
  197.                 // reset the now deleted QObject to an empty {} object
  198.                 // just assigning {} though would not have the desired effect, but the
  199.                 // below also ensures all external references will see the empty map
  200.                 // NOTE: this detour is necessary to workaround QTBUG-40021
  201.                 var propertyNames = [];
  202.                 for (var propertyName in qObject) {
  203.                     propertyNames.push(propertyName);
  204.                 }
  205.                 for (var idx in propertyNames) {
  206.                     delete qObject[propertyNames[idx]];
  207.                 }
  208.             }
  209.         });
  210.         // here we are already initialized, and thus must directly unwrap the properties
  211.         qObject.unwrapProperties();
  212.         return qObject;
  213.     }

  214.     this.unwrapProperties = function()
  215.     {
  216.         for (var propertyIdx in object.__propertyCache__) {
  217.             object.__propertyCache__[propertyIdx] = object.unwrapQObject(object.__propertyCache__[propertyIdx]);
  218.         }
  219.     }

  220.     function addSignal(signalData, isPropertyNotifySignal)
  221.     {
  222.         var signalName = signalData[0];
  223.         var signalIndex = signalData[1];
  224.         object[signalName] = {
  225.             connect: function(callback) {
  226.                 if (typeof(callback) !== "function") {
  227.                     console.error("Bad callback given to connect to signal " + signalName);
  228.                     return;
  229.                 }

  230.                 object.__objectSignals__[signalIndex] = object.__objectSignals__[signalIndex] || [];
  231.                 object.__objectSignals__[signalIndex].push(callback);

  232.                 if (!isPropertyNotifySignal && signalName !== "destroyed") {
  233.                     // only required for "pure" signals, handled separately for properties in propertyUpdate
  234.                     // also note that we always get notified about the destroyed signal
  235.                     webChannel.exec({
  236.                         type: QWebChannelMessageTypes.connectToSignal,
  237.                         object: object.__id__,
  238.                         signal: signalIndex
  239.                     });
  240.                 }
  241.             },
  242.             disconnect: function(callback) {
  243.                 if (typeof(callback) !== "function") {
  244.                     console.error("Bad callback given to disconnect from signal " + signalName);
  245.                     return;
  246.                 }
  247.                 object.__objectSignals__[signalIndex] = object.__objectSignals__[signalIndex] || [];
  248.                 var idx = object.__objectSignals__[signalIndex].indexOf(callback);
  249.                 if (idx === -1) {
  250.                     console.error("Cannot find connection of signal " + signalName + " to " + callback.name);
  251.                     return;
  252.                 }
  253.                 object.__objectSignals__[signalIndex].splice(idx, 1);
  254.                 if (!isPropertyNotifySignal && object.__objectSignals__[signalIndex].length === 0) {
  255.                     // only required for "pure" signals, handled separately for properties in propertyUpdate
  256.                     webChannel.exec({
  257.                         type: QWebChannelMessageTypes.disconnectFromSignal,
  258.                         object: object.__id__,
  259.                         signal: signalIndex
  260.                     });
  261.                 }
  262.             }
  263.         };
  264.     }

  265.     /**
  266.      * Invokes all callbacks for the given signalname. Also works for property notify callbacks.
  267.      */
  268.     function invokeSignalCallbacks(signalName, signalArgs)
  269.     {
  270.         var connections = object.__objectSignals__[signalName];
  271.         if (connections) {
  272.             connections.forEach(function(callback) {
  273.                 callback.apply(callback, signalArgs);
  274.             });
  275.         }
  276.     }

  277.     this.propertyUpdate = function(signals, propertyMap)
  278.     {
  279.         // update property cache
  280.         for (var propertyIndex in propertyMap) {
  281.             var propertyValue = propertyMap[propertyIndex];
  282.             object.__propertyCache__[propertyIndex] = propertyValue;
  283.         }

  284.         for (var signalName in signals) {
  285.             // Invoke all callbacks, as signalEmitted() does not. This ensures the
  286.             // property cache is updated before the callbacks are invoked.
  287.             invokeSignalCallbacks(signalName, signals[signalName]);
  288.         }
  289.     }

  290.     this.signalEmitted = function(signalName, signalArgs)
  291.     {
  292.         invokeSignalCallbacks(signalName, this.unwrapQObject(signalArgs));
  293.     }

  294.     function addMethod(methodData)
  295.     {
  296.         var methodName = methodData[0];
  297.         var methodIdx = methodData[1];
  298.         object[methodName] = function() {
  299.             var args = [];
  300.             var callback;
  301.             for (var i = 0; i < arguments.length; ++i) {
  302.                 var argument = arguments[i];
  303.                 if (typeof argument === "function")
  304.                     callback = argument;
  305.                 else if (argument instanceof QObject && webChannel.objects[argument.__id__] !== undefined)
  306.                     args.push({
  307.                         "id": argument.__id__
  308.                     });
  309.                 else
  310.                     args.push(argument);
  311.             }

  312.             webChannel.exec({
  313.                 "type": QWebChannelMessageTypes.invokeMethod,
  314.                 "object": object.__id__,
  315.                 "method": methodIdx,
  316.                 "args": args
  317.             }, function(response) {
  318.                 if (response !== undefined) {
  319.                     var result = object.unwrapQObject(response);
  320.                     if (callback) {
  321.                         (callback)(result);
  322.                     }
  323.                 }
  324.             });
  325.         };
  326.     }

  327.     function bindGetterSetter(propertyInfo)
  328.     {
  329.         var propertyIndex = propertyInfo[0];
  330.         var propertyName = propertyInfo[1];
  331.         var notifySignalData = propertyInfo[2];
  332.         // initialize property cache with current value
  333.         // NOTE: if this is an object, it is not directly unwrapped as it might
  334.         // reference other QObject that we do not know yet
  335.         object.__propertyCache__[propertyIndex] = propertyInfo[3];

  336.         if (notifySignalData) {
  337.             if (notifySignalData[0] === 1) {
  338.                 // signal name is optimized away, reconstruct the actual name
  339.                 notifySignalData[0] = propertyName + "Changed";
  340.             }
  341.             addSignal(notifySignalData, true);
  342.         }

  343.         Object.defineProperty(object, propertyName, {
  344.             configurable: true,
  345.             get: function () {
  346.                 var propertyValue = object.__propertyCache__[propertyIndex];
  347.                 if (propertyValue === undefined) {
  348.                     // This shouldn't happen
  349.                     console.warn("Undefined value in property cache for property "" + propertyName + "" in object " + object.__id__);
  350.                 }

  351.                 return propertyValue;
  352.             },
  353.             set: function(value) {
  354.                 if (value === undefined) {
  355.                     console.warn("Property setter for " + propertyName + " called with undefined value!");
  356.                     return;
  357.                 }
  358.                 object.__propertyCache__[propertyIndex] = value;
  359.                 var valueToSend = value;
  360.                 if (valueToSend instanceof QObject && webChannel.objects[valueToSend.__id__] !== undefined)
  361.                     valueToSend = { "id": valueToSend.__id__ };
  362.                 webChannel.exec({
  363.                     "type": QWebChannelMessageTypes.setProperty,
  364.                     "object": object.__id__,
  365.                     "property": propertyIndex,
  366.                     "value": valueToSend
  367.                 });
  368.             }
  369.         });

  370.     }

  371.     // ----------------------------------------------------------------------

  372.     data.methods.forEach(addMethod);

  373.     data.properties.forEach(bindGetterSetter);

  374.     data.signals.forEach(function(signal) { addSignal(signal, false); });

  375.     for (var name in data.enums) {
  376.         object[name] = data.enums[name];
  377.     }
  378. }

  379. //required for use with nodejs
  380. if (typeof module === 'object') {
  381.     module.exports = {
  382.         QWebChannel: QWebChannel
  383.     };
  384. }
复制代码
上面这个qwebchannel.js是qt自带的
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4.     <meta charset="UTF-8">
  5.     <title>webchannel测试</title>
  6. </head>
  7. <body>
  8.     <p>webchannel test</p>
  9.     <script type="text/javascript" src="./qwebchannel.js"></script>
  10.     <script type="text/javascript" src="./msgutils.js"></script>
  11.     <input id="待发送消息" type="text" name="msgText" />
  12.     <input type="button" value="发送消息到浏览器" onclick="onBtnSendMsg()" />
  13. </body>
  14. </html>
复制代码
关键的业务代码:
  1. auto webView = new QWebEngineView();
  2.                 webView->setFixedSize(500,300);
  3.                 webLayou->addWidget(webView);
  4.         //网页文件的位置
  5.                 QString _testPath = QString("%1/HtmlTest/index.html").arg(QCoreApplication::applicationDirPath());
  6.                 webView->load(QUrl(_testPath));
  7.                 currentShowW = webView;

  8.                 m_jsContext = new JsContext(this);
  9.                 m_webChannel = new QWebChannel(this);
  10.                 m_webChannel->registerObject("jscontext", m_jsContext);
  11.                 currentShowW->page()->setWebChannel(m_webChannel);
  12.                 //处理收到的来自js的消息
  13.                 connect(m_jsContext, &JsContext::recvdMsg, this, [this](const QString& msg) {
  14.                         QMessageBox::information(NULL, "recvdMsg", msg);
  15.                 });
复制代码
  1. //给js发送消息
  2.         connect(closeButn, &QPushButton::clicked, this, [this]() {
  3.                 QString msg = "hello html page!";
  4.                 if (!msg.isEmpty())
  5.                 {
  6.                         m_jsContext->sendMsg(currentShowW->page(), msg);
  7.                 }
  8.         });
复制代码


回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

QQ|Archiver|手机版|小黑屋|分享学习  

GMT+8, 2024-5-17 11:15 , Processed in 0.047421 second(s), 6 queries , File On.

声明:本站严禁任何人以任何形式发表违法言论!

本站内容由网友原创或转载,如果侵犯了您的合法权益,请及时联系处理!© 2017 zamxqun@163.com

皖公网安备 34010402700634号

皖ICP备17017002号-1

快速回复 返回顶部 返回列表