如何使用Repeater将PathLine添加到ShapePath?

llm*_*llm 3 qt repeater shapes qml

更新 1 - 我可以使用 Javascript 让它工作 - 但这似乎有点未优化(激活时 qmlscene.exe 的负载从 2-3% 到 30%) - 当模型更改时完成重新创建(它不会增长) ),这是唯一的方法还是有更“声明性”的风格可用?

如何将 PathLine 添加到基于模型的 ShapePath?

  • 我知道如何从项目外部使用中继器,但不知道如何在项目内部内爆它们
  • 我需要在 pathElements 上使用某种中继器委托吗?
  • 我不想使用 LineSeries 组件

在此输入图像描述

import QtQuick.Shapes 1.15
import QtQuick 2.15

Shape {
    ListModel {
        id: myPositions
        ListElement { x: 0; y:38 }
        ListElement { x: 10; y: 28 }
        ListElement { x: 20; y: 30 }
        ListElement { x: 30; y: 14 }
    }

    ShapePath {
        strokeColor: "black"
        strokeWidth: 1
        fillColor: "transparent"

        startX: 0
        startY: 0

        PathLine { x: 0; y: 38 }
        PathLine { x: 10; y: 28 }
        PathLine { x: 20; y: 30 }
        PathLine { x: 30; y: 14 }

//        Repeater {
//            model: myPositions
//            PathLine { x: model.x; y: model.y }
//        }
    }
}
Run Code Online (Sandbox Code Playgroud)

更新1

在此输入图像描述

import QtQuick.Shapes 1.15
import QtQuick 2.15

Shape {
    ListModel {
        id: myPositions
    }

    function getRandomInt(min, max) {
        min = Math.ceil(min);
        max = Math.floor(max);
        return Math.floor(Math.random() * (max - min + 1)) + min;
    }

    Timer
    {
        interval: 100
        running: true
        repeat: true

        property real myX: 0

        onTriggered: {
            myPositions.append({"x":myX, "y":getRandomInt(0,30)})
            myX = myX + 10
        }
    }

    function createPathLineElements(positionsModel, shapePath)
    {
        var pathElements = []

        for (var i = 0; i < positionsModel.count; i++)
        {
            var pos = myPositions.get(i)
            var pathLine = Qt.createQmlObject('import QtQuick 2.15; PathLine {}',
                                              shapePath);
            pathLine.x = pos.x
            pathLine.y = pos.y
            pathElements.push(pathLine)
        }

        return pathElements
    }

    ShapePath {
        id: myPath

        strokeColor: "black"
        strokeWidth: 1
        fillColor: "transparent"

        startX: 0
        startY: 0

        pathElements: createPathLineElements(myPositions, myPath)
    }
}
Run Code Online (Sandbox Code Playgroud)

Kim*_*Kim 5

使用实例化器

Shape {
    ListModel {
        id: myPositions
    }

    function getRandomInt(min, max) {
        min = Math.ceil(min);
        max = Math.floor(max);
        return Math.floor(Math.random() * (max - min + 1)) + min;
    }

    Timer {
        interval: 100
        running: true
        repeat: true

        property real myX: 0

        onTriggered: {
            myPositions.append({"x":myX, "y":getRandomInt(0,30)})
            myX = myX + 10
        }
    }
    ShapePath {
        id: myPath
        fillColor: "transparent"
        strokeColor: "red"
        capStyle: ShapePath.RoundCap
        joinStyle: ShapePath.RoundJoin
        strokeWidth: 3
        strokeStyle: ShapePath.SolidLine
    }
    Instantiator {
        model: myPositions
        onObjectAdded: myPath.pathElements.push(object)
        PathLine {
            x: model.x
            y: model.y
        }
    }
}
Run Code Online (Sandbox Code Playgroud)