Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/qt/7.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何将自定义范围注入QML/Javascript控制台_Javascript_Qt_Qml_Qtquick2 - Fatal编程技术网

如何将自定义范围注入QML/Javascript控制台

如何将自定义范围注入QML/Javascript控制台,javascript,qt,qml,qtquick2,Javascript,Qt,Qml,Qtquick2,我发现了一个非常好的学习QML的资源。我目前正在将他们的建议应用到我的QML应用程序中。但是,我正在努力将控制台应用到我的应用程序范围中 摘自QML手册/第13页-Javascript // jsconsole.js .pragma library var scope = { // our custom scope injected into our function evaluation // <<--- How do I inject the "custom" scop

我发现了一个非常好的学习QML的资源。我目前正在将他们的建议应用到我的QML应用程序中。但是,我正在努力将控制台应用到我的应用程序范围中

摘自QML手册/第13页-Javascript

// jsconsole.js
.pragma library

var scope = {
  // our custom scope injected into our function evaluation
  // <<--- How do I inject the "custom" 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;
}
例如,如何将根组件或contactsModel作为自定义范围注入

如果你能帮助我,我将不胜感激

谢谢

import QtQuick 2.4
import QtQuick.Controls 1.3
import QtQuick.Window 2.2
import QtQuick.Dialogs 1.2

ApplicationWindow {
    title: qsTr("Hello World")

    width: 640
    height: 480
    visible: true

    ListModel {
        id: contactsModel
        ListElement {
            name: "Bill Smith"
            position: "Engineer"
        }
        ListElement {
            name: "John Brown"
            position: "Engineer"
        }
        ListElement {
            name: "Sam Wise"
            position: "Manager"
        }
    }

    ListView{
        id: contactsView
        anchors.left: parent.left
        anchors.top: parent.top
        anchors.margins: 10
        width: parent.width
        height: parent.height
        orientation: Qt.Vertical
        spacing: 10
        model: contactsModel
        delegate: Rectangle{
            width: 150
            height: 30
            border.color: "black"
            border.width: 1
            Text{
                text: name
            }
        }
    }
}