Home Ask Login Register

Developers Planet

Your answer is one click away!

Scottfivefour February 2016

How can I make the selected row in a QML table view remain the same when changing the model

I am using a qml table view to display a set of values. On each row, there are buttons that allow the user to adjust the value.

My model is an array of qobjects. Every time the user does something to change a value, I am sending a signal to my controller class, it is creating a new model and giving that to the table view. Unfortunately, when you update the model, the selected row is set to -1. Is there some way to change that behavior?


Miika Pirttilä February 2016

I think you should update the model, not change it entirely.

That said, your problem is that the currently selected row index (currentRow) changes, yes? If so, perhaps you should store the index in a variable, which you update like so:

TableView {
    property int unwaveringCurrentRow
    onCurrentRowChanged: { if (currentRow != -1) unwaveringCurrentRow = currentRow }

    onModelChanged: { currentRow = unwaveringCurrentRow }

Also, note how the current row is reset again, when the model changes.

Post Status

Asked in February 2016
Viewed 1,436 times
Voted 6
Answered 1 times


Leave an answer

Quote of the day: live life