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

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.dtcms.com/a/270771.html

相关文章:

  • python安装pandas模块报错问题
  • Opencv探索之旅:从像素变化到世界轮廓的奥秘
  • Adobe Illustrator 2025 安装图文教程 | 快速上手平面设计
  • 让AI绘图更可控!ComfyUI-Cosmos-Predict2基础使用指南
  • 分治算法---快排
  • ts学习1
  • 宏集案例 | 基于CODESYS的自动化控制系统,开放架构 × 高度集成 × 远程运维
  • 打破传统,开启 AR 智慧课堂​
  • react16-react19都更新哪些内容?
  • 【LeetCode 热题 100】136. 只出现一次的数字——异或
  • Deepoc具身智能大模型:送餐机器人如何学会“读心术”
  • Java结构型模式---装饰者模式
  • Vue3 Element plus table有fixed列时错行
  • Embarcadero Delphi 12.3 Crack
  • C++ 中最短路算法的详细介绍
  • B站排名优化:从算法密码到流量密钥的全方位解析
  • vue快速上手
  • 前端开发自动化设计详解
  • 【牛客刷题】游游的字母串
  • 2023年IEEE TITS SCI2区TOP,增强回溯搜索算法EBSA+多无人机辅助商业包裹递送系统飞行规划,深度解析+性能实测
  • NLP:初识RNN模型(概念、分类、作用)
  • HarmonyOS应用开发者高级试题2025年7月部分单选题
  • 【深度学习】【入门】Sequential的使用和简单神经网络搭建
  • Selenium+Pytest自动化测试框架实战前言#
  • 使用LLaMA-Factory微调Qwen2.5-VL-3B 的目标检测任务-数据集格式转换(voc 转 ShareGPT)
  • Mac mini 高性价比扩容 + Crossover 游戏实测 全流程手册
  • SpringCloud系列 - Seata 分布式事务(六)
  • AJAX 学习
  • 如何将华为手机中的照片传输到电脑
  • Django核心知识点详解:JSON、AJAX、Cookie、Session与用户认证