如何解决QML QtCharts CandlestickSeries Repeater
我想用来自自定义类的数据填充 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,但也失败了。
解决方法
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"
}
}
,
Repeater
不起作用,因为 CandlestickSet
不是 Component
。 CandlestickSeries
有一个 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));
}
}
}
注意:在线Qml编译器不支持QtCharts,所以我没有测试代码。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。