news 2026/11/2 18:35:56

QML调用JS

作者头像

张小明

前端开发工程师

1.2k 24
文章封面图
QML调用JS
JS
.pragma library var scope = { } function call(msg) { var exp = msg.toString(); console.log(exp) var data = { expression : msg } try { var fun = new Function('return (' + exp + ');'); data.result = JSON.stringify(fun.call(scope), null, 2) console.log('scope: ' + JSON.stringify(scope, null, 2) + 'result: ' + result) } catch(e) { console.log(e.toString()) data.error = e.toString(); } return data; }
QML
import QtQuick 2.5 import QtQuick.Controls 1.5 import QtQuick.Layouts 1.2 import QtQuick.Window 2.2 import "jsconsole.js" as Util ApplicationWindow { id: root title: qsTr("JSConsole") width: 640 height: 480 menuBar: MenuBar { Menu { title: qsTr("File") MenuItem { text: qsTr("Exit") onTriggered: Qt.quit(); } } } ColumnLayout { anchors.fill: parent anchors.margins: 9 RowLayout { Layout.fillWidth: true TextField { id: input Layout.fillWidth: true focus: true onAccepted: { root.jsCall(input.text) } } Button { text: qsTr("Send") onClicked: { root.jsCall(input.text) } } } Item { Layout.fillWidth: true Layout.fillHeight: true Rectangle { anchors.fill: parent color: '#333' border.color: Qt.darker(color) opacity: 0.2 radius: 2 } ScrollView { id: scrollView anchors.fill: parent anchors.margins: 9 ListView { id: resultView model: ListModel { id: outputModel } delegate: ColumnLayout { width: ListView.view.width Label { Layout.fillWidth: true color: 'green' text: "> " + model.expression } Label { Layout.fillWidth: true color: 'blue' text: "" + model.result } Rectangle { height: 1 Layout.fillWidth: true color: '#333' opacity: 0.2 } } } } } } function jsCall(exp) { var data = Util.call(exp); outputModel.insert(0, data) } }

创作不易,小小的支持一下吧!

版权声明: 本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若内容造成侵权/违法违规/事实不符,请联系邮箱:809451989@qq.com进行投诉反馈,一经查实,立即删除!
网站建设 2026/11/2 18:24:26

从零搭建VoiceStudio:录音环境、设备调试与后期处理全攻略

从零搭一个能用的 VoiceStudio:录音、调试到后期的完整实操记录标题里那个 VoiceStudio,说白了就是一套"从声音进到声音出"的完整链路。很多人一提录音就以为是买个好麦克风的事,实际上话筒只是最前端的一环,环境、声卡…

作者头像 李华
网站建设 2026/11/2 18:23:26

COMSOL计算陈数的完整指南:从Berry曲率到能带拓扑不变量的工程实现

简介:这份资料围绕光子晶体能带计算与陈数分析,面向使用Comsol Multiphysics和MATLAB开展光子晶体研究的初学者与科研人员,重点解决从三维建模、周期边界设置到能带结构提取的全流程问题。压缩包共5个文件,包含2份PDF文档&#xf…

作者头像 李华
网站建设 2026/11/2 18:23:05

VoiceStudio全流程指南:从录音环境到后期处理的系统性方案

VoiceStudio 这个词,说白了就是一套”把声音干净、专业地录下来,并且高效做成成品”的完整工作流。无论是做播客、配音、有声书录制,还是给短视频配旁白,整个链路无非是:环境拾音、设备链路、软件录制、后期处理、导出…

作者头像 李华
网站建设 2026/11/2 18:20:39

AI安全风险与2028年智能危机应对策略

1. 项目背景与现象分析2028年全球智能危机这个话题最近在各大社交平台和科技论坛引发热议。作为一个长期关注AI发展的从业者,我注意到这个话题的传播呈现出几个典型特征:首先,这个话题的传播速度异常迅猛。从最初在Reddit的AI板块出现&#x…

作者头像 李华
网站建设 2026/11/2 18:20:22

OpenRig:用铝型材和3D打印打造模块化DIY支架系统

如果你和我一样,家里同时堆着直播监视器、摄像机和两盏补光灯,桌上还摆了 NAS、显示器和几台迷你主机,大概很快会对“支架”这两个字产生生理性厌恶。成品的 photo rig 要么尺寸卡得很死,要么配套附件价格一路走高,更难…

作者头像 李华
网站建设 2026/11/2 18:15:57

电容选型与滤波电路设计:从原理到布局的实战指南

电容这个元件,说它简单是真简单,两片金属夹一层介质,连个半导体结都没有;说它难也是真难,我见过不少入行一两年的朋友,电阻电感玩得挺溜,一到电容选型就开始拍脑袋,滤波电路一上电就…

作者头像 李华