如何在 QML 中创建 CandlestickSet 对象实例?

How to create CandlestickSet object instance in QML?

我可以更改 CandlestickSeries 个元素,但不能在 QML 中添加元素:

ColumnLayout
{
    spacing: 5
    anchors.fill: parent

    ChartView {
        title: "Candlestick series"
        theme: ChartView.ChartThemeLight
        legend.alignment: Qt.AlignBottom
        antialiasing: true
        Layout.fillWidth: true
        Layout.fillHeight: true

        CandlestickSeries {
            id: series
            name: "Acme Ltd."
            increasingColor: "green"
            decreasingColor: "red"

            CandlestickSet { timestamp: 1435708800000; open: 6.90; high: 6.94; low: 5.99; close: 6.60 }
            CandlestickSet { timestamp: 1435795200000; open: 6.69; high: 6.69; low: 6.69; close: 6.69 }
            CandlestickSet { timestamp: 1436140800000; open: 4.85; high: 6.23; low: 4.85; close: 6.00 }
            CandlestickSet { timestamp: 1436227200000; open: 5.89; high: 6.15; low: 3.77; close: 5.69 }
            CandlestickSet { timestamp: 1436313600000; open: 4.64; high: 4.64; low: 2.54; close: 2.54 }
        }
    }

    RowLayout
    {
        Layout.margins: 10
        spacing: 5

        Button
        {
            text: "Change 2"
            onClicked: series.at(2).open = 3
        }

        Button
        {
            text: "Change 4"
            onClicked: series.at(4).open = 3
        }

        Button
        {
            id: addButton
            text: "Add"
            onClicked: series.append( /*create new CandlestickSet here*/ )
        }
    }
}

如何在 addButton 按钮单击处理程序中创建 CandlestickSet 对象?

你必须使用 Qt.createQmlObject:

onClicked: {
    var set = Qt.createQmlObject('import QtCharts 2.15; CandlestickSet {}', series, "dynamicSnippet1");
    set.timestamp = 1436227300000; 
    set.open = 6.90
    set.high = 6.94
    set.low = 5.99
    set.close = 6.60
    var ret = series.append(set)
}