QML调用JS

发布时间:2026/11/2 18:37:34
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; }QMLimport 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) } }创作不易小小的支持一下吧

关于本文作者

来自尧图内容编辑团队

尧图内容编辑团队 内容团队

尧图内容编辑团队

本文由尧图网络内容编辑团队执笔。团队由资深项目经理、前端工程师与设计师组成,所有内容均来自亲手交付的真实项目,先讲清问题、再给出可落地的解法。尧图深耕北京网站建设十年,服务过京华建材集团、智造科技等各行业客户,把一线经验沉淀为可复用的行业观察。

  • 十年建站经验,覆盖建材、制造、服务、文创等
  • 项目经理把关选题与事实准确性
  • 工程师与设计师联合撰写专业细节
  • 统一编辑规范,保证文风与排版一致
  • 每月复盘转化数据,迭代选题方向

延伸阅读

相关资讯与近期热门内容

深度阅读推荐

建站决策前值得细读的三篇

网站改版的5个关键决策
2024-08-12

网站改版的5个关键决策

什么时候该改版、改到什么程度、如何避免流量掉光,京华建材集团改版复盘给出答案。

获取专属建站方案

看完文章,把您的行业与预算告诉我们,免费获取一份量身定制的官网建设方案与报价。

立即免费咨询