[Development] ListView.positionViewAtIndex() seems to be unreliable during object initialization
Aleix Pol
aleixpol at kde.org
Mon Jun 2 01:49:10 CEST 2014
On Sat, May 31, 2014 at 11:19 PM, Baldo Davide <baldosoft at gmail.com> wrote:
> Hello,
>
> I'm having in issue with ListView.positionViewAtIndex(), i'm changing the
> index with the last selection of the user in Component.onCompleted(),
> but positionViewAtIndex() seems to behave randomly: sometime the listview
> is moved correctly, sometime is off by 1 or 2 items.
> I've been having this issue since 5.0 and i can still reproduce it on 5.3,
> i spent several hours trying to debug it but without luck, in the end i
> added a workaround:
>
> Timer {
>
> interval: 100
>
> repeat: false
>
> running: true
>
> onTriggered: {
>
> main.alignIndex()
>
> }
>
> }.
> but it's really a bad solution, and it's even visible.
>
> Is it wrong to call positionViewAtIndex() inside Component.onCompleted()?
> If so, when should i call it?
>
> The component is a simple digit selector.
>
> --- CODE ---
>
> import QtQuick 2.0
>
>
> Rectangle {
>
> id: main
>
> width: 60
>
> height: 180
>
> color: "transparent"
>
> border.color: "black"
>
> border.width: 3
>
> radius: 3
>
> clip: true
>
>
> property int initialDigit: 0;
>
> property int digit: 0;
>
> property string textColor: "violet";
>
>
> function alignIndex()
>
> {
>
> var newIndex = main.digit + 50
>
> listView.positionViewAtIndex(newIndex,ListView.Center);
>
> }
>
>
> /** workaround, force refresh after initialization **/
>
> Timer {
>
> interval: 100
>
> repeat: false
>
> running: true
>
> onTriggered: {
>
> main.alignIndex()
>
> }
>
> }
>
>
> onDigitChanged: {
>
> if( main.digit >= 10 ) {
>
> main.digit = main.digit % 10
>
> }
>
> main.alignIndex()
>
> }
>
>
> Component.onCompleted: {
>
> for( var i = 0; i < 10; i +=1 )
>
> {
>
> for( var n = 0; n < 10; n += 1 )
>
> {
>
> listModel.append({
>
> digit: n
>
> })
>
> }
>
> }
>
>
> //console.log("initial digit: "+initialDigit)
>
> digit = initialDigit
>
> alignIndex();
>
> }
>
>
> Timer {
>
> running: true
>
> repeat: false
>
> interval: 1
>
> onTriggered: {
>
> main.alignIndex()
>
> }
>
> }
>
>
> ListView {
>
> id: listView
>
> anchors.fill: parent
>
>
> snapMode: ListView.SnapPosition
>
>
> Rectangle {
>
> anchors.left: listView.left
>
> anchors.right: listView.right
>
> anchors.margins: 3
>
> height: 60 * 0.8
>
> y: main.height/2.0 - height/2.0 - 3
>
> color: "#37ffffff"
>
> }
>
>
> onMovementEnded: {
>
> main.digit = listView.indexAt(
>
> listView.contentX,
>
> listView.contentY + main.height/2.0
>
> ) % 10
>
> }
>
>
> model: ListModel {
>
> id: listModel
>
> }
>
>
> orientation: ListView.Vertical
>
>
> delegate: Component
>
> {
>
> id: digitDelegate;
>
> Item
>
> {
>
> height: 60
>
> width: main.width
>
> Text {
>
> id: digitText
>
> anchors.fill: parent
>
> text: digit
>
> font.pixelSize: parent.height * 0.8
>
> verticalAlignment: Text.Center
>
> horizontalAlignment: Text.Center
>
>
> Component.onCompleted: {
>
> digitText.color = main.textColor
>
> }
>
> }
>
> }
>
> }
>
> }
>
> }
>
>
>
> --
> Regards,
> Davide Baldo
>
> _______________________________________________
> Development mailing list
> Development at qt-project.org
> http://lists.qt-project.org/mailman/listinfo/development
>
>
You probably want to report this to the bug tracking system.
https://bugreports.qt-project.org
Aleix
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.qt-project.org/pipermail/development/attachments/20140602/c79d07a8/attachment.html>
More information about the Development
mailing list