gpt4 book ai didi

qt - QML,在运行时创建 LineSeries

转载 作者:行者123 更新时间:2023-12-02 00:57:14 63 4
gpt4 key购买 nike

我的目标是拥有一个 QML ChartView,并在运行时添加可变数量的 LineSeries。在用户选择并加载包含数据的文件之前,尚不清楚需要添加多少 LineSeries

我尝试在 Repeater 内创建所有 LineSeries,但没有成功。我怀疑这是因为 ChartView 不知道如何处理一堆 Item。不可能让 Repeater 直接创建 LineSeries,因为 Repeater 不适用于 QObject:

Repeater {
model: numberOfColumnsInModel / 2

delegate: Item {
LineSeries {
id: lineSeries
axisX: xAxis
axisY: yAxis

VXYModelMapper {
id: modelMapper
model: lineChart.model //Reimplemented QAbstractTableModel
xColumn: index * 2
yColumn: index * 2 + 1
}

onHovered: {
console.log("Do something...");
}
}
}
}

在我在线看到的示例中,每个 LineSeries 都是硬编码的 - 对于 ChartView 中的每一行都硬编码一次 - 对我来说没有用。

最佳答案

使用force文档,卢克。在下面的示例中,在启动时创建随机数量的线和随机数量的点:

import QtQuick 2.7
import QtQuick.Window 2.2
import QtQuick.Controls 2.0
import QtCharts 2.1

Window {
id: window1
title: "Chart test"
visible: true
width: 600
height: 400

ChartView {
id: chart
anchors.fill: parent
axes: [
ValueAxis{
id: xAxis
min: 1.0
max: 10.0
},
ValueAxis{
id: yAxis
min: 0.0
max: 10.0
}
]
Component.onCompleted: {
var seriesCount = Math.round(Math.random()* 10);
for(var i = 0;i < seriesCount;i ++)
{
var series = chart.createSeries(ChartView.SeriesTypeLine, "line"+ i, xAxis, yAxis);
series.pointsVisible = true;
series.color = Qt.rgba(Math.random(),Math.random(),Math.random(),1);
series.hovered.connect(function(point, state){ console.log(point); }); // connect onHovered signal to a function
var pointsCount = Math.round(Math.random()* 20);
var x = 0.0;
for(var j = 0;j < pointsCount;j ++)
{
x += (Math.random() * 2.0);
var y = (Math.random() * 10.0);
series.append(x, y);
}
}
}
}
}

关于qt - QML,在运行时创建 LineSeries,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47928276/

63 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com