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. ListView: position CurrentItem as last visible element?
QtWS25 Last Chance

ListView: position CurrentItem as last visible element?

Scheduled Pinned Locked Moved Unsolved QML and Qt Quick
qmllistviewcurrentindex
1 Posts 1 Posters 499 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
    mks__
    wrote on 25 Oct 2020, 17:00 last edited by
    #1

    Hello,

    When using highlightRangeMode: ListView.StrictlyEnforceRange the view will use preferredHighlightBegin and preferredHighlightEnd properties to position the currentItem in the relevant part of the visibileArea. However, it seems that it will favor preferredHighlightBegin over preferredHighlightEnd, i.e. if the item is larger than the indicated range, it will position it at preferredHighlightBegin and the item will span over preferredHighlightEnd. This is ok when you want to position the item at the beginning of the visibleArea, in the center, or somewhere inbetween. However, it becomes a problem when you want to display the currentItem as the last visible item and make sure the entire item is visible. A naive approach to achieve this would be:

    preferredHighlightBegin: width - currentItem.width
    preferredHighlightEnd: width
    

    This will lead to either binding loops or will make the view enter an infinite loop if items' width changes (for a horizontally oriented view). If items had the same width, that could be solved with:

    preferredHighlightBegin: width - constItemWidth
    preferredHighlightEnd: width
    

    But in my case, they don't have the same width. I was looking into this and found a solution, which is not as elegant as I would prefer. Namely, if layoutDirection: Qt.RightToLeft then preferredHighlightBegin and preferredHighlightEnd are reversed, i.e.

    preferredHighlightBegin: 0
    preferredHighlightEnd: currentItem.width
    

    results in currentItem being snapped to the list's visual end and view's behavior is stable even when items' width is changing (for example, when zooming in/out). However, of course, the order of items from the model is reversed as well, e.g. if items in the model are in ascending order they will be displayed in descending order. So the not-so-elegant solution is to reverse the order of items in the model. That would solve my visual-alignment issues, but require me to change the underlying model in a non-trivial way.

    Is there a better way of solving this? Perhaps without changing the layoutdirection. One important note - the current item is never the last item in the model, there are always items after it, which are not visible and when the model is scrolled through, more items are added.

    1 Reply Last reply
    0

    1/1

    25 Oct 2020, 17:00

    • Login

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