ListView positionViewAtIndex() not working
listview, qml, qt
Solution
So, I figured it out. It turns out that a `ListView` instantiates its delegates before it worries about its own properties...so the delegate was only reading off of the `ListView`'s width before the `ListView` set its own dimensions. When a delegate has a width/height property in the orientation of the view equal to zero, the view will not know where to scroll to when `positionViewAtIndex()` is called. So, in order to fix this, you have to use a conditional binding:
Component {
id: myDelegate
Item {
width: ListView.view.width == 0 ? 480 /*or some preset*/ : ListView.view.width
}
}
This will give the delegate a nonzero width and cause the `positionViewAtIndex()` function to work.
Of course, if your `ListView` is vertical, then you need to set the `height` property and not the `width` property.
Problem
I have two `ListView`s in a QML project that are both running off of the same model. I am trying to get them to start out at different indices (the model starts with 2 `ListElement`s in it). In order to do this, I call `positionViewAtIndex` when the component completes: ``` ListView { model: mymodel Component.onCompleted: positionViewAtIndex(1,ListView.Beginning) //... } ``` However, neither `ListView` actually is positioned at the desired index. Is there something I'm not doing? The only solution that I have seen for this problem is to ensure that you're not calling the method before the `ListView` completes, but I am doing that. I am using Qt 5.2/QtQuick 2.0. Edit: After playing around with the other positioner functions, I have found that none of them work. I have also found that changing `currentIndex` does not work either. Furthermore, I have found that `currentIndex` is not being changed with the view -- `onCurrentIndexChanged` is never being fired.