How to revert to previous selection when an undo operation happens in QTreeView
-
Hi,
I'm quite new to Qt. So in my current GUI, undoing an operation doesn't return selection back to the previously selected one, which means the previous one doesn't get highlighted.
For example, if I select one part of the model, do some operations, then select another part and undo it, the selection doesn't go back to the previous one, so it must be selected again before I can do other stuff.
-
Hi,
I'm quite new to Qt. So in my current GUI, undoing an operation doesn't return selection back to the previously selected one, which means the previous one doesn't get highlighted.
For example, if I select one part of the model, do some operations, then select another part and undo it, the selection doesn't go back to the previous one, so it must be selected again before I can do other stuff.
-
Let's say I rotated the selected item and now I want to undo this rotation. If I press "ctrl+z" to undo, then the item is deselected, i.e. "un-highlighted", and my selected item is null in this case. So how can I remain the item selected even when the "undo" operation happens such that it is the same as if I had clicked on it?
-
Let's say I rotated the selected item and now I want to undo this rotation. If I press "ctrl+z" to undo, then the item is deselected, i.e. "un-highlighted", and my selected item is null in this case. So how can I remain the item selected even when the "undo" operation happens such that it is the same as if I had clicked on it?
@garcia_g
QTreeView
has QItemSelectionModel *QAbstractItemView::selectionModel() const. And that has e.g. void QItemSelectionModel::select(const QModelIndex &index, QItemSelectionModel::SelectionFlags command). So if you remember QModelIndexList QTreeView::selectedIndexes() const you can use that to re-select.See also e.g. https://stackoverflow.com/questions/4967660/selecting-a-row-in-qtreeview-programmatically or https://forum.qt.io/topic/11170/solved-how-to-set-an-item-in-a-qtreeview-as-selected.
While you are implementing this. Are you aware that Qt has a QUndoStack Class? You might want to use this. It does not provide the undo "operations" --- e.g. adding/deleting/selecting your elements --- you still have to write the code for that, just as now. But it gives you the infrastructure for managing the undo/redo operations, especially if you will allow multi-action undo/redo, which otherwise you will have to write yourself.