Remy Blank
Remy Blank

Reputation: 4275

Keeping the edited item selected in a QListView

I have implemented a custom QAbstractListModel that is displayed in an editable QListView. The items in the model must be sorted, so the model implements the sort() method, and calls it from setData() after the data is stored.

The current behavior is that the user edits an item, the model is sorted, and the QListView is updated. But the selection remains on the item at the index where the edit happened. Instead, I would like the selection to move to the new index of the edited item.

How can I have the selection move with the item?

Upvotes: 1

Views: 420

Answers (1)

Remy Blank
Remy Blank

Reputation: 4275

I have finally found a way to achieve that. It is as simple as connecting to the dataChanged(QModelIndex, QModelIndex) signal of the model and updating the selection in the handler. My error was that I called the signal in setData() before sorting the model.

Instead, the model must be sorted first, then the new index of the modified item can be found, and finally, the dataChanged(QModelIndex, QModelIndex) signal must be called with the new index.

Upvotes: 1

Related Questions