Qt5与Javascript交互

本文介绍如何使用Qt5.7.1与JavaScript进行混合编程。通过示例代码详细讲解了如何配置开发环境、实现Qt与JavaScript之间的数据交换,并解决了实际应用中可能遇到的编码问题。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Qt5与Javascript交互

本文记录Qt5.7.1与JavaScript进行混合编程的例子,网上这些例子基本也是大同小异,我也是参考了网上的一些做法,但很多时候你需要自己手动做一遍才能真正理解和明白其中的过程,同时我发现有些例子的某些操作挺迷惑人——真的需要这样做吗?是否可以更加简单一点,更加清晰地描述其中的过程?下面是我经过修改的例子。

本文使用开发环境是VS2013+Qt5.7.1,然后安装qt-vs-addin-1.2.5.exe插件。使用到的Qt5WebEngine作为网页加载显示,然后用QtWebChannel作为Qt与Javascript交互通讯的桥梁,这也是官方推荐的方法。

运行界面如下:


一、项目配置
新建项目之后,选择Qt Application,然后模块选择如下:
需要注意的是Qt5WebEngine需要手动添加,右键项目属性,然后:
(1)配置属性->c/c++的常规选项->附加包含目录,添加头文件路径
$(QTDIR)\include\QtWebEngineWidgets
(2)配置属性->链接器->输入,添加库文件(Debug配置)
Qt5WebEngined.lib

二、Qt项目源码
其中,JsWidget是窗体显示类,InteractObject是交互类。

main.cpp文件:
#include "JsWidget.h"
#include <QtWidgets/QApplication>
#include <QTextCodec>

int main(int argc, char *argv[])
{
	QTextCodec::setCodecForLocale(QTextCodec::codecForName("utf-8"));

	QApplication a(argc, argv);
	JsWidget w;
	w.show();
	return a.exec();
}

JsWidget.h文件:
#ifndef JSWIDGET_H
#define JSWIDGET_H
#pragma execution_character_set("utf-8")

#include <QtWidgets/QWidget>
#include <QWebEngineView>
#include <QWebChannel>
#include <QPlainTextEdit>
#include <QLineEdit>
#include <QPushButton>
#include "InteractObject.h"

class JsWidget : public QWidget
{
	Q_OBJECT

public:
	JsWidget(QWidget *parent = 0);
	~JsWidget();

public:
	void appendContent(const QString &text);

private slots:
	void onSendSlot();

private:
	QWebEngineView *_webView;
	InteractObject *_interactObj;

	QPlainTextEdit *_edtContent;
	QLineEdit *_edtInput;
	QPushButton *_btnSend;
};

#endif // JSWIDGET_H

JsWidget.cpp文件
#include "JsWidget.h"
#include <QHBoxLayout>
#include <QVBoxLayout>
#include <QGroupBox>
#include <QFile>
#include <QDir>

JsWidget::JsWidget(QWidget *parent)
	: QWidget(parent)
{
	_interactObj = new InteractObject(this);

	// qt界面
	_edtContent = new QPlainTextEdit(this);
	_edtContent->setMinimumWidth(400);
	_edtInput = new QLineEdit(this);
	_btnSend = new QPushButton("发送文本", this);
	connect(_btnSend, SIGNAL(clicked()), this, SLOT(onSendSlot()));

	QHBoxLayout *hLayout1 = new QHBoxLayout();
	hLayout1->addWidget(_edtInput, 1);
	hLayout1->addWidget(_btnSend);
	QVBoxLayout *vLayout1 = new QVBoxLayout();
	vLayout1->addWidget(_edtContent, 1);
	vLayout1->addLayout(hLayout1);
	QGroupBox *qtGroup = new QGroupBox("Qt界面", this);
	qtGroup->setLayout(vLayout1);

	// web界面
	QWebChannel *channel = new QWebChannel(this);
	channel->registerObject(QStringLiteral("interactObj"), _interactObj);
	_webView = new QWebEngineView(this);
	_webView->page()->setWebChannel(channel);

	QUrl baseUrl = QUrl::fromLocalFile(QDir::current().absoluteFilePath("."));
	QFile file("test.html");
	if (file.open(QIODevice::ReadOnly)){
		QTextStream stream(&file);
		QString content = stream.readAll();
		_webView->setHtml(content, baseUrl);
	}
	_webView->show();
	QVBoxLayout *vLayout2 = new QVBoxLayout();
	vLayout2->addWidget(_webView, 1);
	QGroupBox *webGroup = new QGroupBox("Web界面", this);
	webGroup->setLayout(vLayout2);

	// 布局
	QHBoxLayout *mainLayout = new QHBoxLayout();
	mainLayout->addWidget(qtGroup);
	mainLayout->addWidget(webGroup);
	this->setLayout(mainLayout);

	this->setMinimumSize(800, 600);
}

