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 behavi开发者_StackOverflow社区or 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?
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.
精彩评论