QML: ListView highlightFollowsCurrentItem and shortest path



  • I have a ListView which shows items from a model.
    With a timer each 10 seconds I ask the next item:

    Timer {
    	interval: 10000
    	repeat: true
    	running: true
    	triggeredOnStart: true
    
    	onTriggered: {
    		if (list.currentIndex < list.count - 1) list.currentIndex++;
    		else list.currentIndex = 0;
    	}
    }
    
    ListView {
    	id: list
    	anchors.centerIn: parent
    	highlightFollowsCurrentItem: true
    	orientation: ListView.Horizontal
    	spacing: 20
    	model: myModel
    	delegate: myDelegate {}
    }
    

    It's a very simple way to have a scrolling panel.
    It works fine, but when it reaches the last item and I set the currentIndex to 0, it rewind the whole contents, showing all items to go back to the first one!

    How to avoid this?


  • Moderators



  • @p3c0 yes, but what about the animation? I've tried to check the new position but the behavior is the same:

    function gotoIndex(idx) {
            listAnimation.stop()
            var fromX = list.contentX
            list.positionViewAtIndex(idx, ListView.Beginning)
            var toX = list.contentX
            listAnimation.from = fromX
            listAnimation.to = toX
            listAnimation.start()
        }
    

    When I'm viewing the last item and want to go back to the first I see all the others scrolling back!


  • Moderators

    @Mark81 If index is 0 then don't start the animation. Well something like:

    property int myIndex : 0
    
    function animate(idx) {
        listAnimation.running = false
        var pos = list.contentY;
        var destPos;
        list.positionViewAtIndex(idx, ListView.Beginning);
        destPos = list.contentY;
        listAnimation.from = pos;
        listAnimation.to = destPos;
        listAnimation.running = true;
    }
    
    Timer {
       ...
       onTriggered: {
       if(myIndex==0)
            list.positionViewAtIndex(myIndex,ListView.Beginning);
        else
            animate(myIndex)
        myIndex++
        if(myIndex==list.count-1)
            myIndex = 0
       }
    }
    


Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.