将C ++ LineSeries传递给QML图表

时间:2019-01-31 11:35:22

标签: c++ qt qml

我必须建立一个qml折线图。我必须构建要在C ++中显示的系列,因为我要插入很多数据。我给我的意甲QLineSeries * TaProbe打电话,我需要将其传递给QML。我该怎么做? 在我的QML代码下方,我要在其中加载Ta​​Probe行系列

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 ++线路?

有人可以帮我吗?

非常感谢。

1 个答案:

答案 0 :(得分:0)

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

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