Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to add PathLine to ShapePath with a Repeater?

UPDATE 1 - i can get it to work using Javascript - but that seems to be a little bit unoptimized (from 2-3% to 30% load with qmlscene.exe when activated) - complete recreation when the model changes (its not ever growing), is that the only way or is there a more "declarative" style available?

How can i add a PathLine to a ShapePath based on a Model?

  • i know how to use a Repeater from outside of Items but not how to implode them inside of Items
  • do i need some sort of Repeater-Delegate on pathElements?
  • and i don't want to use the LineSeries component

enter image description here

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 }
//        }
    }
}

UPDATE 1

enter image description here

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)
    }
}
like image 738
llm Avatar asked Jan 20 '26 00:01

llm


1 Answers

Use an Instantiator:

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
        }
    }
}
like image 157
Kim Avatar answered Jan 21 '26 15:01

Kim



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!