<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0"><channel><title><![CDATA[QML ListView: center current item]]></title><description><![CDATA[<p dir="auto">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 <strong>snap to item's center</strong>.</p>
<p dir="auto"><a href="http://pasteboard.co/OCzMfNat6.png" target="_blank" rel="noopener noreferrer nofollow ugc">Click for image</a></p>
<p dir="auto">I've tried several solutions but none of them works smoothly enough. Any ideas?</p>
<pre><code>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
             }
        }
    }
}
</code></pre>
]]></description><link>https://forum.qt.io/topic/77643/qml-listview-center-current-item</link><generator>RSS for Node</generator><lastBuildDate>Wed, 06 May 2026 10:50:11 GMT</lastBuildDate><atom:link href="https://forum.qt.io/topic/77643.rss" rel="self" type="application/rss+xml"/><pubDate>Mon, 27 Mar 2017 22:33:45 GMT</pubDate><ttl>60</ttl><item><title><![CDATA[Reply to QML ListView: center current item on Thu, 06 Aug 2020 15:37:05 GMT]]></title><description><![CDATA[<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="/user/j-atp">@<bdi>J-ATP</bdi></a> This causes a Binding loop</p>
]]></description><link>https://forum.qt.io/post/610941</link><guid isPermaLink="true">https://forum.qt.io/post/610941</guid><dc:creator><![CDATA[seyed]]></dc:creator><pubDate>Thu, 06 Aug 2020 15:37:05 GMT</pubDate></item><item><title><![CDATA[Reply to QML ListView: center current item on Tue, 28 Mar 2017 14:57:11 GMT]]></title><description><![CDATA[<p dir="auto">Do you mean something like this?</p>
<pre><code>             preferredHighlightBegin: width /2 - currentItem.width /2
             preferredHighlightEnd: width /2 + currentItem.width /2
</code></pre>
]]></description><link>https://forum.qt.io/post/384766</link><guid isPermaLink="true">https://forum.qt.io/post/384766</guid><dc:creator><![CDATA[J ATP]]></dc:creator><pubDate>Tue, 28 Mar 2017 14:57:11 GMT</pubDate></item></channel></rss>