首页 > 解决方案 > 将 C++ LineSeries 传递给 QML 图表

问题描述

我必须建立一个 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?

有人能帮我吗?

提前谢谢了。

标签: c++qtqml

解决方案


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

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 函数。

您可以遵循本教程:从 JavaScript 创建动态 QML 对象


推荐阅读