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

How to add rectangle in qml dynamically?



  • How add rectangle dynamically with different sizes in qml??I just try with repeater but I can't edit sizes in repeater.



  • @Bharathi

    Hi.

    See the following example.

    import QtQuick 2.5
    import QtQuick.Window 2.2
    
    Window {
        visible: true
        width: 640
        height: 480
        title: qsTr("Hello World")
    
        MouseArea {
            anchors.fill: parent
            onClicked: {
                var newObject = Qt.createQmlObject('import QtQuick 2.5; Rectangle {color: "red";}', parent);
                newObject.x = mouseX;
                newObject.y = mouseY;
                newObject.width = Math.random()*100+10;
                newObject.height = Math.random()*100+10;
            }
        }
    
        Text {
            text: qsTr("Hello World")
            anchors.centerIn: parent
        }
    }
    
    

  • Qt Champions 2017

    You can also try with Loader or Qt.CreateComponent example given in the Assistant.


Log in to reply