javascript - 获取对 QML 中动态创建的 ChartView 的引用

标签 javascript qt qml qt5 qtquick2

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

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)

但是它不被视为矩形或图 TableView 。因此,如果我想做一些事情,比如将 LineSeries 添加到动态创建的图表之一,或者像这样删除 LineSeries:

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

我无法将该对象视为 QML 对象。我得到一个错误:

qrc:/main.qml:80: TypeError: Property 'removeAllSeries' of object QObject(0x7fd35d996b50) is not a function

我不确定我做错了什么,或者我是否需要以完全不同的方式解决这个问题,即不使用 ListView-Model-Delegate 而是动态创建 QML 对象并将对它们的引用存储在数组中.

感谢您的帮助,我很感激。

--E

最佳答案

我想通了,把答案贴在这里供以后的 Nerd 使用。

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

function getDelegateInstanceAt(index) {
    return contentItem.children[index];
}

并修改特定委托(delegate)调用传递索引的函数

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

关于javascript - 获取对 QML 中动态创建的 ChartView 的引用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55123275/

相关文章:

javascript - mongodb - 不明白为什么/如何使用 map-reduce

qt - 为什么我的 Qt5 语言翻译未加载?

javascript - 更改星星颜色 onClick - 为什么这不起作用?

javascript - 第三方REST API基础认证

javascript - 如何使用 URL 传递变量

c++ - C++/QML 应用程序启动后几秒钟调用特定函数

python - pyqt4 + 单选按钮

qt - TabView 中进入/退出事件的信号

QtQuick - 使 TableView 在其模型更改后更新其 rowCount

c++ - 如何在 C++ 文件中声明的 qml 文件中使用信号