Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. QML and Qt Quick
  4. Scrolling of ListView when changing currentItem
Forum Updated to NodeBB v4.3 + New Features

Scrolling of ListView when changing currentItem

Scheduled Pinned Locked Moved QML and Qt Quick
4 Posts 2 Posters 4.2k Views 1 Watching
  • 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.
  • J Offline
    J Offline
    jech
    wrote on 27 Jan 2012, 09:08 last edited by
    #1

    Hi,

    I have a ListView of many items and I like to keep the selected item always visible and keep also one above the selected and one below the selected. This can be done using preferredHighlightBegin and preferredHighlightEnd. It seems to work fine but there is a strange behavior when selecting items which are distant from each other.

    For example let's say I select item #2 then I scroll down and select item #15 which is at the bottom of the view. What I would expect is that the ListView will move a little up so the item becomes not last visible but 2. from bottom. What in fact happens is that the ListView immediately displays item #2 and then scrolls down to item #15.

    How do I avoid this strange behavior? The only thing I could come up is to set the ListView currentIndex first to -1 and then to the desired index. But this causes that the ListView will directly skip to desired position, not scroll smoothly.

    Unfortunately my English is not perfect and I'm not sure if I could explain the problem correctly. So I created this sample. Just save it as a QML file and open it in the viewer. Then try to select distant items.

    @import QtQuick 1.0

    Rectangle {
    width: 200
    height: 200
    color: "#333333"

    ListModel {
        id: listModel
    }
    

    Component {
    id: listDelegate

    Item {
        anchors.right: parent.right
        anchors.left: parent.left
        height: 50
    
        Rectangle {
            id: frame
            anchors.fill: parent
            anchors.rightMargin: 5
            anchors.leftMargin: 5
            anchors.topMargin: 5
            anchors.bottomMargin: 5
            color: "#00000000"
            radius: parent.height/4
            border.width: 2
            border.color: listView.currentIndex == index ? "#ffff00" : "#ffffff"
            
            MouseArea {
                id: itemMouseArea
                anchors.fill: parent
                anchors.rightMargin: 0
                onClicked: {
                    listView.currentIndex = index
                }
            }
        
            Text {
                id: text
                color: "#ffffff"
                text: itemText
                anchors.centerIn: parent
                font.pointSize: 20
            }
        }
    }
    

    }

    ListView {
        id: listView
        clip: true
        anchors.fill: parent
        model: listModel
        delegate: listDelegate
        preferredHighlightEnd: 150
        preferredHighlightBegin: 50
        highlightRangeMode: ListView.ApplyRange
    }
    
    Component.onCompleted: {
        for (var i = 0; i < 21; i++) {
            listModel.append({"itemText": "Item #"+i})
        }
    }
    

    }@

    1 Reply Last reply
    0
    • G Offline
      G Offline
      GentooXativa
      wrote on 27 Jan 2012, 09:19 last edited by
      #2

      You tryed to use
      @followCurrentItem: true@

      Jose Vicente Giner Sanchez - Senior Mobile Developer

      www.gigigo.com

      C/ Dr. Zamenhof 36bis, 1ÂșA 28027 Madrid
      T: +34 917431436

      1 Reply Last reply
      0
      • J Offline
        J Offline
        jech
        wrote on 27 Jan 2012, 19:54 last edited by
        #3

        AFAIK ListView has no property followCurrentItem, it only has highlightFollowsCurrentItem, which doesn't make any difference.

        1 Reply Last reply
        0
        • J Offline
          J Offline
          jech
          wrote on 30 Jan 2012, 15:56 last edited by
          #4

          Bump!

          Could anyone at least please confirm, if this is intended behavior or a bug in Qt?

          I'm sorry for my impatience, but I would like to make a release of my program and this is blocking me from doing it.

          1 Reply Last reply
          0

          1/4

          27 Jan 2012, 09:08

          • Login

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