void JsWidget::appendContent(const QString &text)
{
	_edtContent->appendPlainText(text);
}

JsWidget::~JsWidget()
{

}

void JsWidget::onSendSlot()
{
	QString text = _edtInput->text().trimmed();
	if (text.isEmpty())
		return;

	_interactObj->sendStringToHtml(text);
	_edtInput->setText("");
}

InteractObject.h文件:
#ifndef __INTERACT_OBJECT_H__
#define __INTERACT_OBJECT_H__

#include <QObject>
#include <QString>

class JsWidget;

class InteractObject : public QObject
{
	Q_OBJECT

public:
	InteractObject(JsWidget *parent);

	void sendStringToHtml(const QString &text);

	// 槽
public slots:
	// 可以被javascript调用
	void recvStringSlot(const QString &text);

	// 信号
signals:
	// 在javascript文件里面定义链接函数,
	// 然后发送信号触发javascript函数
	void sendStringSignal(const QString &text);

private:
	QString _text;
	QString _recieveText;
	JsWidget *_jsWidget;
};

#endif

InteractObject.cpp文件:
#include "InteractObject.h"
#include "JsWidget.h"


InteractObject::InteractObject(JsWidget *parent)
: QObject(parent)
, _jsWidget(parent)
{ 
	
}

void InteractObject::sendStringToHtml(const QString &text)
{
	emit sendStringSignal(text);
}

/*!
This slot is invoked from the HTML client side and the text displayed on the server side.
*/
void InteractObject::recvStringSlot(const QString &text)
{
	_jsWidget->appendContent("Received string: " + text);
}

三、Html与Javascript源码
在项目输出目录新建test.html,然后添加代码:
<!DOCTYPE html>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
        <script type="text/javascript" src="./qwebchannel.js"></script>
        <script type="text/javascript">
            // 输出信息
            function output(message)
            {
                var output = document.getElementById("output");
                output.innerHTML = output.innerHTML + message + "\n";
            }
		// 启动加载函数
         window.onload = function() {
            output("安装 QWebChannel.");
            new QWebChannel(qt.webChannelTransport, function(channel) {
                // 获取Qt类交互对象
                var interactObj = channel.objects.interactObj;
				// 按钮点击事件
                document.getElementById("send").onclick = function() {
                    var input = document.getElementById("input");
                    if (!input.value) {
                        return;
                    }
					// 调用Qt类公有函数
                    interactObj.recvStringSlot(input.value);
                    input.value = "";
                }
				// 链接Qt类信号函数
                interactObj.sendStringSignal.connect(function(str) {
                    output("Received string: " + str);
                });
				// 调用Qt类公有函数
                interactObj.recvStringSlot("Client connected, 准备发送、接收数据!");
            });
            }

            //END SETUP
        </script>
        <style type="text/css">
            html {
                height: 100%;
                width: 100%;
            }
            #input {
                width: 300px;
                margin: 0 10px 0 0;
            }
            #send {
                width: 90px;
                margin: 0;
            }
            #output {
                width: 400px;
                height: 300px;
            }
        </style>
    </head>
    <body>
        <textarea id="output"></textarea><br />
        <input id="input" />
		<input type="submit" id="send" value="发送文本" onclick="javascript:click();" />
    </body>
</html>

其中qwebchannel.js是官方自动例子的js文件
/****************************************************************************
**
** Copyright (C) 2015 The Qt Company Ltd.
** Copyright (C) 2014 Klarälvdalens Datakonsult AB, a KDAB Group company, info@kdab.com, author Milian Wolff <milian.wolff@kdab.com>
** Contact: http://www.qt.io/licensing/
**
** This file is part of the QtWebChannel module of the Qt Toolkit.
**
** $QT_BEGIN_LICENSE:LGPL21$
** Commercial License Usage
** Licensees holding valid commercial Qt licenses may use this file in
** accordance with the commercial license agreement provided with the
** Software or, alternatively, in accordance with the terms contained in
** a written agreement between you and The Qt Company. For licensing terms
** and conditions see http://www.qt.io/terms-conditions. For further
** information use the contact form at http://www.qt.io/contact-us.
**
** GNU Lesser General Public License Usage
** Alternatively, this file may be used under the terms of the GNU Lesser
** General Public License version 2.1 or version 3 as published by the Free
** Software Foundation and appearing in the file LICENSE.LGPLv21 and
** LICENSE.LGPLv3 included in the packaging of this file. Please review the
** following information to ensure the GNU Lesser General Public License
** requirements will be met: https://www.gnu.org/licenses/lgpl.html and
** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
**
** As a special exception, The Qt Company gives you certain additional
** rights. These rights are described in The Qt Company LGPL Exception
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
**
** $QT_END_LICENSE$
**
****************************************************************************/

