qt嵌入式html和本地c++通信方式

摘要:
直接将html图标嵌入到应用程序中,但我们需要将数据从后台C++端传输到html端以实现显示。Qt实现了相关方法程序的第一个截图。首先,查看客户端js代码//BEGINSETUP函数输出{varoutput=document.getElementById;output.inerHTML=output.innerHTML+message+”“;}window.onload=function(){output;newQWebChannel(qt.webChannelTransport,function(channel){//makedialogobjectaccessibleglobalyvarcontent=channel.objects.content;document.getElementById(“send”).onclick=function(){varianput=document.getElement ById(”input“);vartext=input.value;if(!text){return;}输出输入值=“”;所容纳之物receiveText;//该方法实际上是调用后台的一个插槽,后端C++通过该插槽接收数据。客户端将数据发送到后端}内容。sendText Connect(function(message){//sendText是后台发送数据的信号。此方法将后端C++信号与html页面函数链接,以将数据发送到后台的前端部分。

前沿:我们在做qt项目的时候,通常会把某个html网页直接显示到应用程序中。比如绘图。直接把html形式的图标嵌入到应用程序中

但是我们需要把数据从后台c++端传到html端,实现显示。qt实现了相关的方法

程序运行截图

qt嵌入式html和本地c++通信方式第1张

一。先看客户端js代码

    <script type="text/javascript" src="http://t.zoukankan.com/qwebchannel.js"></script>
    <script type="text/javascript">
        //BEGIN SETUP
        function output(message) {
            var output = document.getElementById("output");
            output.innerHTML = output.innerHTML + message + " ";
        }
        window.onload = function () {
            output("setting up QWebChannel.");
            new QWebChannel(qt.webChannelTransport, function (channel) {
                // make dialog object accessible globally
                var content = channel.objects.content;

                document.getElementById("send").onclick = function () {
                    var input = document.getElementById("input");
                    var text = input.value;
                    if (!text) {
                        return;
                    }
                    output("Sent message: " + text);
                    input.value = "";
                    content.receiveText(text); //该方法实际是调用的后台的slot,后端c++通过slot来接收数据。客户端向后台发数据
                }
                content.sendText.connect(function (message) {  //sendText是后台发送数据的信号signal,该方法实现把后端c++信号signal,与html页面function链接起来,实现后台向前段发送数据。c++端信号与html端的slot链接起来
                    output("Received message: " + message);
                });
                content.receiveText("Client connected, ready to send/receive messages!");
                output("Connected to WebChannel, ready to send/receive messages!");
            });
        }
        //END SETUP
    </script>

二,后台客户端

1.document实现方式

classDocument :publicQObject

{
    Q_OBJECT
    Q_PROPERTY(QString text MEMBER s_text NOTIFY sendText)
public:
    explicit Document(QObject *parent = nullptr) : QObject(parent) {}
    void setSendTextText(const QString &text);
    void setUi(Ui::MainWidget *ui);
public slots:
    void receiveText(const QString &r_text);
signals:
    void sendText(const QString &text); //这两个是重点,一个slot一个signal分别用于接收和发送数据,注意和前端js代码的对应
private:
    void displayMessage(const QString &message);
    QString s_text;
    QString recieve_text;
    Ui::MainWidget *mainUi;
};
document.cpp文件
/****************************************************************************
**
** Copyright (C) 2015 The Qt Company Ltd.
** Contact: http://www.qt.io/licensing/
**
** This file is part of the demonstration applications of the Qt Toolkit.
**
** $QT_BEGIN_LICENSE:LGPL$
** 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 as published by the Free Software
** Foundation and appearing in the file LICENSE.LGPL included in the
** packaging of this file. Please review the following information to
** ensure the GNU Lesser General Public License version 2.1 requirements
** will be met: 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.
**
** GNU General Public License Usage
** Alternatively, this file may be used under the terms of the GNU
** General Public License version 3.0 as published by the Free Software
** Foundation and appearing in the file LICENSE.GPL included in the
** packaging of this file. Please review the following information to
** ensure the GNU General Public License version 3.0 requirements will be
** met: http://www.gnu.org/copyleft/gpl.html.
**
**
** $QT_END_LICENSE$
**
****************************************************************************/
#include "document.h"
void Document::setSendTextText(const QString &text) //向html页面发送数据
{
    s_text = text;
    emit sendText(s_text);
}
void Document::displayMessage(const QString &message)//接收来自html页面的数据
{
      mainUi->editor->appendPlainText(message);
}
/*!
    This slot is invoked from the HTML client side and the text displayed on the server side.
*/
void Document::receiveText(const QString &r_text)
{
    displayMessage(QObject::tr("Received message: %1").arg(r_text));
}
void Document::setUi(Ui::MainWidget *ui)
{
    mainUi = ui;
}

