Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. QML: ListView highlightFollowsCurrentItem and shortest path
QtWS25 Last Chance

QML: ListView highlightFollowsCurrentItem and shortest path

Scheduled Pinned Locked Moved Unsolved General and Desktop
listviewdelegatemodelhighlight
4 Posts 2 Posters 2.3k Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • M Offline
    M Offline
    Mark81
    wrote on last edited by
    #1

    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?

    p3c0P 1 Reply Last reply
    0
    • M Mark81

      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?

      p3c0P Offline
      p3c0P Offline
      p3c0
      Moderators
      wrote on last edited by
      #2

      @Mark81 Use positionViewAtIndex.

      157

      M 1 Reply Last reply
      0
      • p3c0P p3c0

        @Mark81 Use positionViewAtIndex.

        M Offline
        M Offline
        Mark81
        wrote on last edited by
        #3

        @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!

        p3c0P 1 Reply Last reply
        0
        • M Mark81

          @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!

          p3c0P Offline
          p3c0P Offline
          p3c0
          Moderators
          wrote on last edited by p3c0
          #4

          @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
             }
          }
          

          157

          1 Reply Last reply
          0

          • Login

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • Users
          • Groups
          • Search
          • Get Qt Extensions
          • Unsolved