Qt World Summit: Submit your Presentation

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) {
            var fromX = list.contentX
            list.positionViewAtIndex(idx, ListView.Beginning)
            var toX = list.contentX
            listAnimation.from = fromX
            listAnimation.to = toX

    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: {
            myIndex = 0