当前位置: 首页 > ds >正文

qml加载html以及交互

qml加载html,采用webchannel进行通信,需绑定对象

1、main.cpp

#include <QGuiApplication>
#include <QQmlApplicationEngine>
#include <QWebChannel>
#include <QQmlContext>int main(int argc, char *argv[]) {QGuiApplication::setAttribute(Qt::AA_EnableHighDpiScaling);QGuiApplication app(argc, argv);QQmlApplicationEngine engine;engine.load(QUrl(QStringLiteral("qrc:/main.qml")));return app.exec();
}

2、main.qml

import QtQuick 2.15
import QtWebEngine 1.10
import QtWebChannel 1.0
import QtQuick.Window 2.15
import QtQuick.Controls 2.15Window {width: 800height: 600visible: trueQtObject {id: bridgeproperty string currentTime: new Date().toTimeString()WebChannel.id: "qmlBridge"  // JS object// signal for js listensignal callJs(string strData);// html -> QMLfunction showMessage(msg) {console.log("Js invoke: ", msg)}// QML -> htmlfunction callJavaScript(message) {console.log("Invoke js func " )callJs(message)currentTime = new Date().toTimeString()// invoke js functionwebview.runJavaScript("getResult(5, 10);", function(result) {console.log("JS returns: ", result)})}}WebChannel {id: webChannelregisteredObjects: [bridge]}WebEngineView {id: webviewanchors.fill: parentwebChannel: webChannelurl: "qrc:/html/index.html"Component.onCompleted: {}}Button {text: "Invoke js"anchors.bottom: parent.bottomonClicked: {bridge.callJavaScript(new Date().toLocaleTimeString());}}
}

3、html

<!DOCTYPE html>
<html>
<head><meta charset="utf-8"><script src="qrc:/html/qwebchannel.js"></script><script>// global functionfunction getResult(a, b) {return a + b;}// functionfunction handleQMLmsg(message) {alert(message)document.getElementById("time").innerText = message;}window.onload = function() {// init QWebChannelnew QWebChannel(qt.webChannelTransport, function(channel) {const bridge = channel.objects.qmlBridge;// invoke QMLdocument.getElementById("btn").onclick = function() {bridge.showMessage("HTML invoke QML");};// bind qml signalbridge.callJs.connect(handleQMLmsg);// bind qml properitybridge.currentTimeChanged.connect(function() {//alert(bridge.currentTime)//document.getElementById("time").innerText = bridge.currentTime;});});};</script>
</head>
<body><h2>Qml interactive with html</h2><p>QML invoke js: <span id="time"></span></p><button id="btn">Invoke QML</button>
</body>
</html>
http://www.xdnf.cn/news/15115.html

相关文章:

  • Qt去噪面板搭建
  • Flutter基础(前端教程⑦-Http和卡片)
  • 【EGSR2025】材质+扩散模型+神经网络相关论文整理随笔(二)
  • 图片的拍摄创建日期怎么改?保护好图片信息安全的好方法
  • 李宏毅NLP-9-语音转换
  • 全球发展币GDEV:从中国出发,走向全球的数字发展合作蓝图
  • 本地Qwen中医问诊小程序系统开发
  • kubernetes存储入门
  • Flutter编译安卓应用时遇到的compileDebugJavaWithJavac和compileDebugKotlin版本不匹配的问题
  • 【c++学习记录】状态模式,实现一个登陆功能
  • huggingface笔记:文本生成Text generation
  • WinUI3入门16:Order自定义排序
  • WouoUI-Page移植
  • 一个vue项目的基本构成
  • 实时音视频通过UDP打洞实现P2P优先通信
  • 方法论汇总
  • ACE-Step:AI音乐生成基础模型
  • 【python】 time_str = time_str.strip() 与 time_str = str(time_str).strip() 的区别
  • Mac安装Docker(使用orbstack代替)
  • 云原生详解:构建现代化应用的未来
  • 【Node.js】文本与 pdf 的相互转换
  • eslint扁平化配置
  • 牛市来临之际,如何用期权抢占反弹先机?
  • rabbitMQ读取不到ThreadLocal消息的bug
  • 如何利用机器学习(ML)检测异常登录行为
  • 视频号账号矩阵运营中定制开发开源 AI 智能名片 S2B2C 商城小程序的赋能研究
  • AR 双缝干涉实验亮相:创新科技实验范式,开拓 AR 技术新局​
  • 开源 python 应用 开发(三)python语法介绍
  • Linux操作系统:再谈虚拟地址空间
  • IT 技术领域创作者三周年纪念日