资讯详情

QML调用JS

📅 2026/11/2 18:36:40 | 华诺云谱 👁 阅读
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) } }创作不易小小的支持一下吧
📝

华诺云谱内容团队

资深建站顾问 · 行业研究员

10年+企业数字化服务经验,专注智能建站、SEO优化与品牌营销,持续输出建站技巧、行业洞察与营销干货,已帮助5000+企业实现数字化增长。

你可能需要的服务

订阅华诺云谱资讯周报

每周一封,精选建站技巧、SEO与营销干货,直达邮箱。已有 8,000+ 企业主订阅,助你少走弯路。

↑