How to insert an Item at any position in StackLayout?



  • I need to add a component at a particular position in stack layout and I am not getting how to insert, following is the code.

    import QtQuick 2.6
    import QtQuick.Controls 2.2
    import QtQuick.Layouts 1.3
    
    Component{
        id:comp
        Rectangle{
            id:rect
            anchors.fill:parent
        }
    }
    
    StackLayout
    {
        id:stack
        anchors.fill:parent
    
        currentIndex:index //spin box will update this property 
    }
    
    Button
    {
        id:insert
        onClicked:
        {
            var res = comp.createObject(stack) // Should insert a rect at right to the current index
        }
    }


  • @pra7 Do you mean that the given code works, but you want to insert the object to a certain index instead of just appending it (as happens when you give the parent in createObject?) This is untested, but... StackLayout is an Item which has the 'data' property, the list of objects whose lifetime is managed by the object (see "Concepts - Visual Parent in Qt Quick" in the Qt documentation). Probably the new object is automatically appended to the data list when the parent is given in createObject(). Maybe you can create the object without a parent and immediately insert the new child into a given index in the 'data' list.



  • @Eeli-K Thanks for your suggestion and I will give a try.But I found a way to insert an item at the particular index by using repeaters and Object Model inside a layout, For more information can also refer below link:

    https://stackoverflow.com/a/43225476/6336374



Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.