二,使用document文件并创建webchannel
MainWidget::MainWidget(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::MainWidget)
{
    ui->setupUi(this);
//    PreviewPage *page = new PreviewPage(this);
//    ui->preview->setPage(page);
    m_content.setUi(ui);
    QWebChannel *channel = new QWebChannel(this);
    channel->registerObject(QStringLiteral("content"), &m_content);
    //page->setWebChannel(channel);
    ui->preview->page()->setWebChannel(channel);
    ui->preview->setUrl(QUrl("qrc:/HelloWorld2.html"));
    //ui->preview->setUrl(QUrl("qrc:/index.html"));
    ui->editor->setPlainText("hello...
");
}
这边是后端界面的ui文件,通过上述代码就是实现了c++端document类与客户端html通信。
详细的实例代码大家可以到一下链接下载:http://download.csdn.net/download/soft_123456/10115764
有任何问题欢迎及时联系我活着留言

免责声明:文章转载自《qt嵌入式html和本地c++通信方式》仅用于学习参考。如对内容有疑问,请及时联系本站处理。

上篇uniapp 组件使用mysql5.6和8.0中都没有len()函数,获取字符串长度的函数是length()下篇

宿迁高防,2C2G15M,22元/月;香港BGP,2C5G5M,25元/月 雨云优惠码:MjYwNzM=

相关文章

Ajax动态滚动加载数据

看新浪微博,人人网都有这样的效果:滚动条滚动到最下面的时候,新的数据就被自动加载出来了,今天亲自尝试了一下这个效果的实现。 最开始在CSDN上写了一版,功能比较简单,今天又增加了一个小功能:翻页到指定页数后,自动停止。用户点击继续查看后,再继续滚动。看看实现吧: 更新核心滚动代码: $(window).scroll(function(){...

QT QString类型转换为const char*(toLatin1)

Qstring str = "helloworld"; char *s; QByteArray ba = str.toLatin1(); s = ba.data(); toLatin1、toLocal8Bit都是QString转QByteArray的方法,Latin1代表ASCII,Local8Bit代表unicode。 const char* -- 指...

继承QAbstractTableModel QStyledItemDelegate实现自定义表格,添加进度条和选中框。

由于项目要求,需要实现一个列表目录显示信息,并且需要实现每一项提供进度条和选项框功能,所以需要继承QAbstractTableModel和QStyledItemDelegate进行自定义。 -自定义数据 itemdata.h #ifndef ITEMDATA_H #define ITEMDATA_H #include <QMetaType> #...

python 文件操作

读取文件:r,只读不能写,文件不存在报错 #打开文件 file_object=open('log.txt',mode="r", encoding='utf-8')#读取:mode为r,只读不能写,文件不存在,报错 # 读取内容, content=file_object.read() print(content) #关闭文件 file_object....

我与Python惺惺相惜

while循环 死循环,会一直一直的重复循环.True的首字母要大写. while True: print("我与世界格格不入,我只与你惺惺相惜") count = 1 while count <=5: print("我与世界各格格不入,我只与你惺惺相惜") count = count + 1 解析: count=1,进行判断1小于等于5条件成立...

Qt之启动外部程序(使用参数很全面,还使用了setProcessChannelMode)

简述 QProcess可以用来启动外部程序,并与它们交互。 要启动一个进程,通过调用start()来进行,参数包含程序的名称和命令行参数,参数作为一个QStringList的单个字符串。 另外,也可以使用setProgram()和setArguments()来运行,然后调用start()或open()。 简述 接口 示例 cmd 启动cmd cmd...