Solved Prgramatically set ScrollView's position
-
I have a ListView inside a Scrollview. When the user clicks on an item it brings up a dialog to edit a value. When the user hits save the data gets saved in C++ where endResetModel gets called which causes the item in the QML Listview to get updated. So far so good, but what happens is that the ScrooView goes back to the top. I have tried eveything I can think of to set the ScrollView to the proper position but nothing works. Can anyone please give me advise on how to move scrollbar. Here is some of my code:
onValueChanged: { if (manageInventoryPage.restoreYPosition) { // get this console.log("restore position to: + lastYPosition) mylistview.contentY = lastYPosition; restoreYPosition = false; myScrollView.flickableItem.contentY = lastYPosition } }
Edit: wrapped code with backticks -- @Wieland
-
Is it absolutely necessary to reset the whole model? Reseting a model means that the view gets re-built from scratch. The view can only retain the scrolling position if you modify the model so that you insert, remove, or change data.
-
Thanks for the reply. I found that if I did not do beginResetModel and endResetModel in my model that inherrits from QAbstractListModel, I find that the updates do not show up at all in the QML. Do you know of another way to get the updates into my QML.
Thanks again for taking the time to reply
-
If you change the data of existing items, emit
dataChanged()
. If you insert new row(s), callbeginInsertRows()
before inserting new rows into the data structure, andendInsertRows()
immediately afterwards. If you remove row(s), callbeginRemoveRows()
before removing old rows from the data structure, andendRemoveRows()
immediately afterwards. -
Thank you for the vital lesson on how to properly update items in a QML ListView from C++, the dataChanged signal worked like a charm with no other code necessary in the QML.