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

Component createObject has no effect



  •    Rectangle{
            id: selectedArea
            color: "transparent"
    
            anchors.top: topLeft.top
            anchors.left: topLeft.left
            anchors.right: bottomRight.right
            anchors.bottom: bottomRight.bottom
    
            Component{
                id: verticalLine
                ShapePath {
                    strokeColor: "red"
                    strokeWidth: 1
                    startY: 0
                    PathLine {
                        x: 0
                        y: 100
                    }
                    Component.onCompleted: {
                        console.log(startX);
                    }
                }
            }
    
            Shape {
                id: net
                anchors.fill: parent
                z:1
                Component.onCompleted: {
                    for(var i = 0; i != 10; i ++){
                        verticalLine.createObject(net, {startX: i * 10}); // object created but nothing visible
                    }
                }
            }
    
    
    
        }


  • @exru
    You need to manage the list of items in the data property of Shape. This is a list. So I added your object to the list:

    Rectangle{
            id: selectedArea
            color: "transparent"
    
            anchors.fill: parent
    
            Component{
                id: verticalLine
                ShapePath {
                    strokeColor: "red"
                    strokeWidth: 1
                    startY: 0
    
                    PathLine { x: 0; y: 100 }
    
                    Component.onCompleted: {
                        console.log(startX);
                    }
                }
            }
    
            Shape {
                id: net
                anchors.fill: parent
    
                z:1
                Component.onCompleted: {
                    var obj
    
                    for(var i = 0; i !== 10; i ++){
                        obj = verticalLine.createObject(net, {startX: i * 10})
                        data.push(obj)
                    }
                }
            }
        }
    

    You can also create a new list, push items onto list, and substitute:

    var list=[]
    var obj
    ...
    obj = createObject...
    list.push(obj)
    ...
    data = list
    




  • @exru
    You need to manage the list of items in the data property of Shape. This is a list. So I added your object to the list:

    Rectangle{
            id: selectedArea
            color: "transparent"
    
            anchors.fill: parent
    
            Component{
                id: verticalLine
                ShapePath {
                    strokeColor: "red"
                    strokeWidth: 1
                    startY: 0
    
                    PathLine { x: 0; y: 100 }
    
                    Component.onCompleted: {
                        console.log(startX);
                    }
                }
            }
    
            Shape {
                id: net
                anchors.fill: parent
    
                z:1
                Component.onCompleted: {
                    var obj
    
                    for(var i = 0; i !== 10; i ++){
                        obj = verticalLine.createObject(net, {startX: i * 10})
                        data.push(obj)
                    }
                }
            }
        }
    

    You can also create a new list, push items onto list, and substitute:

    var list=[]
    var obj
    ...
    obj = createObject...
    list.push(obj)
    ...
    data = list
    

Log in to reply