How to know "y" property of an Item when it is created in a ListView



  • I use a ListView and I need to fill my Window with the last Item that was inserted inside it.
    In this case, I set the ListView property: contentY with the y value of myItem

    My problem is that when you insert a new Item, I don't know how to know the y value of the Item because it is not yet set.

    This is what I try:

    ScrollView {
        id: scroll
        ListView {
            model: DelegateModel {
                id: visualModel
                model: myModel //Model is set in the cpp
                delegate: Rectangle {
                    id: rectangleDelegate
    
                    Component.onCompleted: {
                       rectangleDelegate.ListView.view.contentY = rectangleDelegate.y
                       console.log(rectangleDelegate.y ) //Not set yet (y = 0)
                    }
    
                    Button {
                        onClicked { 
                            rectangleDelegate.ListView.view.contentY = rectangleDelegate.y
                            console.log(rectangleDelegate.y ) //Now it is ok (y = rightValue)
                        }
                    }
                }
            }
        }
    }   
    

    How could I proceed ?


  • Moderators

    Which item addRoot refers to?



  • @sierdzio said in How to know "y" property of an Item when it is created in a ListView:

    Which item addRoot refers to?

    I updated my code, it was a mistake


  • Moderators

    Thanks, now it's more clear.

    Have you tried using positionViewAtIndex? Looks like just what might suit your needs.



  • @sierdzio This is exactly what I need, thanks !


  • Moderators

    Great, happy coding :-)


Log in to reply