Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. QML and Qt Quick
  4. How to insert an Item at any position in StackLayout?
QtWS25 Last Chance

How to insert an Item at any position in StackLayout?

Scheduled Pinned Locked Moved Solved QML and Qt Quick
qtquick2qtquick.controlqmlstacklayout
3 Posts 2 Posters 3.2k Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • P Offline
    P Offline
    pra7
    wrote on 31 Jul 2017, 16:06 last edited by
    #1

    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
        }
    }
    
    E 1 Reply Last reply 31 Jul 2017, 17:20
    0
    • E Eeli K
      31 Jul 2017, 17:20

      @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.

      P Offline
      P Offline
      pra7
      wrote on 1 Aug 2017, 04:38 last edited by
      #3

      @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

      1 Reply Last reply
      0
      • P pra7
        31 Jul 2017, 16:06

        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
            }
        }
        
        E Offline
        E Offline
        Eeli K
        wrote on 31 Jul 2017, 17:20 last edited by
        #2

        @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.

        P 1 Reply Last reply 1 Aug 2017, 04:38
        1
        • E Eeli K
          31 Jul 2017, 17:20

          @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.

          P Offline
          P Offline
          pra7
          wrote on 1 Aug 2017, 04:38 last edited by
          #3

          @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

          1 Reply Last reply
          0

          3/3

          1 Aug 2017, 04:38

          • Login

          • Login or register to search.
          3 out of 3
          • First post
            3/3
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • Users
          • Groups
          • Search
          • Get Qt Extensions
          • Unsolved