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

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

程序运行截图

一。先看客户端js代码

<script type="text/javascript" src="./qwebchannel.js"></script>
    <script type="text/javascript">
        //BEGIN SETUP
        function output(message) {
            var output = document.getElementById("output");
            output.innerHTML = output.innerHTML + message + "\n";
        }
        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实现方式

class Document : public QObject

{
    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...\n");
}
这边是后端界面的ui文件,通过上述代码就是实现了c++端document类与客户端html通信。
详细的实例代码大家可以到一下链接下载:http://download.csdn.net/download/soft_123456/10115764有任何问题欢迎及时联系我活着留言

qt嵌入式html和本地c++通信方式的更多相关文章

  1. 转:Qt 嵌入式开发环境搭建

    地址: http://www.cnblogs.com/lishixian/articles/3013897.html         作者:lsx_007 这里主要是记录了自己在搭建嵌入式开发环境时阅 ...

  2. ubuntu 12.04 下搭接Qt 嵌入式开发环境

    1.安装前的准备工作 (1)有ubuntu12.04 的系统镜像(也可以其他linux 如Fedorea9),都是安装好的 (2)虚拟机VMWare 或 VirtualBox ,两者都可以,都是安装好 ...

  3. Qt基于FFmpeg播放本地 H.264(H264)文件(灿哥哥的博客)

    最近在弄H264的硬件编解码,基于DM3730,但是为了调试方便,在小红帽上用FFmpeg实现了H264的软件编解码.现在弄了一个Windows的例子,给需要的同学参考一下,如果大家觉得有帮助,可以小 ...

  4. Qt嵌入式开发环境搭建

    一.Qt版本介绍 按照不同的图形界面来划分,分为四个版本: 1.Win32版:适用于windows平台 2.X11版:适用于各种X系统的Linux和Unix平台 3.Mac版:适用于苹果的MacOS ...

  5. Qt、Qte与Qtopia(Qt嵌入式的发展历程)

    Qt的授权是分为两条线,商业版和开源版.如果使用商业版的Qt,那么开发出的程序可以是私有的和商业的:如果使用的是开源版的Qt,由于其使用的是GPL协议,那么可发出的程序也必须是GPL的.不过自从qt ...

  6. Qt(QtWebEngine)加载本地网页跨域问题的总结

    目录 1. 概述 2. 详论 2.1. 传参 2.2. JS module 3. 建议 4. 参考 1. 概述 浏览器直接加载本地网页的时候,如果网页涉及到加载本地资源(如图片),会出现跨域的问题.Q ...

  7. 用Qt开发Web和本地混合的应用

    QtWebkit 模块使得Qt widget能够通过HTML的object标签嵌入到web页面中,并通过JavaScript代码进行访问,而Qt对象也能相应的访问web页面元素. 将Qt对象插入到we ...

  8. qt webengineview 加载本地资源方式

    一.如果把资源添加到本地资源qrc库里了,请使用 ui->preview->setUrl(QUrl("qrc:/HelloWorld2.html")): 二.如果没有现 ...

  9. 开篇-QT完全手册

    嵌入式工具Qt的安装与使用 摘要 Qt是Trolltech公司的一个产品.Trolltech是挪威的一家软件公司,主要开 发两种产品:一种是跨平台应用程序界面框架:另外一种就是提供给做嵌入式Linux ...

随机推荐

  1. 数据重塑图解—Pivot, Pivot-Table, Stack and Unstack

    Pivot pivot函数用于创建一个新的派生表,该函数有三个参数:index, columns和values.你需要在原始表中指定这三个参数所对定的列名,接下来pivot函数会创建一个新的表格,其中 ...

  2. orcal 中的orcal用法

    ROWID是数据的详细地址,通过rowid,Oracle可以快速的定位某行具体的数据的位置. ROWID可以分为物理rowid和逻辑rowid两种.普通的堆表中的rowid是物理rowid,索引组织表 ...

  3. BS架构和CS架构

    B:browser  浏览器 S:server       服务器 C:client        客户端 BS:浏览器和服务器的关系,通过浏览器来访问服务器.比如:新浪.百度.等等. 优点:只要有浏 ...

  4. 修改Windows 2008以后系统的NTP服务设置

    @echo off echo autor OAK @echo off echo -------------------------------- @echo off echo setup time r ...

  5. python-unittest模块中的各类断言

    unittest中断言主要有三种类型: 基本的布尔断言,即:要么正确,要么错误的验证 比较断言,如比较两个变量的值(跟上面的布尔断言区别不大,主要是通过比较两个变量的值得出布尔值) 复杂断言(一般用的 ...

  6. 线性模型-线性回归、Logistic分类

    线性模型是机器学习中最简单的,最基础的模型结果,常常被应用于分类.回归等学习任务中. 回归和分类区别: 回归:预测值是一个连续的实数: 分类:预测值是离散的类别数据. 1.     线性模型做回归任务 ...

  7. 【VS开发】【智能语音处理】DTW算法(语音识别)

    DTW主要是应用在孤立词识别的算法,用来识别一些特定的指令比较好用,这个算法是基于DP(动态规划)的算法基础上发展而来的.这里介绍语音识别就先介绍下语音识别的框架,首先我们要有一个比对的模版声音,然后 ...

  8. javascrpt的string和Boolean类型

    string类型用于表示由零或多个16位unicode字符组成字符序列,即 字符串,字符可以由双引号(“)或单引号(‘)表示 tostring()与 string() 语法:str.tostring( ...

  9. Sqlserver 2012附加数据库时出错提示操作系统错误5(拒绝访问)错误5120的解决办法

    环境: Win10系统 SQLSERver 2012 情况: 使用混合登陆方式,sa账户密码正确登陆后,附加.mdf文件出现此错误. 尝试解决方法一:使用管理员运行SQLSERver2012,sa账户 ...

  10. IDEA 双击只选择了一个变量的某部分单词

    1,很多抄袭文章说  在keymap 里搜索 select Word at caret , 鄙视手动抄袭和编写爬虫来 作恶的开发者. 2,自己试了,File菜单 ---->  settings- ...