How to sort in TableView like QTableView?
-
I think, I don't need to call dataChanged
?
My advice would be: if @SGaist suggests you try something you heed it!
When you call
beginResetModel
Qt throws away all existing information about the model, including which row was selected. The view presumably then just remembers row #2 was selected and reselects that.If instead you call
dataChanged
on each index it may remember which row in the model was selected and re-select the model row, not the view row number.Otherwise, if that does not work or you wish to stick
beginResetModel
, before you sort you should store which row is selected by its content. That is usually the primary key, which looks like it might be youriNumber
? Then after the sort you go find that row again by its content and reselect that. Any problem with that? -
and store selected and reselect is a good idea, but when I select large of data, it will be very slow,
Sorry, I don't understand? All you are supposed to store is the key of whichever row was previously selected, and then you re-find that in the new data order and select that item. What would be "slow"?
-
@JohnDaYe
Yes, but why in the world would you want to select a row 10,000 times, or reselect a given row 10,000 times?I just don't get what you don't get?
- Store the primary key of the currently selected row somewhere.
- Call
beginResetModel
, do the sort,endResetModel
. - Look through the rows in the newly-ordered model to find what the index is now of the row whose primary key you saved away. (Depending on what it's sorted by, this may be optimizable, or not.)
- Call
tableview.selection.select(index)
.
I haven't called
select()
10,000 times, have I? I'm just selecting one row at the end.