Search code examples
qtqt4qmlqt-quick

Adding elements at runtime



How can I add an element to any view at runtime?
for example, when some signal is fired, the application should add a rectangle to a specific row.
Thanks,


Solution

  • Using Component should do the trick.

    MySignalSource {
        Row {
            id: myRow
            anchors.fill: parent
        }
    
        Component {
            id: myRectComp
            Rectangle {
                width: 50
                height: 50
            }
        }
    
        onSignalFired: {
            var rect = myRectComp.createObject(myRow)
            rect.color = "black"
        }
    }
    

    Not tested, but it should work like that.