Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/368.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
Javascript 获取对QML中动态创建的图表视图的引用_Javascript_Qt_Qml_Qt5_Qtquick2 - Fatal编程技术网

Javascript 获取对QML中动态创建的图表视图的引用

Javascript 获取对QML中动态创建的图表视图的引用,javascript,qt,qml,qt5,qtquick2,Javascript,Qt,Qml,Qt5,Qtquick2,我正在尝试获取对动态创建的ChartView对象的引用。在代码中,您将看到我在单击“添加图表”按钮时动态创建了一个图表作为代理 import QtQuick 2.12 import QtQuick.Window 2.12 import QtCharts 2.3 import QtQuick.Controls 2.4 Window { visible: true width: 1200 height: 800 title: "Charts" ListMo

我正在尝试获取对动态创建的ChartView对象的引用。在代码中,您将看到我在单击“添加图表”按钮时动态创建了一个图表作为代理

import QtQuick 2.12
import QtQuick.Window 2.12
import QtCharts 2.3
import QtQuick.Controls 2.4

Window {
    visible: true
    width: 1200
    height: 800
    title: "Charts"

    ListModel {
        id: modelId
    }

    Rectangle {
        id: rectId
        color: "pink"
        anchors.fill: parent

        GridView {
            id: mGridViewId
            anchors.fill: parent
            cellWidth: 300; cellHeight: 300
            model: modelId
            delegate: Rectangle {
                width: mGridViewId.cellWidth;
                height: mGridViewId.cellHeight
                color: mColor

                ChartView {
                    width: parent.width;
                    height: parent.height

                    LineSeries {
                        name: "LineSeries"
                        XYPoint { x: 0; y: 0 }
                        XYPoint { x: 1.1; y: 2.1 }
                        XYPoint { x: 1.9; y: 3.3 } 
                    }

                }
            }
        }
    }

    Column {
        anchors.centerIn: parent
        Row {
            Button {
                text: "add chart"

                onClicked: {                    
                   modelId.append({'mColor': 'blue'})
                }
            }


            Button {
                text: "remove chart"

                onClicked: {
                    modelId.remove(0)
                }
            }
        }

        Button {
            anchors.horizontalCenter: parent.horizontalCenter
            text: "add line series"

            onClicked: {
                var chart = modelId.get(0)
                chart.removeAllSeries();
            }
        }

    }

}
我可以通过以下方式获得模型特定项目的参考:

var chart = modelId.get(0)
但是,它既不是矩形,也不是图表视图。因此,如果我想在动态创建的图表中添加线条系列,或删除线条系列,如下所示:

onClicked: {
    var chart = modelId.get(0)
    chart.removeAllSeries();
}
我无法将该对象视为QML对象。我得到一个错误:

qrc:/main.qml:80:TypeError:对象的属性“removeAllSeries” QObject(0x7fd35d996b50)不是函数

我不确定我做错了什么,或者我是否需要以完全不同的方式进行操作,即不使用ListView模型委托,而是动态创建QML对象并将对它们的引用存储在数组中

谢谢你的帮助,我很感激


--我找到了答案,把答案贴在这里供未来的书呆子们参考

我需要访问GridView的contentItem属性。我将此函数添加到GridView

function getDelegateInstanceAt(index) {
    return contentItem.children[index];
}
要修改特定的委托,请调用传递索引的函数

onClicked: {
    var chart = mGridViewId.getDelegateInstanceAt(2);
    chart.removeAllSeries();
}

E、 如果我理解正确,您就不需要调用正在创建的单个ChartView qml对象的removeAllSeries()

但是

modelId.get(0)
不是ChartView qml对象,它只是您添加的数据元素。 因此,您得到的错误是有效的,因为您试图访问ListModel的属性

如果您这样做:

onClicked: {
    var chart = modelId.get(0)
    var color = chart.mColor
    console.log(color)
    // blue
}
必须使用GridView的currentItem和/或currentIndex来查找正确的ChartView对象

onClicked: {
    //Gridview -> Rectangle -> ChartView -> method
    mGridViewId.currentItem.visibleChildren[0].removeAllSeries()
}
您可以去掉额外的矩形,而不必处理可见的子对象