Search code examples
qtqmlqtquick2qt-quickqtquickcontrols2

QML - 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 a position not at the end
    }
}

Solution

  • Got the answer and also may be duplicate of:
    https://stackoverflow.com/a/43225476/6336374

    I used insert(index,item) of ObjectModel

    Thanks to @derM it helped a lot.