Итак, что вы хотите, так это:
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.
В 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.Поэтому создайте QString в C ++, а затем отправьте его в функцию javascript через invokeMethod.
Вы можете следовать этому руководству: Создание динамического объекта QML из JavaScript .