[英]qml chartview becomes nullptr when sent to C++
I have a data source containing multiple series of data (not known upfront).我有一个包含多个数据系列的数据源(预先未知)。
I want to add a LineSeries
to a qml ChartView
, so I tried to 'hook' the C++ side to the qml like so:我想将LineSeries
添加到ChartView
,所以我尝试将 C++ 端“挂钩”到ChartView
,如下所示:
// data source
...
Q_PROPERTY(QtCharts::QChartView *chart READ chart WRITE setChart NOTIFY chartChanged)
public slots:
void setChart(QtCharts::QChartView *newChart) {
qDebug() << "received new chart to draw on:" << newChart;
}
And in the qml, I send the chart to C++ more or less like:在 qml 中,我或多或少地将图表发送到 C++:
...
ChartView { id: chart
...
}
Component.onCompleted: { backend.setChart(chart) }
Now the setChart
is called allright, but the type does not appear to match: the incoming chart pointer is null:现在setChart
被调用了,但类型似乎不匹配:传入的图表指针为空:
> received new chart to draw on: QWidget(0x0)
Relaxing the input type to plain QObject*
has shown me that the actual type of the incoming object is QtQuick::DeclarativeChart
.将输入类型放宽为普通QObject*
向我展示了传入对象的实际类型是QtQuick::DeclarativeChart
。
How should I send the chart
item to my C++ model?我应该如何将chart
项发送到我的 C++ 模型? (Or should I use a totally different approach?) (或者我应该使用完全不同的方法?)
If you need to update chart axis and series you should send them into c++ and update them i wrote an example you can take a look i think it dose what you want . 如果您需要更新图表轴和序列,则应将其发送到C ++中并进行更新。我写了一个示例,您可以看一下,我认为它满足了您的要求。 On this example i converted c++ voice example from QWidget charts to QML chart but info in series are updating in c++ : QML chart updating in c++ Example 在此示例中,我将c ++语音示例从QWidget图表转换为QML图表,但是系列信息在c ++中进行了更新: QML图表在c ++示例中进行了更新
I ran into a similar problem (I needed access to the chart in C++ to hide some markers on the chart legend).我遇到了类似的问题(我需要访问 C++ 中的图表以隐藏图表图例上的一些标记)。
I was able to get access to the chart by passing a pointer to an existing series on the chart to my C++ code.通过将指向图表上现有系列的指针传递给我的 C++ 代码,我能够访问图表。
void GraphHelper::hideLegends(QtCharts::QScatterSeries *series) {
QtCharts::QChart *chart = series->chart();
// do something with the chart...
}
''' '''
Maybe this could help?也许这会有所帮助?
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.