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. QML ListView: center current item

QML ListView: center current item

Scheduled Pinned Locked Moved Unsolved QML and Qt Quick
3 Posts 3 Posters 2.8k 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.
  • R Offline
    R Offline
    Ray Gray
    wrote on last edited by Ray Gray
    #1

    Consider a simple horizontally-oriented ListView with a small caveat: items have different widths. We can easily make the currentItem - when flicked - to automatically snap to an arbitrary position (preferredHighlightBegin, preferredHighlightEnd and highlightRangeMode in the code below). This works with fine when item's left border is in question, however there is no easy way to snap to item's center.

    Click for image

    I've tried several solutions but none of them works smoothly enough. Any ideas?

    import QtQuick 2.7
    Item
    {
        width: 600
        height: 100
        ListModel {
            id: myModel
            ListElement { w: 100 }
            ListElement { w: 50 }
            ListElement { w: 200 }
            ListElement { w: 50 }
            ListElement { w: 20 }
        }
     
        Rectangle {
            id: wrapper
            width: parent.width
            height: parent.height
            ListView {
                id: view
                 anchors.fill: parent
                 spacing: 15
                 model: myModel
                 orientation: ListView.Horizontal
                 delegate: Item {
                     width: w
                     height: parent.height
                     Rectangle {
                         anchors.centerIn: parent
                         width: parent.width
                         height: parent.height
                         color: "cyan"
                         Text {
                             text: index
                             anchors.centerIn: parent
                         }
                     }
                 }
                 preferredHighlightBegin: width / 2
                 preferredHighlightEnd: width / 2
                 highlightRangeMode: ListView.StrictlyEnforceRange
                 // One possible solution - but produces strange 'jerking' repositions
                 onCurrentItemChanged: {
                     wrapper.x = -currentItem.width / 2
                 }
            }
        }
    }
    
    1 Reply Last reply
    0
    • J Offline
      J Offline
      J ATP
      wrote on last edited by
      #2

      Do you mean something like this?

                   preferredHighlightBegin: width /2 - currentItem.width /2
                   preferredHighlightEnd: width /2 + currentItem.width /2
      
      S 1 Reply Last reply
      0
      • J J ATP

        Do you mean something like this?

                     preferredHighlightBegin: width /2 - currentItem.width /2
                     preferredHighlightEnd: width /2 + currentItem.width /2
        
        S Offline
        S Offline
        seyed
        wrote on last edited by
        #3

        @J-ATP This causes a Binding loop

        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