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

Adding to GridLayout dynamically



  • I am trying to create some objects and add them to a GridLayout dynamically. I have a couple of questions on how to pull it off.

    I have the GridLayout created.

    GridLayout
    {
        id: grid
        anchors.fill: parent
        rowSpacing: 2
        columnSpacing: 2
    }
    

    I am creating objects to populate it like this

    var object = Qt.createComponent("MyObject.qml");
    var instance = object.createObject(grid);
    

    Will this actually add the object to the grid? How can I set Layout properties on the object, for example Layout.fillHeight?



  • @krobinson hi,

    see this exemple from http://doc.qt.io/qt-5/qtqml-javascript-dynamicobjectcreation.html

    function createSpriteObjects() {
        component = Qt.createComponent("Sprite.qml");
        sprite = component.createObject(appWindow, {"x": 100, "y": 100});
    
        if (sprite == null) {
            // Error Handling
            console.log("Error creating object");
        }
    }
    


  • After a bit more research I have realized that I am creating a Component object. So I cant just set the layout attached property on there. I need to access the actual item defined in the Component. How can I do that? Doing it by name just seems to be undefined.

    console.debug(instance.myItemName)
    

    That just returns undefined. How do I access the internal items in the component?


Log in to reply