ListView as initial item of StackView (scrolling)

I’ve tried asking this on Reddit and the official Qt forums because this is more of a general Qt/QML question and not about Silica, but I’m not getting useful responses. Usually an indication that I am fundamentally holding “it” wrong, but anyway…

(Also note before providing answers that I’m not working on an open-source app.)

I have been having a hell of a time using a ListView (LV) as the initial item of a StackView (SV) and the LV remembering its scrolling position.

No reproducable example yet since I’m working on a bunch of assumptions that I am not sure are true and I want to check that first.

This is with Qt 5.15.

I am using QML with LQML. In my main QML file I create a StackView with a ListView child and set that LV as its initial item. There’s many items in the LV so it scrolls. Whenever I select an item I SV.push() a new QML file onto the stack with QT.resolvedURL.

When I do an SV.pop() in the newly pushed page the main LV page is always reset to the top and not at its latest scrolling position when the push was done.

Is my assumption correct that when returning to the initial LV it should have remembered its scrolling position?

Sure we can discuss about a piece of programm and what it should do. But this needs many beers (your are welcome) and the beer is the only reason to do so. And as you maybe know we here in the Sailfish world don’t use StackView directly.

Before you execute the SV.push save the current position of the ListView in a global variable. And restore the position after this page is activating. This is a short way to do and save time for the discussion described above. Please send a PM for the date :wink:

1 Like

Without knowing the details, I’d:

  1. when the list element is clicked, store its index somewhere. Maybe in something like a property int lastClickedIndex of the ListView itself, or somewhere global.
  2. when the pop() has finished, use positionViewAtIndex method of the ListView to bring the last clicked item into view.

It might also work to use the highlightFollowsCurrentItem and currentIndex properties.

Note: It’s probably better to use some unique ID of the item instead of the index, because assuming the View is displaying a model, and the model contents may have changed between the push() and the pop(), right? So indexes may have changed.

1 Like

Thanks so far.

I’ve indeed been storing the position of the ListView and restoring it when I return to it. (Although I’ve not found a good way to store it yet it’s “unstable”, but will try @nephros suggestions.)

But this confirms that I should not expect the ListView to remember the position by itself.

Alright, it looks like I finally got it working!

Your suggestions made my code less ugly as well but my main issue was positionViewAtIndex not firing consistently when pressing the back button.

I had put it in a Qt.callLater at someone’s suggestion a while ago and did not realize this might be the culprit. Outside of a Qt.callLater it also didn’t fire consistently but I’ve now put it in a Timer and that seems to work all the time :crossed_fingers:

Thanks for the help!

(I’m also now using a unique ID to find the index although the model shouldn’t change between the push and pop calls.)