Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

How to use repeater with Shape+ShapePath?



  • I have a model that is just a list of dict objects that have x and y values in them. The Qt Documentation for Shape says:

    As a general rule, scenes should avoid using separate Shape items when it is not absolutely necessary. Prefer using one Shape item with multiple ShapePath elements over multiple Shape items.

    So I want to use a repeater, something to this effect:

    Shape {
        id: root
        Repeater {
            model: myModel
            ShapePath {
                startX: modelData.startX
                startY: modelData.starty
                PathQuad {
                    x: modelData.width
                    y: modelData.height
                    controlX: modelData.width / 2
                    controlY: modelData.height / 2
                }
            }
        }
    }
    

    but I get this error on the first line of the ShapePath item: QML Component: Delegate must be of Item type

    How can I create multiple ShapePaths based on a model?



  • There is a way, but i dont quite like it. But it works. Shapes added this way will not automaticly update when used with custom QAbstractListModel

    Shape{
    	id: shape
    	anchors.fill: parent
    			
    	Repeater{
    		id: repeater
    		model: myModel
    			delegate: Item{
    				property ShapePath s: ShapePath{					
    				strokeWidth: 2
    				startX: model.x
    				startY: model.y
                                    PathLine{x: model.x2; y: model.y2}
    				}
    				Component.onCompleted: {
    					shape.data.push(s)
    				}
    			}
    		}		
    	}
    }
    

Log in to reply