gpt4 book ai didi

c++ - 将 C++ LineSeries 传递给 QML 图表

转载 作者:太空宇宙 更新时间:2023-11-04 12:40:26 27 4
gpt4 key购买 nike

我必须构建一个 qml 折线图。我必须构建要在 C++ 中显示的系列,因为我有很多数据要插入。我调用了我的系列 QLineSeries * TaProbe,我需要将它传递给 QML。我该怎么做?在我的 QML 代码下方,我要加载 TaProbe 系列的位置

ChartView {
id: chart
anchors.fill: parent

ValueAxis{
id: xAxis
min: 1.0
max: 10.0
},
ValueAxis{
id: yAxis
min: 0.0
max: 10.0
}
}

在上面的代码中,我可以在哪里加载我的 C++ lineserie?

有人可以帮助我吗?

非常感谢。

最佳答案

所以你想要的基本上是这样的:

ChartView {
title: "Line"
anchors.fill: parent
antialiasing: true

LineSeries {
name: "LineSeries"
XYPoint { x: 0; y: 0 }
XYPoint { x: 1.1; y: 2.1 }
XYPoint { x: 1.9; y: 3.3 }
XYPoint { x: 2.1; y: 2.1 }
XYPoint { x: 2.9; y: 4.9 }
XYPoint { x: 3.4; y: 3.0 }
XYPoint { x: 4.1; y: 3.3 }
}
}

但是 XYPoint 来自您的 Cpp,所以您需要从您的 C++ 创建它?

我对 QML 的 Shape 类做了类似的事情。

在 C++ 中:

ShapeEditor::ShapeEditor() {
_shapeInit = ""
"import QtQuick 2.5; \n"
"import QtQuick.Shapes 1.11; \n"
"Shape { \n"
" property string name: \"\" \n"
" id: prlShape; \n"
" opacity: 0.8; \n"
" containsMode: Shape.FillContains; \n"
" function changeColor(newColor) { \n"
" shapePrlPath.fillColor = newColor; \n"
" shapePrlPath.strokeColor = newColor; \n"
" } \n"
" function changeOpacity(opac) { \n"
" prlShape.opacity = opac; \n"
" } \n"
" function destroyArea() { \n"
" destroy(); \n"
" } \n"
" ShapePath { \n"
" joinStyle: ShapePath.RoundJoin; \n"
" strokeColor: \"yellow\"; \n"
" fillColor: \"yellow\"; \n"
" id: shapePrlPath; \n"
" ";
_shapeEnd = " }}";
}

void ShapeEditor::createShapeItemFromPoint()
{
QVector<QPoint> reorderedList = _pointList;
_shapeForm = " startX:" + QVariant(reorderedList[0].x()).toString() + " ; startY: " + QVariant(reorderedList[0].y()).toString() + " \n";
for (int i = 1 ; i < reorderedList.size() ; i++) {
_shapeForm += " PathLine { id: line" + QVariant(i).toString() + "; x: " + QVariant(reorderedList[i].x()).toString() + " ; y: " + QVariant(reorderedList[i].y()).toString() + "} \n";
}
_shapeForm += " PathLine { id: line" + QVariant(reorderedList.size() + 1).toString() + "; x: " + QVariant(reorderedList[0].x()).toString() + " ; y: " + QVariant(reorderedList[0].y()).toString() + "} \n";
QObject* obj = _frontRoot->findChild<QObject*>("shapeEditor");
QMetaObject::invokeMethod(obj, "createShape", Q_ARG(QVariant, QVariant::fromValue(_shapeInit + _shapeForm + _shapeEnd)))
}

qml:

Item {
id: shapeEditor
objectName: "shapeEditor"

function createShape(str) {
var item = Qt.createQmlObject(str, prlEditor, "shape");
}
}

您可以做的是动态创建您的 QML 动态对象。因此,在 C++ 中创建一个 QString,然后通过 invokeMethod 将其发送到 javascript 函数。

您可以按照本教程进行操作:Dynamic QML Object Creation from JavaScript .

关于c++ - 将 C++ LineSeries 传递给 QML 图表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54459713/

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