【问题标题】:QML QtCharts CandlestickSeries RepeaterQML QtCharts CandlestickSeries 中继器
【发布时间】:2021-05-22 14:02:18
【问题描述】:

我想用来自自定义类的数据填充CandlestickSeries

我以为我会像往常一样使用Repeater,但它似乎不起作用:

ChartView {
    title: "Candlestick Series"
    width: 400
    height: 300

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

        /*
        CandlestickSet { timestamp: 1435708800000; open: 690; high: 694; low: 599; close: 660 }
        CandlestickSet { timestamp: 1435795200000; open: 669; high: 669; low: 669; close: 669 }
        CandlestickSet { timestamp: 1436140800000; open: 485; high: 623; low: 485; close: 600 }
        CandlestickSet { timestamp: 1436227200000; open: 589; high: 615; low: 377; close: 569 }
        CandlestickSet { timestamp: 1436313600000; open: 464; high: 464; low: 254; close: 254 }
        */

        Repeater {
            model: 100
            delegate: CandlestickSet {
                timestamp: 1000 * 60 * index + 1436313600000
                open: 400; high: 500; low: 300; close: 380
            }
        }
    }
}

注释掉的部分(直接取自documentation)工作正常。

基于Repeater 的代码不产生数据点。

如何动态填充 CandlestickSeries?

注意:我也试过an alternative approach using JS,但也失败了。

【问题讨论】:

    标签: qt qml qtcharts


    【解决方案1】:

    Repeater 不起作用,因为CandlestickSet 不是ComponentCandlestickSeries 有一个 append 方法。而createQmlObject可以用来动态创建qml对象。

    ChartView {
        title: "Candlestick Series"
        width: 400
        height: 300
    
        CandlestickSeries {
            name: "Acme Ltd."
            increasingColor: "green"
            decreasingColor: "red"
    
            property int setsModel: 100
    
            onSetsModelChanged: {
                clear();
                for (var index = 0; index < setsModel; ++index)
                    append(Qt.createQmlObject(
                        "import QtQuick 2.0; import QtCharts 2.12; " + 
                        "CandlestickSet { timestamp: " + (1000 * 60 * index + 1436313600000) + "; " 
                        "open: 400; high: 500; low: 300; close: 380}", 
                        null));
            }
        }
    }
    

    注意:Online Qml Compilers 不支持 QtCharts,所以我没有测试代码。

    【讨论】:

    • 错误:Qt.createQmlObject():缺少父对象
    • 传递 seriesCandlestickSeries 对象的 id)而不是 null 修复了上述错误,但图表上没有出现烛台。
    • 也是CandlestickSet,不是CandlesticksSet
    • @user140345 谢谢,我刚刚改了。
    【解决方案2】:

    Instantiator 可能有效,但我没有测试以下代码。

    Instantiator {
        model: 100
        delegate: CandlestickSet {
            timestamp: 1000 * 60 * index + 1436313600000
            open: 400; high: 500; low: 300; close: 380
        }
        onObjectAdded: series.insert(index, object)
        onObjectRemoved: series.remove(object)
    }
    
    ChartView {
        title: "Candlestick Series"
        width: 400
        height: 300
    
        CandlestickSeries {
            id: series
            name: "Acme Ltd."
            increasingColor: "green"
            decreasingColor: "red"
        }
    }
    

    【讨论】:

    • 不,基于Instantiator 的解决方案不起作用(图表上没有数据)。此外,当我使用Instantiator 时,应用程序(PySide2 应用程序)在退出时崩溃。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-12-26
    • 1970-01-01
    • 1970-01-01
    • 2013-03-27
    • 2019-08-13
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多