"use strict";

var QWebChannelMessageTypes = {
    signal: 1,
    propertyUpdate: 2,
    init: 3,
    idle: 4,
    debug: 5,
    invokeMethod: 6,
    connectToSignal: 7,
    disconnectFromSignal: 8,
    setProperty: 9,
    response: 10,
};

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

    var channel = this;
    this.transport = transport;

    this.send = function(data)
    {
        if (typeof(data) !== "string") {
            data = JSON.stringify(data);
        }
        channel.transport.send(data);
    }

    this.transport.onmessage = function(message)
    {
        var data = message.data;
        if (typeof data === "string") {
            data = JSON.parse(data);
        }
        switch (data.type) {
            case QWebChannelMessageTypes.signal:
                channel.handleSignal(data);
                break;
            case QWebChannelMessageTypes.response:
                channel.handleResponse(data);
                break;
            case QWebChannelMessageTypes.propertyUpdate:
                channel.handlePropertyUpdate(data);
                break;
            default:
                console.error("invalid message received:", message.data);
                break;
        }
    }

    this.execCallbacks = {};
    this.execId = 0;
    this.exec = function(data, callback)
    {
        if (!callback) {
            // if no callback is given, send directly
            channel.send(data);
            return;
        }
        if (channel.execId === Number.MAX_VALUE) {
            // wrap
            channel.execId = Number.MIN_VALUE;
        }
        if (data.hasOwnProperty("id")) {
            console.error("Cannot exec message with property id: " + JSON.stringify(data));
            return;
        }
        data.id = channel.execId++;
        channel.execCallbacks[data.id] = callback;
        channel.send(data);
    };

    this.objects = {};

    this.handleSignal = function(message)
    {
        var object = channel.objects[message.object];
        if (object) {
            object.signalEmitted(message.signal, message.args);
        } else {
            console.warn("Unhandled signal: " + message.object + "::" + message.signal);
        }
    }

    this.handleResponse = function(message)
    {
        if (!message.hasOwnProperty("id")) {
            console.error("Invalid response message received: ", JSON.stringify(message));
            return;
        }
        channel.execCallbacks[message.id](message.data);
        delete channel.execCallbacks[message.id];
    }

    this.handlePropertyUpdate = function(message)
    {
        for (var i in message.data) {
            var data = message.data[i];
            var object = channel.objects[data.object];
            if (object) {
                object.propertyUpdate(data.signals, data.properties);
            } else {
                console.warn("Unhandled property update: " + data.object + "::" + data.signal);
            }
        }
        channel.exec({type: QWebChannelMessageTypes.idle});
    }

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

    channel.exec({type: QWebChannelMessageTypes.init}, function(data) {
        for (var objectName in data) {
            var object = new QObject(objectName, data[objectName], channel);
        }
        // now unwrap properties, which might reference other registered objects
        for (var objectName in channel.objects) {
            channel.objects[objectName].unwrapProperties();
        }
        if (initCallback) {
            initCallback(channel);
        }
        channel.exec({type: QWebChannelMessageTypes.idle});
    });
};

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

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

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

    var object = this;

    // ----------------------------------------------------------------------

    this.unwrapQObject = function(response)
    {
        if (response instanceof Array) {
            // support list of objects
            var ret = new Array(response.length);
            for (var i = 0; i < response.length; ++i) {
                ret[i] = object.unwrapQObject(response[i]);
            }
            return ret;
        }
        if (!response
            || !response["__QObject*__"]
            || response.id === undefined) {
            return response;
        }

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

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

        var qObject = new QObject( objectId, response.data, webChannel );
        qObject.destroyed.connect(function() {
            if (webChannel.objects[objectId] === qObject) {
                delete webChannel.objects[objectId];
                // reset the now deleted QObject to an empty {} object
                // just assigning {} though would not have the desired effect, but the
                // below also ensures all external references will see the empty map
                // NOTE: this detour is necessary to workaround QTBUG-40021
                var propertyNames = [];
                for (var propertyName in qObject) {
                    propertyNames.push(propertyName);
                }
                for (var idx in propertyNames) {
                    delete qObject[propertyNames[idx]];
                }
            }
        });
        // here we are already initialized, and thus must directly unwrap the properties
        qObject.unwrapProperties();
        return qObject;
    }

    this.unwrapProperties = function()
    {
        for (var propertyIdx in object.__propertyCache__) {
            object.__propertyCache__[propertyIdx] = object.unwrapQObject(object.__propertyCache__[propertyIdx]);
        }
    }

    function addSignal(signalData, isPropertyNotifySignal)
    {
        var signalName = signalData[0];
        var signalIndex = signalData[1];
        object[signalName] = {
            connect: function(callback) {
                if (typeof(callback) !== "function") {
                    console.error("Bad callback given to connect to signal " + signalName);
                    return;
                }

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

                if (!isPropertyNotifySignal && signalName !== "destroyed") {
                    // only required for "pure" signals, handled separately for properties in propertyUpdate
                    // also note that we always get notified about the destroyed signal
                    webChannel.exec({
                        type: QWebChannelMessageTypes.connectToSignal,
                        object: object.__id__,
                        signal: signalIndex
                    });
                }
            },
            disconnect: function(callback) {
                if (typeof(callback) !== "function") {
                    console.error("Bad callback given to disconnect from signal " + signalName);
                    return;
                }
                object.__objectSignals__[signalIndex] = object.__objectSignals__[signalIndex] || [];
                var idx = object.__objectSignals__[signalIndex].indexOf(callback);
                if (idx === -1) {
                    console.error("Cannot find connection of signal " + signalName + " to " + callback.name);
                    return;
                }
                object.__objectSignals__[signalIndex].splice(idx, 1);
                if (!isPropertyNotifySignal && object.__objectSignals__[signalIndex].length === 0) {
                    // only required for "pure" signals, handled separately for properties in propertyUpdate
                    webChannel.exec({
                        type: QWebChannelMessageTypes.disconnectFromSignal,
                        object: object.__id__,
                        signal: signalIndex
                    });
                }
            }
        };
    }

    /**
     * Invokes all callbacks for the given signalname. Also works for property notify callbacks.
     */
    function invokeSignalCallbacks(signalName, signalArgs)
    {
        var connections = object.__objectSignals__[signalName];
        if (connections) {
            connections.forEach(function(callback) {
                callback.apply(callback, signalArgs);
            });
        }
    }

    this.propertyUpdate = function(signals, propertyMap)
    {
        // update property cache
        for (var propertyIndex in propertyMap) {
            var propertyValue = propertyMap[propertyIndex];
            object.__propertyCache__[propertyIndex] = propertyValue;
        }

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

    this.signalEmitted = function(signalName, signalArgs)
    {
        invokeSignalCallbacks(signalName, signalArgs);
    }

    function addMethod(methodData)
    {
        var methodName = methodData[0];
        var methodIdx = methodData[1];
        object[methodName] = function() {
            var args = [];
            var callback;
            for (var i = 0; i < arguments.length; ++i) {
                if (typeof arguments[i] === "function")
                    callback = arguments[i];
                else
                    args.push(arguments[i]);
            }

            webChannel.exec({
                "type": QWebChannelMessageTypes.invokeMethod,
                "object": object.__id__,
                "method": methodIdx,
                "args": args
            }, function(response) {
                if (response !== undefined) {
                    var result = object.unwrapQObject(response);
                    if (callback) {
                        (callback)(result);
                    }
                }
            });
        };
    }

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

        if (notifySignalData) {
            if (notifySignalData[0] === 1) {
                // signal name is optimized away, reconstruct the actual name
                notifySignalData[0] = propertyName + "Changed";
            }
            addSignal(notifySignalData, true);
        }

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

                return propertyValue;
            },
            set: function(value) {
                if (value === undefined) {
                    console.warn("Property setter for " + propertyName + " called with undefined value!");
                    return;
                }
                object.__propertyCache__[propertyIndex] = value;
                webChannel.exec({
                    "type": QWebChannelMessageTypes.setProperty,
                    "object": object.__id__,
                    "property": propertyIndex,
                    "value": value
                });
            }
        });

    }

    // ----------------------------------------------------------------------

    data.methods.forEach(addMethod);

    data.properties.forEach(bindGetterSetter);

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

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

//required for use with nodejs
if (typeof module === 'object') {
    module.exports = {
        QWebChannel: QWebChannel
    };
}


四、其他
应用过程中会遇到编码问题,这里统一用utf-8编码,注意项目中的三处代码:
(1)main.cpp文件:QTextCodec::setCodecForLocale(QTextCodec::codecForName("utf-8"));
(2)JsWidget.h文件:#pragma execution_character_set("utf-8")
(3)html文件:<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />

五、项目下载
完整的项目源码 下载地址:
http://download.youkuaiyun.com/detail/sharetm/9757314








评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值